Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: use bytes without breaking perl 5.005 or 5.004?

by lestrrat (Deacon)
on Sep 26, 2003 at 17:12 UTC ( #294501=note: print w/ replies, xml ) Need Help??

in reply to use bytes without breaking perl 5.005 or 5.004?

Matts showed me a clever way to do it the other day. I thought it was beautiful:

$INC{ '' }++ if $] < 5.006

Once that's in %INC, calls to "use bytes" do nothing, apparently

Comment on Re: use bytes without breaking perl 5.005 or 5.004?
Download Code
Replies are listed 'Best First'.
Re^2: use bytes without breaking perl 5.005 or 5.004? (also)
by tye (Sage) on Sep 26, 2003 at 21:10 UTC


    I had previously convinced myself that because BEGIN requires a block, I couldn't fake out 'use warnings' for people who don't have it but then I realized this works fine:

    BEGIN { if( eval { require warnings } ) { warnings->import(); } else { $^W= 1; } }
    because the pragmas already expect to be inside of a (invisible) block and so apply themselves to one block further out.

    I like this method because it allows the user to install a back-ported or or whatever and use it if they want to and I don't have to trust my knowledge of when the module was added. (:

    Anyway, it is another way to do it.

                    - tye
      Thanks Tye! Applying this to "use bytes," am I correct that your solution is basically the same approach as the one I originally suggested? In other words, isn't this...
      BEGIN { if( eval { require bytes } ) { bytes->import(); } }
      ...pretty much the same as this:
      BEGIN { eval {require bytes; import bytes; 1} }
      I'm a bit new to trying to import modules and pragmas outside of the regular "use" function, so I'm just trying to be sure that I understand the difference, if any.

      Thanks! Josh

        Yes, same result. Here is how I tested it:

        > perl { use warnings; } print $x; <EOF> > perl BEGIN { if( {require warnings} ) { warnings->import(); } } print $x; <EOF> Use of uninitialized value in print at - line 3. > perl BEGIN { eval { require warnings; import warnings; } } print $x; <EOF> Use of uninitialized value in print at - line 3. >
        First is the "control" case to show that 'use warnings' inside of a block doesn't have an effect outside of the block. Next is my method, and the warning generated shows that there is an effect (since I have in this version of Perl). Last is your method, which also works.

        The 'hint' pragmas all work very much the same so this trick works for 'strict', 'warnings', 'bytes', etc.

                        - tye
Just so I understand
by joshclark (Novice) on Sep 28, 2003 at 13:54 UTC
    Hey, interesting! So, the idea would be to do it like this?
    BEGIN { $INC{ '' }++ if $] < 5.006 use bytes; }
    This will cause 5.005 and below to ignore "use bytes" and cause 5.6+ to use the bytes pragma as usual? Nifty!

    Thanks for the help, Josh

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2015-12-01 15:15 GMT
Find Nodes?
    Voting Booth?

    My keyboard shows this many letters:

    Results (13 votes), past polls