Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

detecting active pragmas?

0 views
Skip to first unread message

Ben Low

unread,
Feb 21, 2000, 3:00:00 AM2/21/00
to

I'd like to know within a module whether the integer pragma is in force.
Other than testing 10/3 == 9, is there a general method for detecting if
a given module/pragma is in use?


--

b.d.low (at) unsw edu au


Sent via Deja.com http://www.deja.com/
Before you buy.

Ilya Zakharevich

unread,
Feb 21, 2000, 3:00:00 AM2/21/00
to
[A complimentary Cc of this posting was sent to Ben Low
<af3...@my-deja.com>],
who wrote in article <88qga6$dt4$1...@nnrp1.deja.com>:

>
>
> I'd like to know within a module whether the integer pragma is in force.
> Other than testing 10/3 == 9, is there a general method for detecting if
> a given module/pragma is in use?

vgrep.

Ilya

Gaal Yahas

unread,
Feb 21, 2000, 3:00:00 AM2/21/00
to
On Mon, Feb 21, 2000 at 04:52:23AM +0000, Ben Low wrote:

[cc:ed]

> I'd like to know within a module whether the integer pragma is in force.
> Other than testing 10/3 == 9, is there a general method for detecting if
> a given module/pragma is in use?

Pretty dirty solution (kludge alert!), tested on linux:

print is_loaded('integer') ? "loaded\n" : "not loaded\n";

sub is_loaded {
my $mod = shift;
my $rc = 0;
$mod =~ s,::,/,;

for (keys %::) {
if ($_ =~ /^_<.*${mod}\.pm/) {
$::{$_} =~ /^\main::${_}/;
$rc++;
last
}
}
return $rc;
}

--
believing is seeing
ga...@forum2.org
http://www.forum2.org/gaal/

Malcolm Dew-Jones

unread,
Feb 21, 2000, 3:00:00 AM2/21/00
to
Ilya Zakharevich (il...@math.ohio-state.edu) wrote:
: [A complimentary Cc of this posting was sent to Ben Low
: <af3...@my-deja.com>],
: who wrote in article <88qga6$dt4$1...@nnrp1.deja.com>:
: >
: >
: > I'd like to know within a module whether the integer pragma is in force.

: > Other than testing 10/3 == 9, is there a general method for detecting if
: > a given module/pragma is in use?

: vgrep.

Sounds interesting. What/where is vgrep?

malcolm

: Ilya

--

Michael J Assels

unread,
Feb 21, 2000, 3:00:00 AM2/21/00
to
In article <88qga6$dt4$1...@nnrp1.deja.com>, Ben Low <af3...@my-deja.com> wrote:
>
>I'd like to know within a module whether the integer pragma is in force.
>Other than testing 10/3 == 9, is there a general method for detecting if
>a given module/pragma is in use?

You could look in %:: for appropriate strings. E.g.:

% perl -le 'map {print if s/^_<(.*\.pm)$/$1/} keys %::'

% perl -Mvars -le 'map {print if s/^_<(.*\.pm)$/$1/} keys %::'
/pkg/perl5/lib/5.00503/vars.pm

% perl -MFile::Find -le 'map {print if s/^_<(.*\.pm)$/$1/} keys %::'
/pkg/perl5/lib/5.00503/Carp.pm
/pkg/perl5/lib/5.00503/File/Find.pm
/pkg/perl5/lib/5.00503/decalpha-dec_osf/Config.pm
/pkg/perl5/lib/5.00503/Exporter.pm
/pkg/perl5/lib/5.00503/Cwd.pm


Season to taste.

Michael

Greg Bacon

unread,
Feb 21, 2000, 3:00:00 AM2/21/00
to
In article <2000022123...@netvision.net.il>,
Gaal Yahas <ga...@forum2.org> writes:

: print is_loaded('integer') ? "loaded\n" : "not loaded\n";


:
: sub is_loaded {
: my $mod = shift;
: my $rc = 0;
: $mod =~ s,::,/,;
:
: for (keys %::) {
: if ($_ =~ /^_<.*${mod}\.pm/) {
: $::{$_} =~ /^\main::${_}/;
: $rc++;
: last
: }
: }
: return $rc;

: }

I don't believe that this does what Ben wanted:

#! /usr/bin/perl -lw

sub is_loaded {
my $mod = shift;
my $rc = 0;
$mod =~ s,::,/,;

for (keys %::) {
if ($_ =~ /^_<.*${mod}\.pm/) {
$::{$_} =~ /^\main::${_}/;
$rc++;
last
}
}
return $rc;
}

sub foo {
use integer;

print "foo: ", is_loaded 'integer';
}

sub bar {
print "bar: ", is_loaded 'integer';
}

foo;
bar;
[17:30] ruby% ./try
foo: 1
bar: 1

I interpret Ben's post as looking for `bar: 0' (or something
equivalent).

Greg
--
Join the good guys. Don't fuck with the web.
-- Abigail in <slrn7o89jc....@alexandra.delanet.com>

Tushar Samant

unread,
Feb 21, 2000, 3:00:00 AM2/21/00
to
af3...@my-deja.com writes:
>I'd like to know within a module whether the integer pragma is in force.

Why?

>Other than testing 10/3 == 9, is there a general method for detecting if
>a given module/pragma is in use?

Maybe integer.pm should have an in_force or some such sub, which
returns $^H & 1 ... IF there were any earthly need to know.

Gaal Yahas

unread,
Feb 21, 2000, 3:00:00 AM2/21/00
to
On Mon, Feb 21, 2000 at 11:18:07PM +0200, I wrote:

> Pretty dirty solution (kludge alert!), tested on linux:

Errr, oops, "creative cut'n'paste error", I guess.

> print is_loaded('integer') ? "loaded\n" : "not loaded\n";
>
> sub is_loaded {
> my $mod = shift;
> my $rc = 0;
> $mod =~ s,::,/,;
>
> for (keys %::) {
> if ($_ =~ /^_<.*${mod}\.pm/) {

# XXXX WRONG:


> $::{$_} =~ /^\main::${_}/;
> $rc++;
> last

# the previous three lines should be merged to:
$::{$_} =~ /^\main::${_}/ && $rc++ && last;
> }
> }
> return $rc;
> }

Sorry :-/

Mark-Jason Dominus

unread,
Feb 21, 2000, 3:00:00 AM2/21/00
to

[Mailed and posted]

> I'd like to know within a module whether the integer pragma is in force.

It's not entirely clear why that would ever be useful. The `use
integer' pragma is lexically scoped, and that means you can know at
the time you write the code whether or not a particular line is in its
scope or not. There's no reason to need to find out at run time
because it never changes.

That said, there was some discussion of this on p5p back in April
1998, although I don't think anything ever came of it. The subject
line was `A suggestion for less.pm', should you want to look it up. I
actually proposed a module `use pragmas' that would provide a `using'
function so that you could say

if (using 'integer') { ... }

although I don't think it would work in the form I suggested,
something similar probably would work.

Benjamin Low

unread,
Feb 21, 2000, 3:00:00 AM2/21/00
to

On Mon, 21 Feb 2000 14:22:22 Mark-Jason Dominus wrote:

>> I'd like to know within a module whether the integer pragma is in force.
>
>It's not entirely clear why that would ever be useful. The `use
>integer' pragma is lexically scoped, and that means you can know at
>the time you write the code whether or not a particular line is in its
>scope or not.

Of course, I knew that :-) Thanks for the reminder.

Ben

--== Sent via Deja.com http://www.deja.com/ ==--
Share what you know. Learn what you don't.

Markus Laker

unread,
Feb 21, 2000, 3:00:00 AM2/21/00
to
yf...@victoria.tc.ca (Malcolm Dew-Jones) wrote:

> Sounds interesting. What/where is vgrep?

Visual grep. You scan the code by eye. :-)

Markus

--
Web site of the week (UK only): <http://www.zonelabs.com/>:
A free personal firewall for Windows and NT

Ilya Zakharevich

unread,
Feb 21, 2000, 3:00:00 AM2/21/00
to
[A complimentary Cc of this posting was sent to Malcolm Dew-Jones
<yf...@victoria.tc.ca>],
who wrote in article <38b1...@news.victoria.tc.ca>:
> : > I'd like to know within a module whether the integer pragma is in force.
> : > Other than testing 10/3 == 9, is there a general method for detecting if

> : > a given module/pragma is in use?
>
> : vgrep.

>
> Sounds interesting. What/where is vgrep?

I do not think it is legal in US due to ADA. Other countries may have
similar work laws for disabled. Consult your lawyer.

vgrep is the "visual grep". You look through the code and notice all
the 'use/no integer' in place.

No third party can change pragmatic aspects of your code. There is no
need to check for interness, any more than to check what is the
current package in your code.

[Of course, unless you inject code into a friendly module by C<eval
$code> done by this module. But again, to be sure it is much better
to *insert* 'no integer' on top of the code to eval than *check* for
the current state.]

Ilya

Markus Laker

unread,
Feb 21, 2000, 3:00:00 AM2/21/00
to
Ben Low <af3...@my-deja.com> wrote:

> I'd like to know within a module whether the integer pragma is in force.
> Other than testing 10/3 == 9, is there a general method for detecting if
> a given module/pragma is in use?

You can test whether a particular module has been C<use>d like this:

print "1: Carp exists\n" if exists $main::{'Carp::'};
eval 'use Carp';
print "2: Carp exists\n" if exists $main::{'Carp::'};

The C<eval> is necessary because C<use> statements are executed at
compile time; they don't wait until the program executes. Without the
C<eval>, package Carp would exist before the first C<print> statement
was executed.

How does this code work? The oddly named %main:: is a hash containing
the symbol table for package main. If a dynamically scoped variable
called $foo exists then so does $main::{'foo'}. And if package Foo
exists, so does $main::{'Foo::'}. Finally, if $Foo::bar exists, so
does $Foo::{'bar'} or, equivalently, $main::Foo::{'bar'}.

I believe I read somewhere that, in older versions of Perl, you can't
check for the existence of a package without creating it.
Unfortunately, I can't find any record of that in the perldelta files.
If your code has to run under old Perls, you'll need to check that it
works.

Now, $^H is defined in perlvar as being

# The current set of syntax checks enabled by use strict and other
# block scoped compiler hints.

So I'd expect $^H to change with C<use strict>, C<use integer> and the
like. But on my machine (ActiveState build 522 for Win32), the
following code

#!perl -w

no strict;
print $^H, "\n";
use strict 'vars';
print $^H, "\n";
use integer;
print $^H, "\n";

prints '1281' three times. Why?

Kevin Reid

unread,
Feb 21, 2000, 3:00:00 AM2/21/00
to
Malcolm Dew-Jones <yf...@victoria.tc.ca> wrote:

> Ilya Zakharevich (il...@math.ohio-state.edu) wrote:
> : Ben Low <af3...@my-deja.com> wrote in <88qga6$dt4$1...@nnrp1.deja.com>:
> : >
> : > I'd like to know within a module whether the integer pragma is in


> : > force. Other than testing 10/3 == 9, is there a general method for
> : > detecting if a given module/pragma is in use?

> :


> : vgrep.
>
> Sounds interesting. What/where is vgrep?

<http://www.tuxedo.org/~esr/jargon/html/entry/vgrep.html>

--
Kevin Reid: | Macintosh:
"I'm me." | Think different.

Ilya Zakharevich

unread,
Feb 22, 2000, 3:00:00 AM2/22/00
to
[A complimentary Cc of this posting was sent to Tushar Samant
<Tb...@pimpdaddy.com>],
who wrote in article <88sfa2$pm8$1...@news.enteract.com>:
> Why?

Indeed.

> Maybe integer.pm should have an in_force or some such sub, which
> returns $^H & 1 ... IF there were any earthly need to know.

$^H is in force *at compile time*. At run time there is nothing to
test (unless you eval).

Ilya

Tushar Samant

unread,
Feb 22, 2000, 3:00:00 AM2/22/00
to
il...@math.ohio-state.edu writes:
>> Maybe integer.pm should have an in_force or some such sub, which
>> returns $^H & 1 ... IF there were any earthly need to know.
>
>$^H is in force *at compile time*.

And use integer lexically scoped. I missed that totally.

Simon Cozens

unread,
Feb 22, 2000, 3:00:00 AM2/22/00
to
Michael J Assels (comp.lang.perl.moderated):

>>Other than testing 10/3 == 9, is there a general method for detecting if
>>a given module/pragma is in use?
>
>You could look in %:: for appropriate strings. E.g.:

Bletch, mildly. Yes, it works, but you may find it easier to use %INC
instead:

printing keys %:: with integer loaded gives you:
STDOUT
INC
integer::
_
@
"
$
stdout
ARGV
IO::
STDIN
ENV

DB::
stdin
CORE::
/

0
UNIVERSAL::

_</usr/local/lib/perl5/5.00561/integer.pm
_<universal.c
DynaLoader::

_<-e
STDERR
_<perlmain.c
BEGIN
stderr
main::

printing keys %INC gives you:
integer.pm

Your choice, I suppose.

--
There are no threads in a.b.p.erotica, so there's no gain in using a
threaded news reader.
(Unknown source)

Joe Smith

unread,
Feb 22, 2000, 3:00:00 AM2/22/00
to
In article <m1f3bs85i63e0ukpg...@4ax.com>,

Markus Laker <lakerDele...@tcp.co.uk> wrote:
>Now, $^H is defined in perlvar as being
>
># The current set of syntax checks enabled by use strict and other
># block scoped compiler hints.
>
>So I'd expect $^H to change with C<use strict>, C<use integer> and the
>like. But on my machine (ActiveState build 522 for Win32), the
>following code
>
> #!perl -w
>
> no strict;
> print $^H, "\n";
> use strict 'vars';
> print $^H, "\n";
> use integer;
> print $^H, "\n";
>
>prints '1281' three times. Why?

Because you're testing a run time instead of at compile time.

unix% cat temp.pl
use integer;
BEGIN {
print "In the first BEGIN{} block, \$^H = $^H\n";
}
print "'use integer': At run time, \$^H = $^H\n";

no integer;
BEGIN {
print "In the second BEGIN{} block, \$^H = $^H\n";
}
print "'no integer': At run time, \$^H = $^H\n";
unix% perl temp.pl
In the first BEGIN{} block, $^H = 1
In the second BEGIN{} block, $^H = 0
'use integer': At run time, $^H = 256
'no integer': At run time, $^H = 256
unix%

-Joe
--
See http://www.inwap.com/ for PDP-10 and "ReBoot" pages.

ze...@bawdycaste.org

unread,
Feb 22, 2000, 3:00:00 AM2/22/00
to
Markus Laker <lakerDele...@tcp.co.uk> wrote:
>snip<
: You can test whether a particular module has been C<use>d like this:

: print "1: Carp exists\n" if exists $main::{'Carp::'};
: eval 'use Carp';
: print "2: Carp exists\n" if exists $main::{'Carp::'};

Are you sure?

$ ./foo.pl
2: Carp exists
3: Carp exists
Undefined subroutine &Foo::carp called at ./foo.pl line 8.
$ cat foo.pl
#!/usr/local/bin/perl -w

print "1: Carp exists\n" if exists $main::{'Carp::'};
eval 'use Carp';
print "2: Carp exists\n" if exists $main::{'Carp::'};

package Foo;
print "3: Carp exists\n" if exists $main::{'Carp::'};
carp("What's up, doc?");

__END__

All the world is not in %main::.

--
-Zenin (ze...@archive.rhps.org) "Hey, are you one of those Linux coders?"
"Nyet. Linux coder in next office."
"Good man. Ignore the screams."
--www.userfriendly.org

Larry Rosler

unread,
Feb 22, 2000, 3:00:00 AM2/22/00
to
In article <1e6d6l4.1gs2zp61jbjfvkN%kpr...@attglobal.net> on Mon, 21 Feb
2000 20:03:02 -0500, Kevin Reid <kpr...@attglobal.net> says...

> Malcolm Dew-Jones <yf...@victoria.tc.ca> wrote:
>
> > Ilya Zakharevich (il...@math.ohio-state.edu) wrote:
> > : Ben Low <af3...@my-deja.com> wrote in <88qga6$dt4$1...@nnrp1.deja.com>:
> > : >
> > : > I'd like to know within a module whether the integer pragma is in
> > : > force. Other than testing 10/3 == 9, is there a general method for

> > : > detecting if a given module/pragma is in use?
> > :
> > : vgrep.
> >
> > Sounds interesting. What/where is vgrep?
>
> <http://www.tuxedo.org/~esr/jargon/html/entry/vgrep.html>

I am reminded (from the days of adb, sdb, xdb and such) of recommending
the use of edb (eyeball debugger); though the advent of egrep should
have changed that to vdb by analogy.

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
l...@hpl.hp.com

Hans Mulder

unread,
Feb 24, 2000, 3:00:00 AM2/24/00
to
Gaal Yahas wrote:


> Pretty dirty solution (kludge alert!), tested on linux:
>

> print is_loaded('integer') ? "loaded\n" : "not loaded\n";

Errhm, the question wasn't whether "integer .pm" was loaded; the
question was whether is is active. Lexical pragmas like "strict"
and "interger" may well be loaded by other modules; but they
don't apply to your code, unless your code is in the scope of the
relevant "use" declaration.

Usually this is easy to establish by just reading your own code
(since the pargam is lexical, a "use integer" hidden in some other
file doesn't count), but you can wriggle yourself into a position
where you don't know whether you've "use"d integer, e.g.

BEGIN {
require integer;

if ($ARGV[0] eq "--integer") {
shift;
import integer;
}
}

After this fragment "interger.pm" has been loaded, but it may or
may no be active. The easiest way to find out is, of course,

if ( 7/2 == 3 ) { whatever; }

If you really want, you can test the appropriate bit in $^H.
Keep in mind that you need to know its value at compile time:

my $integer_is_active;
BEGIN { $integer_is_active = $^H & 1; }

if ($integer_is_active) { whatever; }

Hope this helps,

-- HansM

H. Merijn Brand

unread,
Feb 24, 2000, 3:00:00 AM2/24/00
to
han...@xs4all.nl (Hans Mulder) wrote in <38B471E6...@xs4all.nl>:

< Cut *ALL* to prevent rejection >

Maybe a strange follow-up question, but how to detect it in XS?

I mean, if retreiving fields from a database with DBI/DBD with
a driver that returns fields as-is: floats, doubles, amounts and
all like those as Perl's NV's

Does the DBD has to do something or DBI or does the caller deal
with this?

--
H.Merijn Brand
using perl5.005.03 on HP-UX 10.20, HP-UX 11.00, AIX 4.2.1, AIX 4.3.0, DEC
OSF/1 4.0 and WinNT 4.0 SP-6a, often with Tk800.018 and/or DBD-Unify
ftp://ftp.funet.fi/pub/languages/perl/CPAN/authors/id/H/HM/HMBRAND/
Member of Amsterdam Perl Mongers (http://www.amsterdam.pm.org/)

Hans Mulder

unread,
Feb 27, 2000, 3:00:00 AM2/27/00
to
"H. Merijn Brand" wrote:

> Maybe a strange follow-up question, but how to detect it in XS?

Detect what? Whether the pragma is active in the XS file, or
whether it is active in the perl code calling your XS routine?
Since the XS language does not have pragmas, you probably meant
the latter.

I think it would be a mistake to do that. These pramgas are
meant to be lexical in nature, i.e. using the pragma should not
affect the behaviour of code outside its scope. Since hte XS
code is in a different file from the one using the integer pragma,
the XS code should not alter its behaviour based on that.

> I mean, if retreiving fields from a database with DBI/DBD with
> a driver that returns fields as-is: floats, doubles, amounts and
> all like those as Perl's NV's

I haven't tried it, but I'd be hightly surprised if DBI altered
its behaviour in response to a lexical setting in the caller.
If you want to truncate numbers retrieved from a database, then
I think the best way to do it in the SQL query.

> Does the DBD has to do something or DBI or does the caller deal
> with this?

Perhaps I'm misinterpreting your question, but it sounds as if
you're afraid that Perl hides its ability to deal with float when
you "use" integer. That's not quite what the pragma does; it only
makes Perl truncate numbers when doing arithmetic. For example,

use integer;

$x = 1.5;

print $x, " > ", $x+0, "\n";

prints:

1.5 > 1

0 new messages