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

Basically, I have 50+ pages that all require a file called common.pl (which loads all the modules, does user authentication, etc). The problem is, I have the use warnings, strict, and diagnostic pragmas in common.pl. Is there a way to use pragmas globally (besides copying+pasting them into every file?

Thanks.
  • Comment on How do you get around the lexical scoping of use pragmas?

Replies are listed 'Best First'.
Re: How do you get around the lexical scoping of use pragmas? (import)
by tye (Sage) on Aug 27, 2006 at 05:49 UTC

    Note that use strict boils down to

    BEGIN { require 'strict.pm'; strict->import(); }

    So strict->import() actually applies to the lexical scope one step outside of from where import() was called (or something like that). So, the following actually works:

    package AID; require strict; require warnings; sub import { strict->import(); goto &warnings::import; } 1;

    Then:

    use AID; BEGIN { print 0+""; } print $x;

    Gives:

    Argument "" isn't numeric in addition (+) at - line 2. Global symbol "$x" requires explicit package name at - line 3. Execution of - aborted due to compilation errors.

    And diagnostics.pm isn't lexically scoped (and I'm not sure why people even use it, to be quite frank), so you can just "use diagnostics" inside your common.pm file.

    - tye        

      Brilliant! Makes perfect sense, but I wouldn't have even followed this thought process.

      I'd recommend anyone implementing this should document it heavily!

Re: How do you get around the lexical scoping of use pragmas?
by Joost (Canon) on Aug 26, 2006 at 23:55 UTC
Re: How do you get around the lexical scoping of use pragmas?
by GrandFather (Saint) on Aug 26, 2006 at 23:45 UTC

    Write a script to paste them in for you :)


    DWIM is Perl's answer to Gödel

      Well, a more usual approach would to be to use whatever templating or skeleton facilities your editor provides (something like tempo mode or skeleton mode for Emacsen; I'm sure a vim using heathen will chime in with what they use :) to start off new programs from a base with everything you normally use.

      Even if your editor doesn't provide such a facility natively you can always write your own template once and then copy that into a new file (perhaps even getting fancy and using Template Toolkit or the like to fill in variable parts).

        Indeed. In fact I have used Komodo's template facility to do that in the past, except that it's pretty much as quick to type the two use lines so mostly I don't bother with the template.

        OP's immediate problem seems to be different however - 50+ (presumed existing) files that need to be updated. Sounds like a Perl script to me rather than editor fu.


        DWIM is Perl's answer to Gödel
Re: How do you get around the lexical scoping of use pragmas?
by jdporter (Paladin) on Aug 27, 2006 at 14:01 UTC

    You want the PERL5OPT environment variable. For example:

    # MS-DOS: set PERL5OPT=-Mstrict -Mwarnings

    Not that I recommend this. If you do it at all, it should probably be in some launcher script, rather than at the system-global level.

    We're building the house of the future together.
Re: How do you get around the lexical scoping of use pragmas?
by Copper Maiden (Acolyte) on Aug 27, 2006 at 00:35 UTC
    >_> Not exactly the answer I was looking for, but thanks anyway.

    By the way, could this be considered a bug? It seems like a big, unnecessary inconvenience. Or maybe it's just my irrational hatred of repeating code...

      Not a bug, but a very good case can be made for having strictures enabled by default. One has only to notice that about 30% of the "why doesn't this work" questions in SoPW would have been caught by OP if strictures had been included in OP's code to see that that is a good idea.


      DWIM is Perl's answer to Gödel

        There are a number of things I'd like to see done differently by default in Perl, including the strict pragma being on by default (though I'm not so sure about warnings) — and also default lexical scope so that I don't have to use my() all the time, and so that autovivification would create lexically scoped variables.

        print substr("Just another Perl hacker", 0, -2);
        - apotheon
        CopyWrite Chad Perrin

Re: How do you get around the lexical scoping of use pragmas?
by gmccreight (Sexton) on Aug 30, 2006 at 04:46 UTC
    There is a module called Filter::Macro which can do this type of thing. If you were to use it, the strict and warnings pragmas could easily be loaded into each of the 50+ files.

    There are two downsides, though. The first is that Filter::Macro uses source filtering to accomplish its task. The second is that you'd still have to add one line to each of your 50+ files... the line which "uses" the module which "uses" the Filter::Macro module. From that point on, however, you would be able to simply update that one module where you used Filter::Macro and wouldn't have to touch the 50+ pages again.

    Say you wanted to add Carp to your 50+ files, for example. You would just add it to your module which uses Filter::Macro, and it's basically added to all your 50+ files.

    I've only used Filter::Macro on my test suite files so far, but it appears to work as advertised.