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

Use of "Use 5.XXX" pragma

by phatrik (Initiate)
on Dec 19, 2012 at 00:02 UTC ( #1009469=perlquestion: print w/ replies, xml ) Need Help??
phatrik has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I've been in a position of Linux sysadmin for the past 4 years and with the exception of very (very!) light bash use (I use "use" as opposed to scripting since most of what I write fits on a single (albeit very long) line. Not to say that it isn't useful\) I don't really do any scripting. I figured it was time to learn, so I picked up "Learning Perl - 6th edition" from my local bookstore yesterday and I have to say it's been a delight so far! Question: My version of perl is 5.14.2. I'm reading about the use of private persistent variables using the "state" keyword. The book introduces those with the "use 5.010;" pragma. If I don't declare "use 5.010;", the state variables doesn't work as expected and I get: Can't call method "state" on an undefined value at ./state_var.pl line 7. Code:

#!/home/erikg/bin/perl #use 5.010; &marine; sub marine { my $counter = 0; while ($counter < 10) { state $n = 0; $n += 1; print "\n$n"; $counter++; } }

But as soon as I remove the comment in front of use, it works fine. Although I understand "use strict;" and "use warning;" are ways of turning on/off strict/warning use, I was under the impression "use 5.XXX;" was a way of verifying the Perl interpreter was at least up to that version or better. I guess not... Question: If I have "use 5.010;" at the beginning of my code, does that mean any features in Perl newer than 5.010 will be unavailable? TIA

Comment on Use of "Use 5.XXX" pragma
Download Code
Re: Use of "Use 5.XXX" pragma
by BrowserUk (Pope) on Dec 19, 2012 at 00:10 UTC
    I was under the impression "use 5.XXX;" was a way of verifying the Perl interpreter was at least up to that version or better. I guess not..

    It does mean that. But, from 5.10 onwards, it also enables a small set of new keywords; including the state keyword. Others are say & switch.

    See the feature pragma for details.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

Re: Use of "Use 5.XXX" pragma
by LanX (Canon) on Dec 19, 2012 at 00:14 UTC
    googling 'use 5.010' produced the docs for use saying

    In the peculiar use VERSION form, ... A fatal error is produced if VERSION is greater than the version of the current Perl interpreter; Perl will not attempt to parse the rest of the file.

    ...

    Also, if the specified perl version is greater than or equal to 5.9.5, use VERSION will also load the feature pragma and enable all features available in the requested version. See feature.

    state is one of these features! (but better avoid it ATM as it has weird side effects)

    Cheers Rolf

Re: Use of "Use 5.XXX" pragma
by Anonymous Monk on Dec 19, 2012 at 00:14 UTC
Re: Use of "Use 5.XXX" pragma
by davido (Archbishop) on Dec 19, 2012 at 00:18 UTC

    It's a little complicated because Perl 5.16 slightly upgraded what the use VERSION pragma does. From the Perl 5.14.2 documentation for use:

    Also, if the specified Perl version is greater than or equal to 5.9.5, use VERSION will also load the feature pragma and enable all features available in the requested version.

    And then in the Perl 5.16 documentation for use:

    use VERSION also enables all features available in the requested version as defined by the feature pragma, disabling any features not in the requested version's feature bundle.

    In the perl5160delta documentation we're told:

    As of this release, version declarations like use v5.16 now disable all features before enabling the new feature bundle.

    So if your Perl interpreter is from the 5.14.x era, you get the older behavior, and if it's from the 5.16.x era or newer (unless it gets changed again) you get the newer behavior. Practically speaking, in either case, use 5.010000;, or use 5.014000;, or use 5.016000; will result in all of the modern features of version 5.10, 5.14, or 5.16 respectively being enabled.


    Dave

      use 5.016; use Try::Tiny; say fc("Hello World"); try { use 5.010; # Now 5.16-specific features are actually disabled! say fc("Hello World"); } catch { my $e = shift; warn "FAILED: $e"; }; say fc("Goodnight!");
      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1009469]
Approved by davido
Front-paged by Tanktalus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (12)
As of 2014-10-01 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (40 votes), past polls