Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Accessing values outside subroutine

by Anonymous Monk
on Oct 21, 2017 at 09:14 UTC ( [id://1201789]=note: print w/replies, xml ) Need Help??


in reply to Re: Accessing values outside subroutine
in thread Accessing values outside subroutine

my @array get values assigned in sub a . I think even if i define outside as below..it won't print anything in sub b

my @array; sub a { @array = (1,2,3); } sub b { foreach my $a (@array) { print "$a\n"; } }

Replies are listed 'Best First'.
Re^3: Accessing values outside subroutine
by AnomalousMonk (Archbishop) on Oct 21, 2017 at 10:35 UTC
    I think ... it won't print ...

    You think? What happens when you do it?

    c:\@Work\Perl\monks>perl -wMstrict -le "my @ra; ;; sub Sa { @ra = (1,2,3); } ;; sub Sb { foreach my $n (@ra) { print $n; } } ;; Sa; Sb; " 1 2 3
    Note that the behavior of this code depends on execution order. The execution order  Sb; Sa; will have very different results, at least the first time. Try it!

    Update: How could you make this code immune to execution order problems? One way might be:

    c:\@Work\Perl\monks>perl -wMstrict -le "{ my @ra; ;; sub S_init { @ra = (1, 2, 3); } ;; sub S_doit { S_init() if @ra == 0; foreach my $n (@ra) { print $n; } } } ;; S_doit(); " 1 2 3
    The  @ra array is defined within a lexical scope and is completely inaccessible to any code outside the scope (except by deep magick — but you didn't hear that from me). An initialization state exists for  @ra that is checked on every invocation of the  S_doit() subroutine; if the array isn't initialized, initialize it before doing anything else.

    Unfortunately, this imposes the burden, which may not be small, of checking the state of  @ra (and who knows what else) on every  S_doit() invocation. Is there a way to avoid this overhead? This

    c:\@Work\Perl\monks>perl -wMstrict -le "BEGIN { my @ra = (1, 2, 3); ;; sub S_doit { foreach my $n (@ra) { print $n; } } } ;; S_doit(); " 1 2 3
    does lexical initialization at compile time and avoids the need for further thought about what happens at run time. (Update: See BEGIN, UNITCHECK, CHECK, INIT and END in perlmod for info on BEGIN (and the related INIT) blocks.)

    Of course, with techniques like these, you're edging closer and closer to a full-on Object Oriented Programming approach, so why not just take the plunge?


    Give a man a fish:  <%-{-{-{-<

      Thanks it Worked!

Re^3: Accessing values outside subroutine
by Anonymous Monk on Oct 21, 2017 at 09:19 UTC
    You dont call either subroutine

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1201789]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-20 01:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found