May 2011 Archives

Perl 5.14 is now available. While this latest major release of Perl 5 brings with it many bugfixes, updates to the core libraries, and the usual performance improvements, it also includes a few nice new features.

One such feature is loading IO::File on demand.

Autovivification of filehandles (colloquially known as "lexical filehandles") has been in Perl 5 since the release of Perl 5.6.0:

    open my $fh, '>', $filename
        or die "Cannot write to '$filename': $!\n";

These filehandles behaved something like objects if you loaded IO::File or IO::Handle, in that you could call methods on them:

    use IO::File;
    $fh->autoflush(1);

Even though the Perl 5 core performed the appropriate gyrations to produce these filehandles associated with the proper class, you had to remember to use the appropriate module manually.

Perl 5.14 now requires IO::File if necessary for you. This is a small feature, but it smooths out a confusing wrinkle in an important feature of modern Perl 5.

Perl 5.14 is now available. While this latest major release of Perl 5 brings with it many bugfixes, updates to the core libraries, and the usual performance improvements, it also includes a few nice new features.

One such feature is non-destructive substitution:

    use 5.014;

    my $greeting  = 'Hello, world!';

    # be more elite
    say $greeting =~ tr/aeiou/4310V/r;

    # then run away
    say $greeting =~ s/Hello/Goodbye/r;

The new /r modifier to the substitution and transliteration operators causes Perl to return the modified string, rather than modifying the original string in place. This replaces the idiomatic but unwieldy:

    my  $greeting  = 'Hello, world!';
    my ($leetgreet = $greeting) =~ tr/aeiou/4310V/;

This feature is even more useful for avoiding two common problems with substitutions in map expressions:

    my @modified = map { s/foo/BAR/ } @original;

Not only does the substitution modify the values of @original in place, but the substitution returns a true value if the substitution succeeded and a false value otherwise. While that code looks correct, it's very subtly wrong. The corrected version of this code in Perl 5.12 or earlier is:

    my @modified = map { my $copy = $_; $copy =~ s/foo/BAR/; $copy } @original;

5.14 requires the addition of a single character to produce the intended behavior:

    my @modified = map { s/foo/BAR/r } @original;

See perldoc perlop for documentation of the /r modifier.

Perl 5.14 is now available. While this latest major release of Perl 5 brings with it many bugfixes, updates to the core libraries, and the usual performance improvements, it also includes a few nice new features. This series of articles provides a quick introduction to several of these features.

One such feature is the package BLOCK syntax:

    package My::Class
    {
        ...
    }

When you declare a package, you may now provide a block at the end of the declaration. Within that block, the current namespace will be the provided package name. Outside of that block, the previously effective namespace will be in effect. The block provides normal lexical scoping, so that any lexical variables declared within the block will be visible only inside the block. As well, any lexical pragmas will respect the block's scoping.

You do not need a trailing semicolon after the closing curly brace.

You may combine this with the package VERSION syntax introduced in Perl 5.12:

    package My::Class v2011.05.16
    {
        ...
    }

The VERSION must be an integer, a real number (with a single decimal), or a dotted-decimal v-string as shown in the previous example. When present, the VERSION declaration sets the package-scoped $VERSION variable within the given namespace to the provided value.

perldoc -f package documents this syntax.

Visit the home of the Perl programming language: Perl.org

Sponsored by

Monthly Archives

Powered by Movable Type 5.13-en