Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > an array of of arrays in Perl, behavior different in Strawberry andLinux perl

Reply
Thread Tools

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

 
 
removeps groups
Guest
Posts: n/a
 
      09-13-2012
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?

 
Reply With Quote
 
 
 
 
Peter Makholm
Guest
Posts: n/a
 
      09-13-2012
removeps groups <(E-Mail Removed)> writes:

> 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
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      09-13-2012
Peter Makholm <(E-Mail Removed)> writes:
> removeps groups <(E-Mail Removed)> writes:


[...]

>> (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/perl5140delt...l-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.'
 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      09-13-2012
Peter Makholm <(E-Mail Removed)> writes:
> removeps groups <(E-Mail Removed)> writes:


[...]

>> (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/perl5140delt...l-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.'
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Perl 5.14 Windows Strawberry Perl 64 bits brillisoft@gmail.com Perl Misc 0 07-08-2012 07:28 PM
ActiveState Perl and MinGW [was: Perl 5.14 Windows Strawberry Perl 64 bits] Dilbert Perl Misc 0 11-10-2011 02:20 PM
Why my Strawberry Perl act strangely under MSYS? Water Lin Perl Misc 3 01-06-2009 06:21 AM
strawberry perl: Form method not found Nospam Perl Misc 0 03-23-2008 04:09 AM
Installing strawberry perl on winxp sp2 already with activeperl Nospam Perl Misc 0 03-19-2008 08:20 AM



Advertisments