Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

use _specific_ version

by particle (Vicar)
on Jul 09, 2001 at 19:13 UTC ( #94997=perlquestion: print w/replies, xml ) Need Help??

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

i'd like to be able to use a specific version of perl to run a perl script. i've been asked to ensure my code is compatible with the company standard perl distributions (5.004 and 5.6.0.) also, i've been asked if there is a way to enforce this compatibility at runtime. i'd like to be able to do something like this at the top of my scripts~
use v5.6.0; no use v5.6.1;
which would say 'use 5.6.0 or higher, but don't use 5.6.1 or higher. but i'm not sure this will work, or if it's clear. i could code something like~
die( printf("must use perl version %vd\n", $^V) ) unless $^V eq 5.6.0; # only use company standard perl 5.6.0
which checks the version of perl and dies otherwise, but then i don't know if $^V will work correctly in earlier versions of perl, or if i should use $] as in $] eq 5.006. in this case, i don't know how 5.6.1 displays in $].

i have 5.6.0 installed on Win2000, and i can't install any other versions to test. go figure.

~Particle

Replies are listed 'Best First'.
Re: use _specific_ version
by bikeNomad (Priest) on Jul 09, 2001 at 19:30 UTC
    I think the following will work with any version of Perl 5:
    BEGIN { require 5.004; die "Requires only 5.004" if int($] * 1000) > 5004; }

    update: why bother with the require, either? Just use the die and !=

      yes. this makes sense to me. is there good reason to use require inside 'BEGIN', or is use okay as well? i'm not sure of the subtle differences.

      ~Particle

        I believe that in this case, the import is skipped, so: use 5.004; is the same as BEGIN { require 5.004 }

        You want to check inside a begin block, anyway, so you do as little as possible with the wrong Perl.

        Note also that the version number could have more information; 5.6.1 is encoded as 5.006001 in $]. If you care about 5.6.0 vs 5.6.1, you need to multiply by 1000000 first and compare with the right number.

Re: use _specific_ version
by arturo (Vicar) on Jul 09, 2001 at 20:02 UTC

    Here's an idea (dunno how portable she be):

    BEGIN { die "You must use 5.004 or 5.6.0 to run this script.\n" unless ( ($] >= 5.004 && $] <= 5.005) || ($] >= 5.006 && $] < 5.006001) ); }

    I think that'll work, anyway =)

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
(jptxs)Re: use _specific_ version
by jptxs (Curate) on Jul 09, 2001 at 20:49 UTC
    I'm assuming the two installs are in distinct directories. If so, why not just:
    #!/path/to/right/perl BEGIN { @INC=/the/path/to/your/mods }

    wouldn't that do the trick?

    thanks arturo =)

    We speak the way we breathe. --Fugazi

      HAHA! caught in your own web of assumptions! ;)

      my client has propriatary technology on Windows which allows applications to run in their own environment space, and without a hard-coded location. for example, alice could have perl 5.004 and 5.6.0 installed on the same machine, and run both versions at the same time without @INC getting confused. also, bob could run 5.6.0 from j:\, while charlie could run from c:\ or from k:\apps, and the scripts would all execute correctly.

      so, the path to perl is not the same for all users. it can be found with environment variables, but 1) that won't work on the #! line, and 2) windows doesn't really care anyway.

      on unix, yours is generally the method i use. but these scripts are Win32 based, and i believe others have provided me with the portable code i need, and will include everywhere.

      ~Particle

        I don't see anybody who has coded what you want. Several have given you code to check which version was called, but none to pull the version of Perl that you wanted.

        If this is just for testing, then I would recommend a wrapper script that explicitly calls the wanted version of Perl. Call the wrapper script and it should just work.

        (Don't forget that the wrapper may need to set environment variables, etc for your client's environment.)

Re: use Ispecific/I version
by Malkavian (Friar) on Jul 09, 2001 at 19:19 UTC
    I believe you're looking for 'require'.
    At the top of your code put a line 'require 5.006;' or similar.
    As far as I'm aware, it just forces the compliance with a particular, known and tested version of Perl.
    Is it a requisite that it needs to check for more than one version of Perl, or just require the one?
    You could use the $] (perl version variable) as a trap, but, on the whole, I think requiring a particular version is cleaner.

    Malk
      no, no.

      require and use both specify this version or higher. the difference between require and use is that require is executed at runtime, and use is executed at compile time. therefore, if i require 5.6.0, any BEGIN blocks will execute before the version of perl is checked.

      that would be a Bad Thing.

      ~Particle

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2022-05-18 23:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (71 votes). Check out past polls.

    Notices?