perl中my与local的区别
my: lexically scoped variablelocal:Dynamically scoped variable 内部->外部:(1)my和local都只在一个block里有效,出去就失效;(2)但是local的变量可以继续在这个block中调用的子程序中存在;(3)如果...
Perl中有效创建Getter 和Setter的方法
利用Class::MethodMaker可以有效避免重新写很多getter和setter的代码,具体如下:package Animal;use Class::MethodMakernew_with_init => 'new',get_set => [ -eiffel => [qw(color height name age)]],...
Coverage Testing
Coverage Test 包括:Statement coverage、Branch coverage、Path coverage、expression coverage and so on. 资料Devel::Coverage::Tutorial$perl -MDevel::Cover yourprog args$cover$cover -delete ...
Restricting a Method to Class-Only o Instance-Only
use Carp qw(croak);sub instance_only {ref(my $self = shift ) or croak "instance variable needed";... use $self as the instance ...}sub class_only {ref(my $self = shift) and croak "class nam...
Perl的面向对象方法
{package LivingCreature;sub speak{my $class = shift;my $gender = shift;print "a $gender $class goes ", $class->sound, "!\n";}}{ package Animal;@ISA = qw(LivingCreature);# no definition...
Filehandle References
1)IO::Fileuse IO::File;my $fh = IO::File->new( ' > castaways.log' )or die "Could not create filehandle: $!";my $read_fh = IO::File->new( 'castaways.log', 'r' );my $write_fh = IO::File->new( ...
Clouse Variables as Inputs
use File::Find;sub print_bigger_than {my $minmum_size = shift;return sub { print "$File::Find::name\n" if -f and -s >= $minimun_size};}my $bigger_than_1024 = print_bigger_than(1024);find ($b...
Perl的一些重要模块使用介绍
1) use Cwd;my $directory = cwd;2) use Fatal qw/ open chdir/;chdir '/home/merlyn'; # "or die" is now supplied automatically3) use File::Basename;for (@ARGV){my $basename = basename $_;m...
使用sprintf 输出"money number"
sub big_money{1my $number = sprintf "%.2f", shift @_;2#add one comma each time through the do-nothing loop31 while $number =~ s/^(-?\d+)(\d\d\d)/$1,$2/;4# Put the dollar sign in the right ...
Non-Greedy and Greedy Quantifier In Perl
比较以下两个正则匹配:$_ = "fred and barney went bowling last night ";1)贪心/fred.+barney/过程:首先匹配成功fred,然后.+匹配之后尽量多的信息,即:and barney went bowling last night,随后barne...