| Store | Cart

Re: it's probably just me

From: Francisco Zarabozo <fzar...@hotmail.com>
Wed, 28 Oct 2015 10:33:15 -0600
Hello John,

As I said, you should always use strict and warnings, for your own good. If 
you do, you will receive a fatal message telling you that your array is not 
actually declared even when you used the "local" keyword. Using warnings and 
strict is one of the best ways to quickly detect risks, typos, bugs and 
errors in your code.

The "my" and "our" keywords are meant for variable declaration and they 
differ in scope. When declared with "my", they are visible only to the 
current immediate scope, and when declared with "our", they are visible from 
other scopes too.

Now, the "local" keyword is meant to treat a variable or handler as a copy 
of an original one for the current scope. You can then alter it and it will 
be restored to its original state when leaving that scope. You can read more 
about it here: http://perldoc.perl.org/functions/local.html

HTH :-)

Francisco Zarabozo



-----Mensaje original----- 
From: John DePasquale
Sent: Wednesday, October 28, 2015 10:17 AM
To: 'Francisco Zarabozo' ; acti...@listserv.ActiveState.com
Subject: RE: it's probably just me

Autovivication. Interesting concept. I didn't realize that such a reference
in a print statement was causing new array rows to be created. Explains a
great deal about the vexing bugs I've been wrestling with today.
Much thanks for the information, Francisco.
Regarding LOCAL vs.OUR, I've been using LOCAL for a long time to declare
variables that I need visible throughout a program, seems to work very well.
you would use OUR to accomplish the same need?

John DePasquale
Chief Executive Officer
Paradigm Consulting
"Proudly presenting the Vinopedia System"
www.vinopedia.us
49 Dalby Street
Newton, MA  02458
Mobile: 617-610-2424
Fax: 617-600-7326



-----Original Message-----
From: Francisco Zarabozo [mailto:fzar...@hotmail.com]
Sent: Wednesday, October 28, 2015 11:48 AM
To: John DePasquale; acti...@listserv.ActiveState.com
Subject: Re: it's probably just me

> From: John DePasquale, Sent: Wednesday, October 28, 2015 9:13 AM>The following snippet prints a 1>                local @aList;>                push @aList, ['first','second','third'];>                my $nCount = @aList;>                print "count: $nCount";

Yes, of course it does. After your push, @aList contains a single element,
which is an arrayref. So $aList[0] = ['first', 'second', 'third'], which is
a single element.

> BUT, if I add one seemingly irrelevant line ( the second one below ),> something crazy happens:>                local @aList;>                print "non-existent value: $aList[0][0]\n";>                push @aList, ['first','second','third'];>                my $nCount = @aList;>                print "count: $nCount";

Not crazy at all. Perl has this thing called "autovivification", where it
creates an element on the fly when requested by your own code, like your
second line is creating not only the first element of @aList, but it's also
creating it as an arrayref by specifying its own first element too.

>instead of printing a 1, the last line prints a 2

After that autovivification, you are then pushing again a hashref containing
on its own 3 elements, but it is a single element by itself to @aList.
That's why it prints a 2.

So, first things first. Always use strict and warnings. That would've have
you noticed that "local" is not the keyword you want to use. That keyword is
not for declaring a variable. Instead, use "my" or "our". Here's an example:

use strict;
use warnings;

my @aList;
push @aList, (1, 2, 3); # Notice I used parenthesis, not brackets.
my $nCount = @aList;
print "count: $nCount\n"; # This will print 3

# Now, I can push an arrayref by using brackets. An arrayref is a single
element.
push @aList, [4, 5, 6];
print "Number of aList elements: "....@aList."\n";  # This will print 4

# Now I can push more elements by using parenthesis:
push @aList, (7 .. 10);
print "Number of aList elements: "....@aList."\n";  # This will print 8

# @aList now contains the following structure of 8 elements:
@aList = (
    1,
    2,
    3,
    [4, 5, 6],
    7,
    8,
    9,
    10,
);

# And I can access the elements on that arrayhref by doing this:

print "Arrayref elements are $aList[3][0], $aList[3][1] and $aList[3][2]\n";


HTH

Francisco Zarabozo






_______________________________________________
ActivePerl mailing list
Acti...@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Recent Messages in this Thread
John DePasquale Oct 28, 2015 03:13 pm
JONES, ROBERT E CTR USAF AETC AETC/A5TP Oct 28, 2015 03:20 pm
Francisco Zarabozo Oct 28, 2015 03:48 pm
John DePasquale Oct 28, 2015 04:17 pm
Francisco Zarabozo Oct 28, 2015 04:33 pm
nonlin Oct 28, 2015 05:57 pm
Messages in this thread