Using Perl 5.10 features
perl 5.10 includes new language features
incompatible with previous version
use feature ':5.10';
use feature qw(switch say state);
use 5.010
use feature qw(switch say state);
use 5.010
say feature.
#!/usr/bin/perl
#
use strict;
use warnings;
use feature ":5.10";
main(@ARGV);
sub main
{
say "This is the Perl 5.10 new features exercise file.";
say "this is another line";
}
sub error
{
my $e = shift || 'unkown error';
my $me = ( split(/[\\\/]/, $0 ) )[-1];
print("$me: $e\n");
exit 0;
}
#
use strict;
use warnings;
use feature ":5.10";
main(@ARGV);
sub main
{
say "This is the Perl 5.10 new features exercise file.";
say "this is another line";
}
sub error
{
my $e = shift || 'unkown error';
my $me = ( split(/[\\\/]/, $0 ) )[-1];
print("$me: $e\n");
exit 0;
}
switch feature:
switch.pl
#!/usr/bin/perl
#
use strict;
use warnings;
use feature ":5.10";
main(@ARGV);
sub main
{
my $s='jimi hendrix';#5
given($s){
when(undef){say'$s is undefined'}
when('jimi'){say'$s is musician'}
when(/jimi/){say'$s maybe a muscian'}
when([1,3,5,7,9]){say'$s is odd number'}
default{say '$s is something else!'}
}
}
sub error
{
my $e = shift || 'unkown error';
my $me = ( split(/[\\\/]/, $0 ) )[-1];
print("$me: $e\n");
exit 0;
}
#
use strict;
use warnings;
use feature ":5.10";
main(@ARGV);
sub main
{
my $s='jimi hendrix';#5
given($s){
when(undef){say'$s is undefined'}
when('jimi'){say'$s is musician'}
when(/jimi/){say'$s maybe a muscian'}
when([1,3,5,7,9]){say'$s is odd number'}
default{say '$s is something else!'}
}
}
sub error
{
my $e = shift || 'unkown error';
my $me = ( split(/[\\\/]/, $0 ) )[-1];
print("$me: $e\n");
exit 0;
}
state feature;
state.pl
#!/usr/bin/perl
#
use strict;
use warnings;
use feature ":5.10";
main(@ARGV);
sub main
{
my $i = 5;
increment($i);
increment($i);
increment($i);
increment($i);
}
sub increment{
state $n = shift;
say ++$n;
}
sub error
{
my $e = shift || 'unkown error';
my $me = ( split(/[\\\/]/, $0 ) )[-1];
print("$me: $e\n");
exit 0;
}
#
use strict;
use warnings;
use feature ":5.10";
main(@ARGV);
sub main
{
my $i = 5;
increment($i);
increment($i);
increment($i);
increment($i);
}
sub increment{
state $n = shift;
say ++$n;
}
sub error
{
my $e = shift || 'unkown error';
my $me = ( split(/[\\\/]/, $0 ) )[-1];
print("$me: $e\n");
exit 0;
}
注意此时的state关键字,如果用my替换,运行结果会不同!
到此为止,Perl学习手札的更新,告一段落。进阶更新待定