Last week while we were all busy getting ready for the holidays, version 5.10 of Perl appeared on CPAN. That surprised me, because I thought the next Perl I'd see would be the dreaded Perl 6.. I don't know when that will be (earlier in 2007 the project manager said "The Perl 6 project has no schedule") but I keep hearing things about February.. oh well, forget about that, here's 5.10 to play with.
I downloaded it from CPAN, unpacked the tar file and took a quick look at the README and the README.macosx - nothing to get excited or worried about there. But still, I'd hate to break any of my current scripts, so it would seem best to install it locally first rather than overwriting Apple's Perl. The package is designed to be helpful in that respect: I made a ~/perl510 directory and then (in the unpacked perl-5.10.0 directory) did:
./Configure -des -Dprefix=$HOME/perl510 make test make install
The "make test" ran a long, long time, which gave me the chance to start reading some of the documentation. I noticed a change that is related to something that bit me once: Perl loop causes strange read-only error explained how I hadn't realized that $_ is not a local variable (even though it sure looks like one!). Well, in Perl 5.10, you can make it local by saying "my $_". If you happen to need the gobal $_, you can use $::_ or "our $_".
While I was thinking about how unlikely it is that I'd ever need that feature, the "make test" finished and it had an error..
Failed 1 test out of 1386, 99.93% okay.
Arrgh. I scrolled back up the screen and found the problem:
ext/Cwd/t/cwd.................................................# Failed test 'cwd()' # at ../ext/Cwd/t/cwd.t line 109. # got: '/Users/apl/Downloads/perl-5.10.0/t' # expected: '/Users/apl/downloads/perl-5.10.0/t' FAILED at test 7
Oh, that's just Perl not grokking old MacOSX filesystem case insensitivity (my machine predates Apple changing its mind about this stuff, and upgrades don't change the underlying filesystem). That shouldn't be a show-stopper, so I proceeded with "make install" and soon had a working Perl 5.10
The first thing I looked at was the new "smart matching" operator (~~). This is "smart" because it doesn't matter what you are comparing - you can pretty much use this with anything:
#!/Users/apl/perl510/bin/perl
$a="hello"; $b="hello you"; $c="hello";
print "a eq b\n" if $a ~~ $b;
print "a eq c\n" if $a ~~ $c;
print "subs a eq b\n" if a() ~~ b();
print "subs a eq c\n" if a() ~~ c();
sub a {
return 5;
}
sub b {
return 6;
}
sub c {
return 5;
}
(output)
a eq c
subs a eq c
That's just the beginning though: you can search arrays or hashes:
#!/Users/apl/perl510/bin/perl
$a{"1"}="one";$a{"2"}="two";$a{"3"}="three";
print "found one\n" if %a ~~ "1";
print "found eight\n" if %a ~~ "8";
push @a,"red"; push @a,"orange"; push @a,"blue";
print "found blue\n" if @a ~~ "blue";
print "found blu \n" if @a ~~ "blu";
print "found blu partial\n" if @a ~~ /blu/;
print "found green\n" if @a ~~ "green";
(output)
found one
found blue
found blu partial
See Smart matching in detail for more examples. Obviously I'd use that a lot - nice feature.
Closely related is the new "switch" block. To use this, you need "use feature 'switch';". The example they give is straightforward:
given ($foo) {
when (/^abc/) { $abc = 1; }
when (/^def/) { $def = 1; }
when (/^xyz/) { $xyz = 1; }
default { $nothing = 1; }
}
but it doesn't tell the whole story. You can actually use "when" anywhere, but it has a side effect which may or may not be desirable. Consider this code:
#!/Users/apl/perl510/bin/perl
use feature "switch";
push @array,"foo";
push @array,"foo";
push @array,"foo 2";
push @array,"red";
push @array,"green";
my $precount = 0;
my $count = 0;
my $postcount = 0;
foreach (@array) {
$precount++ if $_ eq "foo";
when("foo") { ++$count };
$postcount++ if $_ eq "foo";
}
print "(pre) \@array contains $precount copies of 'foo'\n";
print "\@array contains $count copies of 'foo'\n";
print "(post) \@array contains $postcount copies of 'foo'\n";
(output)
(pre) @array contains 2 copies of 'foo'
@array contains 2 copies of 'foo'
(post) @array contains 0 copies of 'foo'
The "when" shortcircuits the loop.
A new "say()" ("use feature say;") is just "print()" with a "\n" added.. just candy. There are new "state" variables (actually since Perl 5.94). These are very useful - so often we'll have to use a global variable only because we need it persistent in a subroutine. Using "state" fixes that (well, you could do it with a closure before that, but this is easier and more obvious):
#!/Users/apl/perl510/bin/perl
use feature qw(state say switch);
tracking();
tracking();
say(tracking());
sub tracking {
state $tracking=1;
# that only happens first time it's called
return ++$x;
}
(output)
3
There's more, of course, but just this was enough to convince me. I hope I'm as easily persuaded when Perl 6 arrives..
More Articles by Anthony Lawrence - Find me on Google+
Have you tried Searching this site?
Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.
We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.
Click here to add your comments
Don't miss responses! Subscribe to Comments by RSS or by Email
Click here to add your comments
If you want a picture to show with your comment, go get a Gravatar