Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

import duties

by jimc (Sexton)
on Dec 24, 2003 at 06:20 UTC ( #316800=perlquestion: print w/replies, xml ) Need Help??

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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://316800]
Approved by jdtoronto
Front-paged by jdtoronto
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2023-02-07 01:38 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (38 votes). Check out past polls.