Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am just reading and testing the 5.10 features of perl. Tested "say", "state" but it works only when 'use v5.10' is there in file. Why it is ?

perl version available by default in my machine is

perl -v
This is perl, v5.10.1 (*) built for i486-linux-gnu-thread-multi Copyright 1987-2009, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 +source kit. Complete documentation for Perl, including FAQ lists, should be found +on this system using "man perl" or "perldoc perl". If you have acces +s to the Internet, point your browser at http://www.perl.org/, the Pe +rl Home Page.


without version requirement
perl -e 'say "test"'; String found where operator expected at -e line 1, near "say "test"" (Do you need to predeclare say?) syntax error at -e line 1, near "say "test"" Execution of -e aborted due to compilation errors.


with version requirement it works fine
perl -M5.10.1 -e 'say "test"'; test


Is it my misunderstanding, please explain me. Google search with these words 'say', 'state', are not giving useful results. Thanks for your help.

Replies are listed 'Best First'.
Re: wondering why does 5.10 features are not working
by moritz (Cardinal) on Sep 23, 2011 at 19:31 UTC

    For backward compatibility, the features that involve a new keyword need to be enabled explicitly, either via use 5.010; or use feature ':5.10'; or use feature qw/say switch state/;

    In onliners, using -E instead of -e also enables all new features:

    perl -wE 'say 42'
      This is why I like perl very much ! Instant & good responses !
      Thank you very much !
Re: wondering why does 5.10 features are not working
by BrowserUk (Patriarch) on Sep 23, 2011 at 19:30 UTC

    Use -E instead of -e to enable features on the command line:

    c:\test>perl -E"say $]" 5.010001

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: wondering why does 5.10 features are not working
by ikegami (Patriarch) on Sep 23, 2011 at 19:39 UTC

    use feature qw( state ); is required to use state. This requirement prevents the following perfectly legitimate code from breaking when someone upgrades Perl:

    $ perl -e'sub state { print "I state: @_\n" } state("foo");' I state: foo

    use feature qw( state ); is available via use feature ':5.10';, which is available via use 5.010;.

    Same goes for say.

    The documentation for say and the documentation for state both mention this.