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










/Programming/perl510.html Comment posting

Comments by new and anonymous visitors are moderated and must be approved before they will appear on the site. We apologize for the necessity of this procedure.

Your name: (or leave as anonymous)
Your Website (optional): 
Your Email :

(optional - use if you want to be notified of new comments, will not be displayed )

We have strong spam controls here: preview your post to be sure it will be let through.
Comments over 6K in length cannot be posted. Please send them to me in email.

For easy cut and paste, the article text and other comments are reproduced below.


Perl 5.10


2007/12/29

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..



Comments