lexical declaration and array dereference

K

ko

Quick question. The code snippet works under the following builds:

v5.6.1 built for MSWin32-x86-multi-thread (Activestate 635)
v5.6.1 built for i386-freebsd (pkg_info = perl-5.6.1_11)

==CODE==
use strict;
use warnings;

my $string = '1 2 3 4';
my @$ar = split(/\s+/,$string);
print "@$ar\n";

==RESULTS==
1 2 3 4

But if run under Activestate 804 or 5.005_03 for i386-freebsd (taking
out 'use warnings, and replacing with #!/usr/bin/perl -w), I get the
following error message:

'Can't declare array dereferences in my at...'

Looking at the error message, I would guess that you're only allowed
to declare scalar, array, or hash variables? Being a novice
programmer, I thought the declaration was ok through autovivification.
Could someone please explain what I am missing and why it works under
Perl 5.6.1?

Thanks in advance
keith
 
U

Uri Guttman

k> Quick question. The code snippet works under the following builds:
k> v5.6.1 built for MSWin32-x86-multi-thread (Activestate 635)
k> v5.6.1 built for i386-freebsd (pkg_info = perl-5.6.1_11)

k> my @$ar = split(/\s+/,$string);

k> But if run under Activestate 804 or 5.005_03 for i386-freebsd (taking
k> out 'use warnings, and replacing with #!/usr/bin/perl -w), I get the
k> following error message:

k> 'Can't declare array dereferences in my at...'

it has nothing to do with build numbers but perl versions. i tried that
on 5.004_04, 5.005_02, 5.005_03 and 5.6.1. it works only on 5.6.1 so
they probably added the ability to autovivify and my declare in one
statement.

k> Looking at the error message, I would guess that you're only allowed
k> to declare scalar, array, or hash variables? Being a novice
k> programmer, I thought the declaration was ok through autovivification.
k> Could someone please explain what I am missing and why it works under
k> Perl 5.6.1?

if you want that construct, then use 5.6.1.

i would stick with:

my $a = [ split(/\s+/,$string) ] ;

which does the same thing and works in all perl5 versions

uri
 
T

Tassilo v. Parseval

Also sprach Uri Guttman:
k> Quick question. The code snippet works under the following builds:
k> v5.6.1 built for MSWin32-x86-multi-thread (Activestate 635)
k> v5.6.1 built for i386-freebsd (pkg_info = perl-5.6.1_11)

k> my @$ar = split(/\s+/,$string);

k> But if run under Activestate 804 or 5.005_03 for i386-freebsd (taking
k> out 'use warnings, and replacing with #!/usr/bin/perl -w), I get the
k> following error message:

k> 'Can't declare array dereferences in my at...'

it has nothing to do with build numbers but perl versions. i tried that
on 5.004_04, 5.005_02, 5.005_03 and 5.6.1. it works only on 5.6.1 so
they probably added the ability to autovivify and my declare in one
statement.

Interesting. I didn't know that you can do this in 5.6.1. Looks a little
like an accidental feature to me, particularly since it is no longer
there in 5.8.x. Also reminds me of a discussion recently held among the
p5porters whether

my @hash{ qw/s l i c e/ } = ();

should be allowed (I think the consensus eventually being that it's not
a good idea).

Tassilo
 
K

ko

Uri said:
i would stick with:

my $a = [ split(/\s+/,$string) ] ;

which does the same thing and works in all perl5 versions

Thank you. That's much more readable than what I originally had.

Unfortunately, being the novice programmer, I sometimes make mistakes
with context and therefore end up creating intermediate variable(s) -
tends to make me see the correct context better for some reason. My
first thought to fix the problem was:

my @a = split(/\s+/,$string);
my $ar = \@a;
some_sub($ar,@other_args);

And other times it just plain escapes me to directly use return values.
I'm still at a point where it takes me three or four steps to accomplish
a task that could be done in one or two steps :(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top