Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

use bytes without breaking perl 5.005 or 5.004?

by joshclark (Novice)
on Sep 26, 2003 at 16:55 UTC ( #294493=perlquestion: print w/replies, xml ) Need Help??
joshclark has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,

I'm writing a routine that needs to deal with bytes even when the string contains multibyte characters. "use bytes" of course does the trick but would break on pre-5.6 perl (alas, I need to be able to have this run on 5.004+). So, I need to ask the compiler to use the bytes pragma only if it exists...

I *think* this will do the trick but wanted to confirm with those who are wiser than I that I'm not missing anything important here:

BEGIN { eval {require bytes; import bytes; 1} }

Thanks for any advice!

Replies are listed 'Best First'.
Re: use bytes without breaking perl 5.005 or 5.004?
by lestrrat (Deacon) on Sep 26, 2003 at 17:12 UTC

    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


      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
      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
Re: use bytes without breaking perl 5.005 or 5.004?
by bart (Canon) on Sep 27, 2003 at 00:57 UTC
    You can make a copy of the string that is marked as a byte string, even if the original string is UTF8, by doing this:
    $bytes = pack "C0a*", $bytes_or_utf8;
    That should work on pre-5.6 perls too — even though it won't do much.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://294493]
Approved by Mr. Muskrat
Front-paged by Enlil
and the fire pops...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2017-09-20 10:43 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (236 votes). Check out past polls.