|
In a message dated 6/23/2006 1:24:11 P.M. Eastern Standard Time,
[EMAIL PROTECTED] writes:
> On 23 Jun 2006 at 12:41, [EMAIL PROTECTED] wrote:
> > > In a message dated 6/23/2006 12:30:35 P.M. Eastern Standard Time, > > [EMAIL PROTECTED] writes: > > > > > hi charles -- > > > > > > In a message dated 6/22/2006 10:38:47 P.M. Eastern Standard Time, > > [EMAIL PROTECTED] writes: > > > > > > > BEGIN { > > > > > > > > my @pad = (some large list of numbers); > > > > > > > > ... > > > > > > > > > just a point of curiosity... > > > why is @pad defined inside a BEGIN block instead of just any old > > block? > > > a little more reflection is sometimes useful. > > the answer, correct me if i'm wrong, is that placing @pad in a BEGIN > > block closure > > allows one to be sure that the array will be initialized before any of > > the functions that > > depend upon it are run, whereas if it was in an ordinary block closure > > one could not > > be so assured. > > But I'm still not understanding: the *SCOPE* of that declaration should > be limited to the BEGIN block, and so if you try to *use* "@a" anyplace > else you should get an error. In addition to the fact that it is [IMO] > unnecessarily obscure code, since if that worked as you said, then it'd > be quite the exception to the entire block-structuring/scoping rules. this is actually a fairly common perl idiom for making data private to a
function
or functions. it is equivalent in some respects to ``static''
data in other
languages, e.g., c.
>
> I tried a test case: > > perl -e 'BEGIN { my @a = (4); } ; warn "@a" ' > Warning: something's wrong at -e line 1. > > Which is just what I would have expected to have happened. When I change > that code to what I think is the more clear version: > > perl -e 'my @a; BEGIN { @a = (4); } ; warn "@a" ' > 4 at -e line 1. > > it is not only more clear [making the declaration and scope of @a crystal > clear AND making its initialization clear] but it works. so I must be > missing something: did the original code [with the 'my' inside the BEGIN] > actually *work*?? i can't speak for the functions themselves, but the variable should
certainly have
been completely inaccessible outside the block and accessible only to the
functions
defined within the block. see command line examples below, with
and without
final print statement commented out.
>
> /Bernie\ hth -- bill walters
C:[EMAIL PROTECTED]>perl -we "use strict;
BEGIN { my @private = qw(this is private); sub print_private_stuff { print qq(@private \n) } } print_private_stuff(); ### print qq(@private \n)" this is private C:[EMAIL PROTECTED]>perl -we "use strict;
BEGIN { my @private = qw(this is private); sub print_private_stuff { print qq(@private \n) } } print_private_stuff(); print qq(@private \n)" Possible unintended interpolation of @private in string at -e line 1. Global symbol "@private" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors. |
_______________________________________________ ActivePerl mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
