Click here to get back home

How to "use bytes" compatibly ?

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
How to "use bytes" compatibly ? Peter Billam 03-04-2005
Get Chitika Premium
Posted by Peter Billam on March 4, 2005, 10:39 am
Please log in for more thread options


Greetings. In Crypt::Tea_JS I'd like to "use bytes" in one of the
routines, but the trusty old FreeBSD4.8 which I keep around to test
such things can't find bytes.pm in @INC. I know about "use if" but
unfortunately it can't handle that either. Understandably, require
doesn't work on pragmas, so I can't test at run-time.

All I can think of now is to get Makefile.PL to detect versions and
rewrite the Tea_JS.pm in-place accordingly ... (deprecated?)

When were "use bytes" and "use if ..." introduced (5.6?) ?

How should I write a module which will "use bytes", but also run
on old perl ? Or is it time to abandon old perl to its fate ?

--

Regards, Peter

Peter Billam, DPIWE/ILS/CIT/Servers, hbt/lnd/l8, 6233 3061


Posted by chris-usenet on March 4, 2005, 2:27 pm
Please log in for more thread options


> Understandably, require
> doesn't work on pragmas, so I can't test at run-time.

I think you may be missing something here? perldoc -f tells me that
"use" is exactly equivalent to "require" followed by "import".

> How should I write a module which will "use bytes", but also run
> on old perl ? Or is it time to abandon old perl to its fate ?

So, this works safely for me on perl versions 5.005_03 and 5.8.1:

BEGIN {
        eval { require bytes; import bytes (); };
        $@ &&
         warn "eval failed: bytes: $@n";
}


You could omit the "()" construct in production code where you had no
import list, of course.

Chris


Posted by Slaven Rezic on March 7, 2005, 1:16 am
Please log in for more thread options


chris-usenet@roaima.co.uk writes:

> > Understandably, require
> > doesn't work on pragmas, so I can't test at run-time.
>
> I think you may be missing something here? perldoc -f tells me that
> "use" is exactly equivalent to "require" followed by "import".
>
> > How should I write a module which will "use bytes", but also run
> > on old perl ? Or is it time to abandon old perl to its fate ?
>
> So, this works safely for me on perl versions 5.005_03 and 5.8.1:
>
> BEGIN {
>         eval { require bytes; import bytes (); };
>         $@ &&
>          warn "eval failed: bytes: $@n";
> }
>
>
> You could omit the "()" construct in production code where you had no
> import list, of course.
>

Unfortunatel "use bytes" is lexically scoped, so it is not much of use
in a BEGIN block. I use rather the following trick.

BEGIN {
if ($] < 5.006) {
         $INC = 1; # cheating that warnings.pm is loaded
         *warnings::import = sub { }; # do nothing
         *warnings::unimport = sub { };
}
}

From this point "use warnings" and "no warnings" may be used on all
perls, with no effect on older perls.

As an alternative, if.pm is pure perl and also runs with older perls.
You can just add a prereq (it's also on CPAN) or include it in your
distribution.

Regards,
Slaven

--
Slaven Rezic - slaven <at> rezic <dot> de

Tk-AppMaster: a perl/Tk module launcher designed for handhelds
        http://tk-appmaster.sf.net


Posted by Peter Billam on March 7, 2005, 1:00 pm
Please log in for more thread options


>
> Unfortunately "use bytes" is lexically scoped, so it is not
> much of usein a BEGIN block. I use rather the following trick.
>
> BEGIN {
> if ($] < 5.006) {
>          $INC = 1; # cheating that warnings.pm is loaded
> *warnings::import = sub { }; # do nothing
> *warnings::unimport = sub { };
> }
> }
>
> From this point "use warnings" and "no warnings" may be used on all
> perls, with no effect on older perls.
>
I like that; if that works on my trusty old FreeBSD4.8 when I get
home, that's what I'll use :-)

Related question: given a piece of Perl syntax (like "use bytes"),
is there a way of discovering at which version it was introduced ?

> As an alternative, if.pm is pure perl and also runs with older perls.
> You can just add a prereq (it's also on CPAN) or include it in your
> distribution.
>
It's short, too. But I'd be scared to include in my distribution -
wouldn't CPAN complain that I was defining subroutines in namespaces
that weren't mine ?

I wrote:
> In my particular case, it's "split //, $s;" that I need, not "length",
> and split's not mentioned in my "perldoc bytes" as having a bytes::split
> equivalent ...
>
and yet (5.008003) "split //, $s" works fine according to "use bytes"
or "no bytes", so that's OK :-)

--

Thanks for your help, Regards, Peter

Peter Billam, DPIWE/ILS/CIT/Servers, hbt/lnd/l8, 6233 3061


Posted by Slaven Rezic on March 7, 2005, 10:41 pm
Please log in for more thread options



> >
> > Unfortunately "use bytes" is lexically scoped, so it is not
> > much of usein a BEGIN block. I use rather the following trick.
> >
> > BEGIN {
> > if ($] < 5.006) {
> >          $INC = 1; # cheating that warnings.pm is loaded
> > *warnings::import = sub { }; # do nothing
> > *warnings::unimport = sub { };
> > }
> > }
> >
> > From this point "use warnings" and "no warnings" may be used on all
> > perls, with no effect on older perls.
> >
> I like that; if that works on my trusty old FreeBSD4.8 when I get
> home, that's what I'll use :-)
>
> Related question: given a piece of Perl syntax (like "use bytes"),
> is there a way of discovering at which version it was introduced ?
>

This should be harmless since it does not import anything:

if (!eval { require bytes; 1 }) {
# insert the fake code
}

Another way is just to install Module::CoreList and use the corelist
script:

$ corelist bytes
bytes was first released with perl 5.006

> > As an alternative, if.pm is pure perl and also runs with older perls.
> > You can just add a prereq (it's also on CPAN) or include it in your
> > distribution.
> >
> It's short, too. But I'd be scared to include in my distribution -
> wouldn't CPAN complain that I was defining subroutines in namespaces
> that weren't mine ?
>

You can put if.pm into a subdirectory of your module distribution,
e.g.

lib/YourModule/if.pm

and say

use YourModule::if, ...;

instead. Or just inline the code, if it's short. And you can edit
META.yml to make the if.pm module invisible for the various indexers.

Regards,
Slaven

--
Slaven Rezic - slaven <at> rezic <dot> de

tkruler - Perl/Tk program for measuring screen distances
http://ptktools.sourceforge.net/#tkruler


Similar ThreadsPosted
Net::SSH::Perl - Corrupted check bytes February 8, 2006, 9:33 am
Net::SNMP - ERROR: Invalid IpAddress length (8 bytes) January 20, 2006, 1:41 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap