Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Accessing values outside subroutine

by Anonymous Monk
on Oct 21, 2017 at 08:08 UTC ( #1201787=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to access the values which is defined inside a subroutine . Here is the code flow

Sub a { my @array = (1, 2, 3); } Sub b { foreach $value (@array) }

I tried to define array as "our " keyword in Sub a. But, it is not retaining the values when it is accessesed inside Sub b . Is there a way to make it work

Replies are listed 'Best First'.
Re: Accessing values outside subroutine
by Corion (Pope) on Oct 21, 2017 at 08:13 UTC

    No. Declare your variable globally so that it is visible to both subroutines:

    my @array = (1,2,3); sub a { ... }; sub b { ... };

      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"; } }
        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:  <%-{-{-{-<

        You dont call either subroutine

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1201787]
Approved by Athanasius
Front-paged by haukex
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2018-02-19 14:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When it is dark outside I am happiest to see ...














    Results (265 votes). Check out past polls.

    Notices?