jimc has asked for the wisdom of the Perl Monks concerning the following question:

Ive been experimenting with adding a questionable feature to Data::Dumper::EasyOO-0.03_01 (on CPAN), ie: passing a $var to import(), which initializes it with an object thats then available for the (lazy, and arent we all) user:
my ($mdd, %style); BEGIN { %style = (indent=>1, autoprint=>1) }; # 1st 2 work, last doesnt ! use Data::Dumper::EasyOO ( %style, init => \$mdd); use Data::Dumper::EasyOO ( %style, init => \our $odd); use Data::Dumper::EasyOO ( %style, init => \my $ndd); $mdd->(mdd => \%INC); $odd->(odd => \%INC); $ndd->(ndd => \%INC);
I expected 3rd way to work just like the others, by creating a my variable in main. In support of my expectation, the following works, almost analogously, except for the use-time action.
perl -e 'sub f{my$p=shift;$$p++;} f(\(my ($g)="val")); print "g:$g\n" +'
thanks in advance for your insights, and I take bug reports ;-)

Replies are listed 'Best First'.
Re: import duties
by Anonymous Monk on Dec 24, 2003 at 11:33 UTC
    According to perlfunc, C<use> is exactly equivalent to

    BEGIN { require Module; import Module LIST; }

    so the my declaration is only in scope for the use statement.

      Thanks for the (now obvious) explanation. shoulda slept on it.
Re: import duties
by tcf22 (Priest) on Dec 24, 2003 at 14:57 UTC
        I expected 3rd way to work just like the others, by creating a my variable in main.

    If you use Devel::Symdump, you'll see that my variables, aren't actually in the main package, they are local to the block.. The list outputted by the following code, shows all scalar symbols in the main package.
    #! /usr/bin/perl use strict; use Devel::Symdump; my $obj = Devel::Symdump->new('main'); my $var1 = 'var1'; our $var2 = 'var2'; foreach($obj->scalars){ print "$_\n"; } __OUTPUT__ ...Builtin Vars... main::var2 ...More Builtin Vars...
    Update: Fixed misplaced sentence fragment.

    - Tom