Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

use strict without typing use strict?

by nysus (Parson)
on May 26, 2021 at 03:02 UTC ( #11133034=perlquestion: print w/replies, xml ) Need Help??

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

Spent about 15 min googling this but came up empty. I seem to recall that there is a at least one way of calling the “strict” pragma without actually typing in “use strict;”. Is there something similar to the -w option for warnings for the shebang line? I believe there are other ways, too. Can someone please help jog my memory?

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
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
      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.

        I had experimental features in mind when I mentioned "limit functionality". I agree with your observations. My key point is that using a specific version just to enable strict isn't a great idea.

        Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re: use strict without typing use strict?
by choroba (Archbishop) 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]

      Hm, but why does it add "This setting provides a diagnostic mechanism intended for development purposes only "?


      The way forward always starts with a minimal test.
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.
    - Ron
      Since Modern::Perl is written in pure Perl, he could just copy it to his own module M.pm in his path and adjust it to his needs.

      Running perl -MM script.pl would do the trick then.

      edit

      That's not longer than a hypothetical switch -sw which activates s trict and w arnings

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        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.

Re: use strict without typing use strict?
by eyepopslikeamosquito (Bishop) on May 26, 2021 at 08:37 UTC

    The only thing I could see at perlrun was the -M switch. For example:

    > 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.

      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.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

      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...:)

        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?

      If you wanted to use that all the time, then PERL5OPT is one solution:

      $ 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. $

      🦛

        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.

Re: use strict without typing use strict?
by kikuchiyo (Friar) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11133034]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2021-10-18 17:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (74 votes). Check out past polls.

    Notices?