APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Perl 5.10

© December 2007 Anthony Lawrence

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:

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:


$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;

a eq c
subs a eq c

That's just the beginning though: you can search arrays or hashes:



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";

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:

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";

(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):

use feature qw(state say switch);


sub tracking {
 state $tracking=1;
 # that only happens first time it's called
 return ++$x;


There's more, of course, but just this was enough to convince me. I hope I'm as easily persuaded when Perl 6 arrives..

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> Perl 5.10

Inexpensive and informative Apple related e-books:

Digital Sharing Crash Course

Take Control of Numbers

Take Control of IOS 11

Take Control of Automating Your Mac

Take Control of the Mac Command Line with Terminal, Second Edition

More Articles by © Anthony Lawrence

Printer Friendly Version

Have you tried Searching this site?

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.

Contact us

Printer Friendly Version

I am not out to destroy Microsoft, that would be a completely unintended side effect. (Linus Torvalds)

Linux posts

Troubleshooting posts

This post tagged:





Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode