FMc*_*FMc 17
对于类级变量,有两种常用方法:
package Bar;
use strict;
use warnings;
sub new { bless {}, shift };
# (1) Use a lexical variable scoped at the file level,
# and provide access via a method.
my $foo = 123;
sub get_foo { $foo }
# (2) Use a package variable. Users will be able to get access via
# the fully qualified name ($Bar::fubb) or by importing the name (if
# your class exports it).
our $fubb = 456;
Run Code Online (Sandbox Code Playgroud)
用法示例:
use Bar;
my $b = Bar->new;
print "$_\n" for $b->get_foo(), $Bar::fubb;
Run Code Online (Sandbox Code Playgroud)