in reply to Re^2: Need script help
in thread Need script help

Mostly good advice, but I would advise STRONGLY against putting anything in PERL5OPT. It makes your scripts non-portable.

This is plain rubbish. The PERL5OPT environment variable doesn't have anything to do with portability, but with the current (application dependant, personal, temporal) options for perl, which might also be provided on the command line.

If you set up your PERL5OPT, you should know what's in there. If you distribute a suite of perl scripts relying on some value in PERL5OPT, you should inform your audience/customers about its rationale and sensible values. PERL5OPT is an environment variable, same as PERLINC (the latter having a default value). They only have effect if a) perl b) your scripts are already ported/portable.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Replies are listed 'Best First'.
Re^4: Need script help [aliases and function]
by kcott (Archbishop) on Sep 25, 2016 at 09:14 UTC

    G'day shmem,

    ++ That goes some way to corroborating my «I don't think it would actually make any of my scripts "non-portable"».

    Tired of continually having to add ' -E to multiple perlo calls when testing, I wrote the alias [perle = perl + options and execute] to do it for me.

    There are certain local usages of Perl where I don't want these pragmata: when using B::Deparse being a prime example. In these cases, having PERL5OPT set would be less than optimal. As these have, more or less, boilerplate command lines, I wrote the aliases [perlb = perl + B::Deparse] (which includes the '-p' option, to print extra parentheses — useful when checking complex predence rules) and [perlr = perl + B::Deparse raw] (which includes no options — useful when seeing how the code is parsed without the distraction of a forest of parentheses).

    My .bash_aliases (sourced by .bash_profile) now includes:

    alias perlo='perl -Mstrict -Mwarnings -Mautodie=:all' alias perle='perl -Mstrict -Mwarnings -Mautodie=:all -E' alias perlb='perl -MO=Deparse,-p -e' alias perlr='perl -MO=Deparse -e'

    Now I can write, for example:

    $ perlo -E 'my $x; $x = 41; ++$x; say $x' 42 $ perle 'my $x; $x = 41; ++$x; say $x' 42 $ perlb 'my $x; $x = 41; ++$x; say $x' my($x); ($x = 41); (++$x); $x->say; -e syntax OK $ perlr 'my $x; $x = 41; ++$x; say $x' my $x; $x = 41; ++$x; $x->say; -e syntax OK

    I also considered other common usages that might have boilerplate command lines. I often use Perl as a handy calculator (e.g. perl -E 'say($calculation_result)'). I wrote the function [perls = perl + say()].

    My .bash_functions (also sourced by .bash_profile) now includes:

    perls () { eval "echo \`perl -T -Mstrict -Mwarnings -Mautodie=:all -E ' my \$ev = eval \"$@\"; say defined \$ev ? \"\$ev\" : \"$@\"; '\`" }

    Now I can write, for example:

    $ perls 1 + 1 2 $ perls 67 / 33 2.03030303030303 $ perls \(67 / 33\) + 1 3.03030303030303 $ perls '(67 / 33) + 1' 3.03030303030303 $ perls 'my \\\$x; \\\$x = 41; ++\\\$x' 42 $ perls `perle 'my $x; $x = 41; ++$x; say $x'` 42 $ perle 'my $x; $x = 41; ++$x; say $x' 42 $ perls Hello, world! Hello, world!

    Setting PERL5OPT globally is not useful for me; however, I don't discount its use in limited scope.

    — Ken