Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Conditionally executing different code based on Perl Version

by tobyink (Abbot)
on Feb 21, 2012 at 22:05 UTC ( #955400=note: print w/replies, xml ) Need Help??

in reply to Conditionally executing different code based on Perl Version

The previous answer should work nicely for your particular case. For the more general case,

if ($] >= 5.014) { ... } else { ... }

will often work. However, in some cases you'll have code that while OK in one version of Perl, will result in a compile-time error in another. In those cases, you may need to protect Perl from your code using a stringy eval, or placing the code in external files and conditionally requireing them.

The $] >= 5.014 can probably be optimized by declaring a constant at BEGIN time:

BEGIN { *NEW_PERL = ($] >= 5.014) ? sub(){1} : sub(){0}; } ... if (NEW_PERL) { ... } else { ... }

Replies are listed 'Best First'.
Re^2: Conditionally executing different code based on Perl Version
by dlarochelle (Sexton) on Feb 21, 2012 at 22:29 UTC

    Thanks Eliya and tobyink.

    This is exactly what I was looking for!

Re^2: Conditionally executing different code based on Perl Version
by JavaFan (Canon) on Feb 22, 2012 at 08:42 UTC
    Note that none of your suggestions actually works for the OP. He cannot use use re '/d'; in code that's compiled under 5.12 or earlier, and since its effect is scope based, putting it inside an eval isn't going to help either, as an eval creates its own scope.

    What may work is:

    require re; re->import('/d') if $] >= 5.014;
    but I have not tested this.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://955400]
[Corion]: Hi choroba! I'm somewhat fond of this picture - I think it is Breaking Bad-style even though I haven't watched the series at all ;)
[Corion]: Hmmm - and now that I look at it, the gallery I'm using doesn't produce non-Javascript compatible links in the sense that hotlinking to an image will only work for Javascript enabled...
[Corion]: On the other hand, maybe supporting non-Javascript isn't that much a priority, and I'm not exactly sure how I could make it work for both kinds of browsers without server-side magic

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2017-02-27 08:32 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (377 votes). Check out past polls.