Re: use strict without typing use strict?
by GrandFather (Saint) on May 26, 2021 at 04:18 UTC
|
Just "use strict;" and move on. You can turn on strict with use 5.12.0; or any explicit later version, but that's not a great way of just turning on strict because you may get unexpected side effects or limit functionality when using newer version of Perl.
There was a suggestion that it would be the default for Perl 7, but that is now off the table due to potential for breakage of legacy code.
Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
| [reply] [d/l] [select] |
|
You can turn on strict with use 5.12.0; or any explicit later version, but that's not a great way of just turning on strict because you may get unexpected side effects or limit functionality when using newer version of Perl.
I recommend to add a use $VERSION; anyway, but not 5.12.0 unless you've actually tested your code under that version. It doesn't limit functionality: If you need functionality from a newer version of Perl, you're well advised to adjust to that version in the use statement.
Unexpected side effects can occur if you add a version declaration to legacy code which hadn't one, but if you start writing your code with a version declaration, you can look it up what side effects are to expected from bumping that version.
| [reply] |
|
| [reply] |
Re: use strict without typing use strict?
by choroba (Cardinal) on May 26, 2021 at 10:26 UTC
|
Compile 5.34+ with -Dusedefaultstrict and you'll get strict everytime when running Perl (except for -e and -E oneliners). See perl5340delta.
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
| [reply] [d/l] [select] |
|
| [reply] |
|
| [reply] |
Re: use strict without typing use strict?
by The Perlman (Scribe) on May 26, 2021 at 05:41 UTC
|
Something like Modern::Perl activates all your boilerplate at once.
| [reply] |
|
| [reply] [d/l] [select] |
|
Ha ha, I see Modern::Perl contains a "odern" (sic) directory, in addition to a Modern directory,
presumably to enable this hack to work with only one M:
perl -Modern::Perl script.pl
As an aside for CPAN authors, note that Release::Checklist cautions
against adding developer convenience modules, such as Modern::Perl, as a dependency.
| [reply] [d/l] [select] |
|
|
Re: use strict without typing use strict?
by eyepopslikeamosquito (Archbishop) on May 26, 2021 at 08:37 UTC
|
> perl -c -Mstrict -e "my $fred = 42"
-e syntax OK
> perl -c -Mstrict -e "$fred = 42"
Global symbol "$fred" requires explicit package name (did you forget t
+o declare "my $fred"?) at -e line 1.
-e had compilation errors.
| [reply] [d/l] [select] |
|
Well in theory he could set the environment with PERL5OPT=-Mstrict or manipulate sitecustomize.pl (see perlrun for details) ...
D:\tmp>set PERL5OPT=-Mstrict
D:\tmp>perl -ce "$fred = 42"
Global symbol "$fred" requires explicit package name (did you forget t
+o declare "my $fred"?) at -e line 1.
-e had compilation errors.
... but I would hate the side effect of random errors, if the script was accidentally run in another environment and strict was missing.
The safest would be to alias/link perl to a new name like superl - the "super language" ;) - which auto-defaults to strict.
Like this superl script.pl would simply not run if the workaround was missing.
PS: On a side-note: The whole Perl7 mess could be solved like this too! (The language needs a rebranding anyway to overcome the mobbing. Don't shoot the messenger...:) | [reply] [d/l] [select] |
|
Good catch. Totally forgot about PERL5OPT. :)
Just in case this is an XY Problem, I'd be interested to learn the underlying real reason why the OP wants to do this.
Just for personal convenience? Or is there an organisational back story, perhaps trying to control what others do?
| [reply] [d/l] |
|
$ export PERL5OPT='-Mstrict'
$ perl -e '$x = 1; print qq{$x\n}'
Global symbol "$x" requires explicit package name at -e line 1.
Global symbol "$x" requires explicit package name at -e line 1.
Execution of -e aborted due to compilation errors.
$
| [reply] [d/l] |
|
One caveat to keep in the back of your mind if you dabble with PERL5OPT: every arbitrary perl you may run will see that and (try to) honor it. At $work we've got some stuff therein that works fine when run with our separate application copy of a newer version, but woe be unto the person that manages to mungle their PATH and instead wind up getting the (long in the tooth) OS' /usr/bin/perl instead because there's modules pulled in which have a newer use 5.xx constraint and there's much errors and blowing up and the not running (GLEIVEN!</bad professor frink>).
Probably not an issue WRT just a PERL5OPT="-Mstrict" but be aware of that limitation if you start getting fancier.
The cake is a lie.
The cake is a lie.
The cake is a lie.
| [reply] [d/l] [select] |
Re: use strict without typing use strict?
by kikuchiyo (Hermit) on May 28, 2021 at 20:16 UTC
|
For completeness sake:
You could do BEGIN{$^H|=0x6e2}, (something close to) that's what strict.pm is doing internally.
But don't do it, because this is unreadable and not portable across perl versions.
| [reply] [d/l] |