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:  <%-{-{-{-<


In reply to Re^3: Accessing values outside subroutine by AnomalousMonk
in thread Accessing values outside subroutine by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.