Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Off by one key... or yet another example of why strict and warnings are always a good idea (even for "one-liners")

by LanX (Archbishop)
on Jul 18, 2019 at 00:53 UTC ( #11102961=note: print w/replies, xml ) Need Help??


in reply to Off by one key... or yet another example of why strict and warnings are always a good idea (even for "one-liners")

Good story, we've all been thru this once. =)

Just a hint

> (And yes, I do use the the duo on most every script I write-just not always on "one-liners".)

well you could set up an alias psw=perl -Mstrict -Mwarnings if you think that you'd really go to declare variables.

IIRC there was also a possibility to globally require a a piece of Perl code whenever your installation runs.

update

> where "-Mstrict -Mwarnings" would have added a mere 19 characters in length

or see perlrun#PERL5OPT: "Switches in this variable are treated as if they were on every Perl command line."

C:\>set PERL5OPT=-w -Mstrict C:\>perl -e"print $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.

though you might prefer to avoid strict vars in one-liners

C:\>set PERL5OPT=-w -Mstrict=refs,subs C:\>perl -e"print $x" Name "main::x" used only once: possible typo at -e line 1. Use of uninitialized value $x in print at -e line 1.

comming back to your original case

C:\>perl -le "my $str = q{zxcv}; if ( $str -~ m/some pattern/i ) { pri +nt $str; }" Use of uninitialized value $_ in pattern match (m//) at -e line 1. Argument "zxcv" isn't numeric in subtraction (-) at -e line 1. zxcv

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

  • Comment on Re: Off by one key... or yet another example of why strict and warnings are always a good idea (even for "one-liners")
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Off by one key... or yet another example of why strict and warnings are always a good idea (even for "one-liners")
by Eily (Monsignor) on Jul 18, 2019 at 13:20 UTC

    though you might prefer to avoid strict vars in one-liners
    I like to pretend that I don't use strict and warnings on oneliner because it lets me use the full power of unconstrained perl, but trully 95% of the time I just want to be able to use variables without declaring them (4.999% are for being able to leave a word unquoted. The rest is for shooting myself in the foot)

    But this is where Windows is clearly superior to Linux, because you can use ' to be able to use undeclared variables where Linux would require the twice boringer ::

    perl -Mstrict -E "$'x = 2; say $'x" 2
    Or if you're a true smartass, you can just limit yourself to using only _, a and b as variable names :P

      > Windows is clearly superior to Linux,

      You mean because on Windows you need to put the code in double quotes which don't collide with single quotes?

      The best thing would be a Perl shell without the need to quote at all and calling OS commands inside out.

      update

      the second best thing is perl <RET>

      Perl reads from STDIN then, Ctrl-d or __END__ ends it. Works on both plattforms.

      C:\>perl -w print $x ^D Name "main::x" used only once: possible typo at - line 2. Use of uninitialized value $x in print at - line 1. C:\>

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      > this is where Windows is clearly superior to Linux

      Let me correct you. I'm getting 2 as the output on Linux, too. That's because `$'` is not a valid variable name in the shell (at least in bash which I use).

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11102961]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2019-12-11 10:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?