Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re^2: A curious case of of my()

by shmem (Chancellor)
on May 05, 2011 at 10:34 UTC ( #903112=note: print w/replies, xml ) Need Help??

in reply to Re: A curious case of of my()
in thread A curious case of of my()

$href is empty, since it is not passed into foo. hence @array ends up being a package variable and not a lexical variable.

As others have pointed out, that's wrong. my creates lexicals at compile time. It doesn't have a our fallback. The 'if' modifier with a false condition inhibits the opcode which clears the lexical at runtime.

our @array; # package vaiable @array = qw( foo bar ); sub foo{ my $var = shift; my $href = shift; my @array = $href->{pass_in} if defined $href and exists $href +->{pass_in}; push @array, $var; print "contents : @array\n"; } print "outer scope - contents : @array\n"; foo(1); foo(2); print "outer scope - contents : @array\n"; __END__ outer scope - contents : foo bar contents : 1 contents : 1 2 outer scope - contents : foo bar

As you can see, the variable @array inside the sub is a lexical.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://903112]
[Your Mother]: I am quite aware that Module::Build is MUCH more in favor with many monks but I had trouble with it every time I tried to use it and trouble with CPAN stuff that used it too.
[Your Mother]: Take my advice with a grain of salt. I haven't done a new CPAN release in years at this point.
[LanX]: I want to author a new module for CPAN w/o complicated build structure
[LanX]: ther is also Dist::Zilla :-/
[LanX]: How to make a CPAN Module Distribution from 2002
[Your Mother]: Dist::Zilla is kind of the nuclear option. I set it up back in the day but it's really targeted at authors who are managing MANY modules and want to automate everything.
[erix]: perlancar should know ;)
[LanX]: see warning

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2017-08-18 17:45 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (306 votes). Check out past polls.