simone
simone

Reputation: 5221

How can I access what variables are being imported in a perl module?

I would like to be able to do something like this:

use Foo::Bar $baz, qw/one two three/;

and then, inside a package

package Foo::Bar;

# probably do something magic here

sub do_something {
     # access $baz and 'one', 'two' and 'three' 
}

I remember seeing it done in a module and thinking it was a cool thing. Now I'd like to do it myself, and can't find the module anymore.

How can I do that?

Upvotes: 1

Views: 56

Answers (1)

Filip Roséen
Filip Roséen

Reputation: 63797

In order to pass data to a module you will need to write your own import sub, as in the below example.


# Foo.pm
package Foo;

use Data::Dumper;

sub import {
    my $package = shift;
    my @data = @_;

    print STDERR Dumper \@data;
}

1;
# foo.pl
use Foo qw/hello world/;

$VAR1 = [
          'hello',
          'world'
        ];

Upvotes: 3

Related Questions