an array of of arrays in Perl, behavior different in Strawberry andLinux perl

R

removeps groups

Using Strawberry Perl "This is perl 5, version 16, subversion 1 (v5.16.1) built for MSWin32-x86-multi-thread", the following program compiles and prints out the things noted below.

use strict;
use warnings;
my $myarray = [[1,11], [2,22]];
push(@$myarray[1], 222);
print "MYDEBUG $$myarray[1][0]\n"; # prints 2
print "MYDEBUG $$myarray[1][1]\n"; # prints 22
print "MYDEBUG $$myarray[1][2]\n"; # prints 222
print "MYDEBUG @{$$myarray[1]}\n"; # prints 2 22 222

But on Linux perl "This is perl, v5.6.1 built for i686-linux", the above program results in a compile error

Type of arg 1 to push must be array (not array slice) at array.pl line 4, near "222)"

To get the above program to work in Linux perl, through trial and error, I found this is the script that works:

use strict;
use warnings;
my @tmpmyarray = ([1,11], [2,22]);
my $myarray = \@tmpmyarray;
push(@{$$myarray[1]}, 222);
print "MYDEBUG $$myarray[1][0]\n"; # prints 2
print "MYDEBUG $$myarray[1][1]\n"; # prints 22
print "MYDEBUG $$myarray[1][2]\n"; # prints 222
print "MYDEBUG @{$$myarray[1]}\n"; # prints 2 22 222

This script also works on Strawberry Perl.

Questions:

(1) Is Strawberry Perl wrong to run the first snippet, or is Linux Perl wrong to not run it?
(2) I don't understand the use of $, @, {}, \ to get things working. Is there some logic to it?
 
P

Peter Makholm

removeps groups said:
Using Strawberry Perl "This is perl 5, version 16, subversion 1 (v5.16.1) built for MSWin32-x86-multi-thread", the following program compiles and prints out the things noted below.

use strict;
use warnings;
my $myarray = [[1,11], [2,22]];
push(@$myarray[1], 222);

The expression '@$myarray[1]' evaluates to an array slice containing a
single array ref.

In Perl 5.14 the definition of push was changed to accept an array
reference as the first argument, previously the first argument could
only be an real array.

This explains the difference in behaviour between your Strawberry Perl,
which is quite new and the perl on your Linux system which is horribly
old (11 years 5 months old to be exact). Get a newer Linux system.

(I wouldn't expect Linux distributions older than January 2012 to
include a perl newer than 5.12)
Questions:

(1) Is Strawberry Perl wrong to run the first snippet, or is Linux
Perl wrong to not run it?

Your Strawberry Perl behaves as it ought to with a modern perl. Your
Linux perl behavies as intended in the previous decade.
(2) I don't understand the use of $, @, {}, \ to get things working.
Is there some logic to it?

Yes. Have you read the perlref and perlreftut manual pages?

//Makholm
 
R

Rainer Weikusat

Peter Makholm said:
[...]
(1) Is Strawberry Perl wrong to run the first snippet, or is Linux
Perl wrong to not run it?

Your Strawberry Perl behaves as it ought to with a modern perl. Your
Linux perl behavies as intended in the previous decade.

This was introduced with perl 5.14.0, released on 2011/05/14. The
corresponding 'changes' documentation paragraph starts with

,----
| Warning: This feature is considered experimental, as the exact
| behaviour may change in a future version of Perl.
`----
http://perldoc.perl.org/perl5140delta.html#Syntactical-Enhancements

Consequently, this would be more correctly described as 'You are using
an experimental feature a litte more than a year ago. Unless you're
prepared to (and capable of) using "roll-your-own" versions of perl
everywhere where you might need to use perl, you should probably not
yet write code depdendent on this at the moment.'
 
R

Rainer Weikusat

Peter Makholm said:
[...]
(1) Is Strawberry Perl wrong to run the first snippet, or is Linux
Perl wrong to not run it?

Your Strawberry Perl behaves as it ought to with a modern perl. Your
Linux perl behavies as intended in the previous decade.

This was introduced with perl 5.14.0, released on 2011/05/14. The
corresponding 'changes' documentation paragraph starts with

,----
| Warning: This feature is considered experimental, as the exact
| behaviour may change in a future version of Perl.
`----
http://perldoc.perl.org/perl5140delta.html#Syntactical-Enhancements

Consequently, this would be more correctly described as 'You are using
an experimental feature introduced a litte more than a year
ago. Unless you're prepared to (and capable of) using "roll-your-own"
versions of perl everywhere where you might need to use perl, you
should probably not yet write code depdendent on this at the moment.'
 

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

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top