Welcome to WebProNews Breaking eBusiness and Search News
Advertise | Newsletter | Sitemap | News Feeds News Feed 
 WebProNews Search Part of the iEntry network iEntry inc. 

Playing With Perl 5.10

A.P. Lawrence
Expert Author
Published: 2008-01-14

WebProNews RSS Feed


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

*Originally published at APLawrence.com

View All Articles by A.P. Lawrence



Receive Our Daily Email of Breaking eBusiness News


About the Author:
A.P. Lawrence provides SCO Unix and Linux consulting services http://www.pcunix.com

WebProNews RSS Feed

More Expert Articles Articles

Contact WebProNews
Advertisement





TOP NEWS

WebProBlog
The official blog of WebProNews.

Go to WebProBlog

Targeted Information for Business
WebProNews is part of the iEntry network

Internet Business: Marketing: Small Business:
WebProNews MarketingNewz SmallBusinessNewz
WebProWorld AdvertisingDay PromoteNews
EcommNewz SalesNewz EntrepreneurNewz

Software: Search Engines: Web Design:
WebMasterFree Jayde B2B DesignNewz
NetworkingFiles SearchZA FlashNewz
SecurityConfig SearchNewz WebSiteNotes

Developer: IT Management: Security:
DevWebPro ITManagement SecurityProNews
DevNewz SysAdminNews SecurityConfig
TheDevWeb NetworkingFiles NetworkNewz

The iEntry Network consists of over 100 web publications reaching millions of Internet Professionals. Contact us to advertise.
eBUSINESS RESOURCES






 Advertise | Contact Us | Corporate | Newsletter | Sitemap | Submit an Article | News Feeds
 WebProNews is an iEntry, Inc. ® publication - $line) { echo $line ; } ?> All Rights Reserved
WebProWorld
Ten most recent posts.


SearchBrains.com
NetworkingFiles
Featured Software


About WebProNews
WebProNews is the number one source for eBusiness News. Over 5 million eBusiness professionals read WebProNews and other iEntry business and tech publications.

WebProNews provides real-time coverage of internet business.

Free Email Newsletters:
WebProNews SearchNewz
WebProWorld DevWebPro
Marketing SecurityNews
Plus over 100 other newsletters!

Send me relevant info on products and services.


iEntry.com WebProWorld RSS Feed WebProWorld Contact WebProNews Print Version Email a friend Bookmark us SearchBrains.com SearchBrains RSS Feed