Perl Advent Calendar 2007-12-19

What do you give someone with a Birthday in the Holidays?

by Bill Ricker

This is a bit of a departure from normal, to continue the Community's celebration for Perl's 20th Birthday.

Our favorite language turned 20 yesterday. (I hope Perl doesn't go out and get really loaded next year.) So what did the community give to Perl this anniversary?

Perl 5.10? What's that? Is it just 5.8.9, rounding up? No, so much more than a maintenance release. Is that like Perl 6, in time for this Christmas? No, not really, but closer than some might imagine. And whatever it is, it is in time for this Christmas.

First, and best for folks with legacy code (and with CPAN, that's all of us), 5.10 is backwards compatible by default, so can be treated like a maintenance release of 5.8.x if you want to (or need to). All new features are, by default, b, so won't break old code that doesn't import them by name or by announcing 5.10 intended. Like other core module-pragmas, the aptly named feature pragma will bring in only the one's you've asked for (and learned?): use feature qw/say ~~/; But we'll bring them in the easy way in out example.

So What new features are available?

So what will be our first Perl 5.10 program be, Hello World or a JAPH or something showing syntax? TIMTOWTDI, so why choose!

	perl mod19.pl hello world 0 ' ' undef JaPh jApH
	Hello World
	Hello World for the 2th time
	Hello 0 for the 3th time
	Hello 0 for the 4th time
	Hello World for the 5th time
	Hello Just another Perl 5.10 hacker for the 6th time
	Hello just Another perl 5.10 Hacker for the 7th time

mod19.pl

   1 use feature ':5.10'; # or ...
   2 # use 5.10.0; or 5.9.5 or v5.10 or 5.010
   3 
   4 sub hello;
   5 while (@ARGV){
   6  given(shift){
   7    when("hello"){hello; }
   8    when("hello world"){hello; }
   9    when('Hello World'){hello; }
  10    when(q{world}){hello;   }
  11    when(/ \b hello [-\s\W_]?+(?<Name>\w+)/ix){
  12 	hello $+{Name}; }
  13    when(/ \A (?: false | 0{1}+ [.0]* | \s++ | ) \Z /ix){
  14 	hello 0; } # false but defined
  15    when(/ null | undef /ix){hello undef } #  undef
  16    when(/\b hello \b/ix){hello; } #catch others
  17    when(/(?<J>j)(?<A>a)(?<P>p)(?<H>h)/ix){
  18 	hello("$+{J}ust $+{A}nother $+{P}erl 5.10 $+{H}acker");
  19 	}
  20    default { say "what '$_'?";    }
  21  }
  22 }
  23 
  24 sub hello {
  25   my ($msg)=@_;
  26   state $n=0;
  27   $msg //= 'World';      
  28   say "Hello $msg",
  29         $n++ ? " for the ${n}th time" : "";
  30 }

For more information on Perl 5.10 -