http://www.perlmonks.org?node_id=958505

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

I have learnt that in order to use featrures like the statement "say" or the "switch" style programming structure, I have to initiate all my Perl programs with the following declaration:

use feature ":5.10";

Is there some sort of instructing Perl to take that as a default. I mean, if I want, for instance, to use those features from the command line, say, to use

$ ls -l | perl -ane 'say "@F[5..7]"'

instead of the more verbose

$ ls -l | perl -ane 'print "@F[5..7]\n"'

Do you have any comments on this?
Thanks!
--Sergio.

Replies are listed 'Best First'.
Re: Permanent <<use feature ":5.10";>>
by mirod (Canon) on Mar 08, 2012 at 18:01 UTC

    Use -E instead of -e:

    ls -l | perl -anE 'say "@F[5..7]"'

    From man perlrun:

    -E commandline
                behaves just like -e, except that it implicitly enables all optional features (in the main compilation unit). See feature.
    
Re: Permanent <<use feature ":5.10";>>
by toolic (Bishop) on Mar 08, 2012 at 18:28 UTC
    Another possibility is to set the PERL5OPT environment variable:
    $ setenv PERL5OPT -Mfeature=:5.10 $ perl -e 'say 5' 5
Re: Permanent <<use feature ":5.10";>>
by JavaFan (Canon) on Mar 08, 2012 at 18:03 UTC
    For command line perl, just replace -e with -E.

    For programs, just start your programs with use 5.010; (or some higher number). Declaring a minimum version is a good idea anyway. If you find that too much typing, spend a few minutes learning how to add a macro in your editor.

Re: Permanent <<use feature ":5.10";>>
by CountZero (Bishop) on Mar 08, 2012 at 19:46 UTC
    I do a use Modern::Perl at the start of my programs. That gets me all the new features, strictures and warnings as well and 'C3' method resolution order.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Permanent <<use feature ":5.10";>>
by kcott (Archbishop) on Mar 08, 2012 at 19:58 UTC

    Using Modern::Perl from the commandline is probably just as much typing but, as it provides quite a few other items (strict, warnings, etc.), you might find it's worth it.

    Simply to get access to say(), I'd just use -E as mirod suggested (above).

    -- Ken

Re: Permanent <<use feature ":5.10";>>
by LanX (Saint) on Mar 08, 2012 at 17:55 UTC

      Ok, but what should be the name of the rc-file and where is to be located?

        Finally I found what I had in mind, see $Config{sitelib}/sitecustomize.pl in perlrun

        But contrary to toolic's PERL5OPT it's site-wide, so better think twice about what could be affected.

        Cheers Rolf

        I said IIRC and your too fast! :)

        Couldn't find it seems I was confusing with the debugger or perl6 feature. Sorry!

        Anyway for the general problem it should be possible to alias perl to a command which loads a module first which does whatever settings you want.

        For your special problem the mentioned -E is good enough.

        Cheers Rolf

Re: Permanent <<use feature ":5.10";>>
by ForgotPasswordAgain (Priest) on Mar 09, 2012 at 00:22 UTC
    Probably won't be a popular comment, but you asked for any comments. Mine: don't use 'say' (or 'switch'). There, I sayed it. (How to justify it... Time saved? You spent some amount of time puzzling over it, then posted to perlmonks asking how to do it. A lot of time there. Or maybe it's clearer? 'say', a new feature, works now because....somehow it's been enabled. Well, if you set some environment variable or whatever, which won't work elsewhere. Seems not really so clear. Easier to wait a few years before using new syntax of dubious value, IMHO. Just because UNIVERSAL::can($you, 'do_something') doesn't mean UNIVERSAL::should($you, 'do_something') :))

      Although I don't agree with you, 5.10 has been out for long enough to be pretty much everywhere, in this case it's fairly easy to emulate say using -l:

      ls -l | perl -l -ane 'print "@F5..7"'