perl 6 say function on perl 5

Tuesday, December 09 2008 @ 06:30 PM GMT

Contributed by: robin

Perl6 has a few "nice" features that just make life that little bit easier. One of them is the "say" function - basically, "print" with a newline.

However, this is also available in perl 5.10 with "use feature 'say'" and in perl 5.8 with "use Perl6::Say".

Here's how to load the right version depending on what version of perl your code is running on:

require 5.008;
BEGIN {
    if ($] <= 5.009) {
        use Perl6::Say;
    } else {
        if ( $] <= 6.000 ) {
            use feature 'say';
        }
    }
}

Note, this won't work on perl 5.8 if the Perl6::Say module is not installed so if you're looking for maximum portability (i.e. only core modules) then rolling your own subroutine is probably the best idea:


BEGIN {
    my $nl = qq(\n);
    *say = sub { print @_, $nl } ;
}

Comments (0)


robinbowes.com
http://robinbowes.com/article.php/20081209183046977