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

is there a way to make "use strict" optional?

i've got users who are - er - untidy who don't want the restriction of use strict. i'd like to beat them over the head, but they'd like a flag to turn it off. something like:

script.pl -nostrict inputfile

where inputfile has some perlcode to be evaluated by script.pl. i've tried:

unless($nostrict) { use strict }

which compiles, but has no effect, no matter the value of $nostrict

ideas? t.i.a.

sj

Replies are listed 'Best First'.
Re: make "use strict" optional?
by Zaxo (Archbishop) on Sep 28, 2004 at 23:18 UTC

    Not that I like the idea, but if.pm can do this. use if (not grep {/--nostrict/} @ARGV), 'strict'; if.pm ships with recent perl, but works with long-gone ones too, where it can be installed from CPAN.

    Added - You should use Getopt::Long or something to remove that flag from @ARGV before you start processing input files. With a $nostrict flag set by that, your users can take advantage of all that freedom with things like,

    goto I_LIKE_MY_VARIABLES_STEALTHED_AND_GLOBAL if $nostrict; my ($foo, @bar, %baz); I_LIKE_MY_VARIABLES_STEALTHED_AND_GLOBAL:
    I'm a little puzzled, btw. What sort of perl programs are you providing your users that they have to care about strict or even know about it?

    After Compline,
    Zaxo

Re: make "use strict" optional?
by diotalevi (Canon) on Sep 28, 2004 at 23:15 UTC

    You can say no strict; to turn it off for the rest of the lexical scope that it will be in. I imagine you only want to use no strict 'vars'; since the other two uses are even less likely to be what your knuckle dragging users want.

    Of course, I would tend to think that anyone who actually wants to turn off strict in this manner is obviously unqualified for the job and shouldn't be allowed to do it.

      Or you could point them at perldoc perl and tell them if they're not versed enough to figure out how to turn off strict themselves they shouldn't be trying to do so. :)

Re: make "use strict" optional?
by eyepopslikeamosquito (Archbishop) on Sep 28, 2004 at 23:59 UTC

    Why not follow the lead of the master and simply comment out that annoyin' use strict line? -- while carefully noting the reason for the change ("removed stupid use strict") in the change log file. ;-)

    Seriously, unless their Perl skills are close to TheDamian I would start by trying to persuade them politely and logically that this is a bad idea by pointing them to the many available sources describing why use strict should generally be employed. And if that didn't work, proceed to beat them over the head with a stick.

      I'd just like to say, I like your answer. I have taken a few classes with Damian Conway, and he does recomend the use strict pragma. I'd also like to agree with diotalevi, in saying that anyone who cannot code to this standard, should not be coding Perl.

      May the Force be with you
      The change log file your link points to just states that 'use strict' was removed - it doesn't list a reason at all. Or is your point that it was removed for no reason?

        Perl6::Rules is not your run-of-the-mill CPAN module and is not recommended for production use; it's an exploratory module, right at the leading edge, pushing Perl 5 beyond its capabilities. This module is used mainly by Perl 6 language designers to gain some real-world experience with the new Perl 6 Rules in order to improve and debug their specification. I assume use strict was turned off because this module, in pushing Perl 5 way beyond its normal boundaries, uncovered some bugs or limitations with use strict.

        It is so well-known in the Perl community that only Neanderthals (note to DrHyde: davorg's description, not mine) make comments like use strict is stupid or even, gasp, use strict is gay, that there are at least two running jokes on this theme, namely use strict is gay and use strict with pride. Knowing TheDamian's sense of humour (he has the dubious distinction of being the inventor of the Acme namespace), I'm sure he was just making a joke when he put in the change log "removed stupid use strict ;-)".

Re: make "use strict" optional?
by davorg (Chancellor) on Sep 29, 2004 at 08:11 UTC

    Get yourself some better programmers. There are plenty of talented Perl programmers out there looking for work. Neanderthals like you're dealing with shouldn't be taking their jobs.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Call for Mr. Davorg on line 1, from the Neanderthal Anti-Defamation League.
Re: make "use strict" optional?
by CountZero (Bishop) on Sep 29, 2004 at 06:05 UTC
    eval-ing code written by users who cannot code without breaking strict seems IMHO to be a Generally Bad Idea™.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law