Injecting variables after the import subroutine has been called?

Discussion in 'Perl Misc' started by Daniel S., Jul 7, 2004.

  1. Daniel S.

    Daniel S. Guest

    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.
     
    Daniel S., Jul 7, 2004
    #1
    1. Advertising

  2. Daniel S.

    Paul Lalli Guest

    On Wed, 7 Jul 2004, Daniel S. wrote:

    > 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
     
    Paul Lalli, Jul 7, 2004
    #2
    1. Advertising

  3. Daniel S.

    Daniel S. Guest

    Paul Lalli <> wrote in message news:<>...
    > On Wed, 7 Jul 2004, Daniel S. wrote:
    >
    > > 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


    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.
     
    Daniel S., Jul 7, 2004
    #3
  4. Daniel S.

    gnari Guest

    "Daniel S." <> wrote in message
    news:...
    > Paul Lalli <> wrote in message

    news:<>...
    > > On Wed, 7 Jul 2004, Daniel S. wrote:
    > >


    [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
     
    gnari, Jul 7, 2004
    #4
  5. Daniel S.

    Ben Morrow Guest

    Quoth (Daniel S.):
    > Paul Lalli <> wrote in message news:<>...
    > > On Wed, 7 Jul 2004, Daniel S. wrote:
    > >
    > > > 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.

    >
    > 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

    --
    Like all men in Babylon I have been a proconsul; like all, a slave ... During
    one lunar year, I have been declared invisible; I shrieked and was not heard,
    I stole my bread and was not decapitated.
    ~ ~ Jorge Luis Borges, 'The Babylon Lottery'
     
    Ben Morrow, Jul 15, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad
    Replies:
    8
    Views:
    3,351
    Marian Kostal
    Jan 11, 2007
  2. Paul J. Lucas
    Replies:
    28
    Views:
    6,209
    Dale King
    Dec 5, 2003
  3. Replies:
    1
    Views:
    18,032
  4. king
    Replies:
    5
    Views:
    202
  5. Replies:
    2
    Views:
    136
Loading...

Share This Page