niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » Perl » modules
How to "use bytes" compatibly ?
Post new topic   Reply to topic Page 1 of 1 [6 Posts] View previous topic :: View next topic
Author Message
Slaven Rezic
*nix forums beginner


Joined: 01 Mar 2005
Posts: 18

PostPosted: Mon Mar 07, 2005 7:41 pm    Post subject: Re: How to "use bytes" compatibly ? Reply with quote

Peter Billam <peter@pjb.dpiwe.tas.gov.au> writes:

Quote:
In article <87fyz8s6v8.fsf@vran.herceg.de>, Slaven Rezic wrote:

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{"warnings.pm"} = 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

Quote:
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
Back to top
Peter Billam
*nix forums beginner


Joined: 25 Feb 2005
Posts: 26

PostPosted: Mon Mar 07, 2005 12:00 am    Post subject: Re: How to "use bytes" compatibly ? Reply with quote

In article <87fyz8s6v8.fsf@vran.herceg.de>, Slaven Rezic wrote:
Quote:

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{"warnings.pm"} = 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 ?

Quote:
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:
Quote:
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
Back to top
Slaven Rezic
*nix forums beginner


Joined: 01 Mar 2005
Posts: 18

PostPosted: Sun Mar 06, 2005 10:16 pm    Post subject: Re: How to "use bytes" compatibly ? Reply with quote

chris-usenet@roaima.co.uk writes:

Quote:
Peter Billam <peter@pjb.dpiwe.tas.gov.au> wrote:
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{"warnings.pm"} = 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
Back to top
Peter Billam
*nix forums beginner


Joined: 25 Feb 2005
Posts: 26

PostPosted: Sun Mar 06, 2005 8:55 pm    Post subject: Re: How to "use bytes" compatibly ? Reply with quote

In article <fa4mf2-c3d.ln1@news.roaima.co.uk>, chris-usenet@roaima.co.uk wrote:
Quote:
Peter Billam <peter@pjb.dpiwe.tas.gov.au> wrote:
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".

I know that's what it says, but as far as pragmas are concerned,
it seems to be, er, inexact. "require" is executed at run-time,
so it can't scope syntactically, or affect compilation options.

Quote:
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";
}

That compiles OK, but it doesn't have much effect.
For example, adapting from "perldoc bytes":

$x = chr(400);
print "length is ", length $x, "\n"; # length is 1 char
{
require bytes; import bytes;
print "length is still ", length $x, "\n"; # length is STILL 1 char
print "bytes::length is ", bytes::length $x, "\n"; # bytes::length is 2
}

but if I put bytes::length in my code then it won't run on 5.005_03 :-(

(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 ... (this might vary with version, perhaps ...))

If it is a choice between
1) abandoning old perl
2) editing Tea_JS.pm in-place at the start of Makefile.PL
then which is the less deprecated ?

--

Regards, Peter

Peter Billam, DPIWE/ILS/CIT/Servers, hbt/lnd/l8, 6233 3061
Back to top
Guest






PostPosted: Fri Mar 04, 2005 12:27 pm    Post subject: Re: How to "use bytes" compatibly ? Reply with quote

Peter Billam <peter@pjb.dpiwe.tas.gov.au> wrote:
Quote:
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".

Quote:
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
Back to top
Peter Billam
*nix forums beginner


Joined: 25 Feb 2005
Posts: 26

PostPosted: Thu Mar 03, 2005 9:39 pm    Post subject: How to "use bytes" compatibly ? Reply with quote

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
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [6 Posts] View previous topic :: View next topic
The time now is Wed Jan 07, 2009 11:04 pm | All times are GMT
navigation Forum index » Programming » Perl » modules
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts [PATCH] Mantaining turnstile aligned to 128 bytes in i386... Attilio Rao Architecture 5 Tue Jul 25, 2006 3:13 pm
No new posts Specified key was too long; max key length is 1000 bytes ... Lubomir Host 'rajo MySQL 1 Fri Jul 07, 2006 9:52 am
No new posts spam scanning only the first X bytes of an email Ian P. Christian Exim 0 Tue Jul 04, 2006 2:21 pm
No new posts Archive::Zip problem - bytes being added/changed by zip->... Mike Scott Perl 2 Tue Jul 04, 2006 8:17 am
No new posts to access the large page(2112 bytes) NAND Flash from the ... venkatesh.k.desai5@gmail. C 3 Wed Jun 21, 2006 3:14 pm

Credit Cards | Personal Car Finance | Credit Cards | Credit Counseling | Remortgages
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2543s ][ Queries: 20 (0.1303s) ][ GZIP on - Debug on ]