Injecting variables after the import subroutine has been called?

D

Daniel S.

Hi!

I'm the author of the Regexp::Extended module and I'm trying to inject
variables into the main perl module (like the Exporter module does).

I'm using overload:constant to modify regexp expressions and there is
an extension for named groups (?<var>pattern). However, if I try to
export '$var' at the time overload:constant is being called (after the
import sub has been executed) all I get is a segfault.

Is there a way of exporting variables during the execution of a perl
code short of using $::var?

Thanks for any help, no matter how complex or obscure.
Daniel S.
 
P

Paul Lalli

Hi!

I'm the author of the Regexp::Extended module and I'm trying to inject
variables into the main perl module (like the Exporter module does).

I'm using overload:constant to modify regexp expressions and there is
an extension for named groups (?<var>pattern). However, if I try to
export '$var' at the time overload:constant is being called (after the
import sub has been executed) all I get is a segfault.

Is there a way of exporting variables during the execution of a perl
code short of using $::var?

Er, is there a reason you can't just use the import function in your main
code?


#!/usr/bin/perl
use strict;
use warnings;
use MyMod('$foo');

print "Module's foo value: $foo\n";

import MyMod ('$bar');
print "Module's bar value: $bar\n";

__END__

Of course, MyMod in this case would still have to have $bar in it's
@EXPORT_OK array.


'use' is nothing more than a call to require and import wrapped in a
BEGIN{} block.

Paul Lalli
 
D

Daniel S.

Paul Lalli said:
Er, is there a reason you can't just use the import function in your main
code?


#!/usr/bin/perl
use strict;
use warnings;
use MyMod('$foo');

print "Module's foo value: $foo\n";

import MyMod ('$bar');
print "Module's bar value: $bar\n";

__END__

Of course, MyMod in this case would still have to have $bar in it's
@EXPORT_OK array.


'use' is nothing more than a call to require and import wrapped in a
BEGIN{} block.

Paul Lalli

Well the problem happens to be that people declare named pattern
groups like this:

"1-2-2003" =~ /(?<date>\d+-\d+-\d+)/;

which gets coded as:

"1-2-2003" =~ /(\d+-\d+-\d+)(?{ $date = $^N })/;

However, $date has never been declared so I get a bit fat error
message at compilation under strict.

What I can do is this:

"1-2-2003" =~ /(?<date>\d+-\d+-\d+)(?{ $::date = $^N })/;

But its a pain to have to say $::date afterwards instead of plain
$date.

Daniel S.
 
G

gnari

Daniel S. said:
Paul Lalli <[email protected]> wrote in message

[dynamic export of symbols from module to caller under strict]

did you look at the Exporter code

(untested)
my $pgk='Yourpgk';
my $callpkg = caller();
my $sym='somevar';
*{"${callpkg}::$sym"} = \${"${pkg}::$sym"};

this is just grossly taken from Exporter.pm, and I
may have mangled it.

gnari
 
B

Ben Morrow

Quoth (e-mail address removed) (Daniel S.):
Well the problem happens to be that people declare named pattern
groups like this:

"1-2-2003" =~ /(?<date>\d+-\d+-\d+)/;

which gets coded as:

"1-2-2003" =~ /(\d+-\d+-\d+)(?{ $date = $^N })/;

However, $date has never been declared so I get a bit fat error
message at compilation under strict.

What I can do is this:

"1-2-2003" =~ /(?<date>\d+-\d+-\d+)(?{ $::date = $^N })/;

But its a pain to have to say $::date afterwards instead of plain
$date.

....also it breaks namespacing (all your regex captures go into main::).

The following works for me (perl5.8.2):

#!/usr/bin/perl

use strict;
use warnings;

BEGIN {
{{

package RX;

use overload;

sub import {
overload::constant qr => sub {
my $x = shift;
my $c = caller;

for ($x =~ /\$(\w+)/) {
warn "importing \$$_ into $c";
no strict "refs";
*{"$c\::$_"} = \${"$c\::$_"};
}

return $x;
}
}

}}

RX->import;
}

my $rx = qr/(\d+-\d+-\d+)(?{ $date = $^N })/;
print $rx;
"2004-02-03" =~ $rx;
print $date;

__END__

It outputs

importing $date into main at overload line 23.
(?-xism:(\d+-\d+-\d+)(?{ $date = $^N }))
2004-02-03

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top