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

Brothers, I come to you this day with a question I thought I would never ask... How might I make the usage of strictures optional in my code, at Perl's discretion?

The situation is that, yesterday, I distributed some code to a host of hosts, set to run unattended each night. One of these hosts appears to be exceedingly ancient, as it failed to run my code, crying out Can't locate strict.pm in @INC (@INC contains: /opt/perl5/lib/5.00502/PA-RISC1.1 /opt/perl5/lib/5.00502 /opt/perl5/lib/site_perl/5.005/PA-RISC1.1 /opt/perl5/lib/site_perl/5.005 .) before dying.

How might I alter my customary use strict such that this ancient host will withstand its presence? For it seems the alternatives would be to either discard it entirely or attempt to maintain separate versions of all code for that host and I fear the dragons that lie in wait along either path.

Update: Thanks, everyone, for both the suggestions that I try to get this borked Perl install fixed and for the code to work around this particular (aspect of the) problem.

Replies are listed 'Best First'.
Re: Making strict optional
by davorg (Chancellor) on May 31, 2006 at 15:02 UTC

    You're asking the wrong question. strict.pm was included in Perl 5.005. If your installation doesn't have it then your installation is badly broken and can't be trusted. I recommend getting Perl reinstalled on that server as a matter of some urgency. You shouldn't be trying to work around its problems.

    If you can't get Perl reinstalled then I recommend getting a new hosting company, sysadmin or job as appropriate.

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

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

      I'm currently on contract with the sysadmin group at this random large corporation, so I suppose I'm in as good of a position as anyone to try to get it fixed. I expect the attempt to fail, thanks to their paranoid change management policies, but, if it does fail, my contract runs out in a month, so I'll be getting a new job anyhow.
Re: Making strict optional
by gellyfish (Monsignor) on May 31, 2006 at 15:04 UTC

    Well perl 5.00502 as that message indicates the version in question is certainly did have strict I have a horrible feeling that you are going to find something even worse wrong with the perl installation even if you fix that, I suspect that they probably have just copied the perl binary without the libraries or just a few of them.

    Of course you could do something like:

    BEGIN { eval { require strict; }; strict->import() unless $@; }
    but I think you will find that you end up playing whack-a-mole with stuff like that until the perl installation is repaired.

    /J\

      Your suspicion looks quite likely to be correct... After discovering that the host in question is also missing Sys::Hostname, I took a look at @INC and none of the directories it references (except '.', of course) even exist.

      sigh.

Re: Making strict optional
by Zaxo (Archbishop) on May 31, 2006 at 15:08 UTC

    That's a very broken installation of perl. Perl 5.5 should have strict.pm installed with it. That's a fault the admin should correct.

    Failing administrative sympathy, you can move a copy of strict.pm into your own space and tell perl to look there with the PERL5LIB environment variable.

    After Compline,
    Zaxo

Re: Making strict optional
by dave_the_m (Monsignor) on May 31, 2006 at 15:05 UTC
    Note that strict has been part of perl since 5.000. It looks like your HPUX 5.00502 installation is missing some stuff, so strict may be the least of your problems.

    Dave.

      Just out of curiousity, what gave it away as HPUX? Are they the only ones who put it in /opt/.../PA-RISC?
        The most common OS that runs on PA-RISC is HP-UX. See the Wikipedia PA-Risc family entry for more.
Re: Making strict optional
by Herkum (Parson) on May 31, 2006 at 15:03 UTC
    Here you go,
    eval 'use strict'; if ($@) { # If it cannot 'use strict' then it will put the error in $ +@ print "Cannot 'use strict', it appears to be missing\n"; }
    Put this in your code, and it will print a message if it cannot load strict for whatever reason. You can take out the message if you want.

    Update: Put the {} for the eval command in the wrong place... Just taking them out altogether and removing it from the BEGIN block. At least I learned something today.

      While that trick can be used for detecting if strict exists, it won't turn strict on.

      strict ends here | | BEGIN { v eval 'use strict'; warn("Cannot 'use strict', it appears to be missing\n") if $@; }

      On the other hand, the following works (and doesn't use eval EXPR):

      BEGIN { eval { require strict }; if ($@) { warn("Cannot 'use strict', it appears to be missing\n"); } else { strict->import(); } }

      Seeing how strict is lexically scoped using it inside of an eval inside of a BEGIN block's scope isn't really going to do the rest of one's code much good . . .

        Well you might say that:

        BEGIN { eval { require strict; }; strict->import() unless $@; } + $foo = 1;
        Gives rise to:
        Global symbol "$foo" requires explicit package name at hdhd line 10. Execution of hdhd aborted due to compilation errors.
        So no it does work. Update: but of course the string eval is different.

        /J\

      The problem with that is that the following script compiles without complaints (even when strict is available) and prints "5".
      sub BEGIN { { eval 'use strict' } } $not_declared = 5; print $not_declared, "\n";
      "use strict" is always local for the surrounding block, so you cannot put it into eval or BEGIN.
A reply falls below the community's threshold of quality. You may see it by logging in.