Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Journal of markjugg (792)

Monday August 21, 2006
09:34 PM

refactoring CGI::App for Perl6: less syntax is a theme

[ #30714 ]
One of the themes I've noticed moving from Perl5 to Perl6 is that less syntax is required. While I still need to get the tests to pass, CGI::App is now valid Perl6 syntax. Using "sloccount" on both versions, the Perl6 version has about 4% fewer lines in it. (A drop from 1510 lines to 1450).

Considering Perl5 had fairly efficient syntax already, each little simplification is refreshingly welcome!

Here are some things I noticed that have been removed or simplified:

  • 1; at the end of a file
  • use strict; (on by default)
  • our $VERSION = 1.23 (part of module declaration now).
  • declaring new()is not needed for simple objects. (See The Objects Synopsis)
  • Simple object methods are automatic.
  • my $self = shift; is gone -- my favorite!
  • $self->{_FOO} is just $!foo.
  • my is no longer necessary in a for loop. Instead the syntax is: for @array -> $i {

I'm sure there are more shortcuts to be discovered.

I'm also starting to refactor Perl5 idioms into more pleasant Perl6 idioms. Here are two snippets, both valid Perl6, but the second takes advantage of two new features: qw// has been replaced with < >, and any() and all() have added to the core, working like Quatum::Superpositions in Perl5, which I've always been superstitious about using in production.

Perl5ish code in Perl6

my %allowed_header_types = (
     header   => 1,
     redirect => 1,
     none     => 1,
);

print "Invalid header_type '$header_type'" if (not %allowed_header_types<$header_type>);

Refactored to use Perl6 quoting and any()

my @allowed_header_types = <header redirect none>;
print "Invalid header_type '$header_type'" if not $header_type eq any(@allowed_header_types);

My experience with Perl6 thus far has been the sum of a details like this that add up to a more pleasant syntax.

Thanks to TreyHarris and wolverian for post-launch feedback on this post.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.