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

Re: Conditionally executing different code based on Perl Version

by JavaFan (Canon)
on Feb 22, 2012 at 08:37 UTC ( [id://955473]=note: print w/replies, xml ) Need Help??


in reply to Conditionally executing different code based on Perl Version

I'm trying to upgrade some code to run correctly in Perl 5.14. However, I still need it to work in older version of perl.

I would like to have certain code execute only if Perl 5.14 is detected. Essentially I'm looking for something similar to the #if MACRO #endif construct in C.

My specific use case is that I need use re '/d/'; in order for some existing regular expressions to want with 5.14 but earlier Perl versions will give a compile error if I add this pragma.

Considering that /d is the default, why even bother? If you aren't using use re '/l'; or use re '/u'; in an outer scope, there's no need to do use re '/d'; in an inner. Quoting from perlre:
If none of the above apply, for backwards compatibility reasons, the "/d" modifier is the one in effect by default. As this can lead to unexpected results, it is best to specify which other rule set should be used.

Replies are listed 'Best First'.
Re^2: Conditionally executing different code based on Perl Version
by dlarochelle (Sexton) on Mar 08, 2012 at 16:32 UTC
    Considering that /d is the default, why even bother? If you aren't using use re '/l'; or use re '/u'; in an outer scope, there's no need to do use re '/d'; in an inner. Quoting from perlre:
    If none of the above apply, for backwards compatibility reasons, the "/d" modifier is the one in effect by default. As this can lead to unexpected results, it is best to specify which other rule set should be used.
    Good question. It turns out that the default is different if the code has 'use locale' and this code did:

    The use re '/foo' pragma can be used to set default modifiers (including these) for regular expressions compiled within its scope. This pragma has precedence over the other pragmas listed below that change the defaults.

    Otherwise, use locale sets the default modifier to /l ; and use feature 'unicode_strings or use VERSION (or higher) set the default to /u when not in the same scope as either use locale or use bytes. Unlike the mechanisms mentioned above, these affect operations besides regular expressions pattern matching, and so give more consistent results with other operators, including using \U , \l , etc. in substitution replacements.

    If none of the above apply, for backwards compatibility reasons, the /d modifier is the one in effect by default. As this can lead to unexpected results, it is best to specify which other rule set should be used.

    So for code with 'use locale' the default regex behavior changed from 5.12 to 5.14.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-03-19 02:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found