jason.jackal has asked for the wisdom of the Perl Monks concerning the following question:

Folks, I am a little confused why I need to declare the version I am using when my default is a higher version. For example: I am learning from Learning Perl 6thed by Randal L. Schwartz ISBN 9781449303587. On page 14 is shows the 'say' command. I currently have 5.24 ActiveState installed; however, 'say' will not work until I declare "use 5.010". Why is that? Thank you
  • Comment on Why do I need 'use 5.010;' ahen I am running '5.24'

Replies are listed 'Best First'.
Re: Why do I need 'use 5.010;' ahen I am running '5.24'
by choroba (Cardinal) on Jun 30, 2017 at 14:55 UTC
    > 'say' will not work until I declare "use 5.010"

    5.10 is the minimum version you need, but you can as well specify

    use 5.024;

    but it would be impolite to other users of your code who run on 5.10 - 5.22, as they can run say, but wouldn't be able to run your code.

    My recommendation is to always specify the features you use: it works in all the versions, and you don't have to look up what feature was introduced in what version.

    use feature qw{ say };

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Why do I need 'use 5.010;' ahen I am running '5.24'
by marto (Cardinal) on Jun 30, 2017 at 14:40 UTC

    say states:

    "say is available only if the say feature is enabled or if it is prefixed with CORE:: . The say feature is enabled automatically with a use v5.10 (or higher) declaration in the current scope."

    See also feature.

    #!/usr/bin/perl use feature say; say 'derp';

    Update: doh, the above will fail with use strict; so:

    use strict; use warnings; use feature 'say'; say 'Derp';

    You may be interested in Modern::Perl. Thanks choroba.

      That is why I use 5.12, as that implies strict:

      $ perl -we'use 5.10.0; say 1, $x' Name "main::x" used only once: possible typo at -e line 1. Use of uninitialized value $x in say at -e line 1. 1 $ perl -we'use 5.12.0; say 1, $x' Global symbol "$x" requires explicit package name (did you forget to d +eclare "my $x"?) at -e line 1. Execution of -e aborted due to compilation errors.

      Enjoy, Have FUN! H.Merijn
Re: Why do I need 'use 5.010;' ahen I am running '5.24'
by kcott (Archbishop) on Jul 01, 2017 at 05:41 UTC

    G'day jason.jackal,

    If you're using Perl from the command line, you can use the -E switch to get all the features of your current version of Perl. See -E commandline in perlrun.

    I'm using 5.26.0. Here's an example with 'say':

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

    Using a much newer feature (see "perlref: Postfix Dereference Syntax"):

    $ perl -e 'my $x = [qw{a b c}]; print "$x->@*\n"' ARRAY(0x7f82b20040b0)->@* $ perl -E 'my $x = [qw{a b c}]; print "$x->@*\n"' a b c

    Now, if I switch my Perl version to 5.14.0 (long before that feature was available; and no other special significance beyond it being the earliest version I currently have available):

    $ perlbrew switch perl-5.14.0t $ perl -E 'my $x = [qw{a b c}]; print "$x->@*\n"' ARRAY(0x7fe830004ae8)->@*

    Without that feature being available in 5.14.0, '-E' works the same as '-e' in 5.26.0 (with respect to that specific feature). Other features, that were available in 5.14.0, still work as expected:

    $ perlbrew switch perl-5.14.0t $ perl -E 'say "fred"' fred

    — Ken

Re: Why do I need 'use 5.010;' ahen I am running '5.24'
by dsheroh (Monsignor) on Jul 02, 2017 at 09:13 UTC
    To answer the part of your question that hasn't been addressed ("Why do I even need to do this at all? Why doesn't it just use all available features by default?"): Backwards compatibility.

    Imagine, if you will, a Perl programmer working in the Dark Ages of Perl 5.008. Perhaps this programmer was working with, oh, I don't know, speech synthesis, and wrote a sub say which takes a string and sends it to the text-to-speech engine. If we take this perfectly good Perl 5.008 code and run it on 5.24 in the real world, it will probably Just Work™ without modification. In an alternate reality where 5.24 enables all of the 5.10-and-later features by default, however, we run into an immediate conflict between the programmer's sub say and the built-in say keyword, which will almost certainly break something, if it runs at all.

      "To answer the part of your question that hasn't been addressed ("Why do I even need to do this at all? Why doesn't it just use all available features by default?"): Backwards compatibility."

      It has been addressed, it's discussed in feature linked to in first reply, and expanded upon in others.

        Nonetheless, it hadn't been previously addressed in this topic on this site, only in linked documents, where it is not visible to someone reading only this discussion. Please forgive my imprecision in omitting the limiting clause.