Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Perl-Sensitive Sunglasses
 
PerlMonks  

array copy question

by smackdab (Pilgrim)
on Dec 08, 2003 at 17:02 UTC ( [id://313266]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

smackdab has asked for the wisdom of the Perl Monks concerning the following question:

In 5.6 I was doing this to copy an array from an array ref:

my @$ra_ret_save = @$ra_ret;

5.8 doesn't like it and now I am doing:

my $ra_ret_save;
@{$ra_ret_save} = @{$ra_ret};

Was the orig wrong, what changed?

Replies are listed 'Best First'.
Re: array copy question
by duff (Parson) on Dec 08, 2003 at 17:10 UTC

    I don't think you are remembering the 5.6 code correctly. Most likely you had my @array = @{$array_ref};. If my @$foo did work with 5.6 is was a bug and what changed was that bug was fixed.

Re: array copy question
by Roy Johnson (Monsignor) on Dec 08, 2003 at 17:26 UTC
    Just to be clear: the curlies are still not required, but you have to declare the reference ($ra_ret_save) separately from dereferencing it.
    my $ra_ret_save; @$ra_ret_save = @$ra_ret;
    If you really had a jones for doing it all at once:
    my $ra_ret_save = [@$ra_ret];

    The PerlMonk tr/// Advocate
Re: array copy question
by Roger (Parson) on Dec 08, 2003 at 17:49 UTC
    Yes declaration of array reference has changed in 5.8. The code below will not compile under 5.8:
    my @$ra_ret_save = @$ra_ret;
    You do not have to declare array refence and copy from another array in two separate steps. You can do in one single step -
    my $ra_ret_save = [ @$ra_ret ];
    I have created a demo script below -
    use strict; use warnings; use Data::Dumper; my $ra_ret = [ qw/ a b c d / ]; print Dumper($ra_ret); my $ra_ret_save = [ @$ra_ret ]; print Dumper($ra_ret_save);
    And the output when run from 5.8 and 5.6 -
    perl 5.8.0 ---------- $VAR1 = [ 'a', 'b', 'c', 'd' ]; $VAR1 = [ 'a', 'b', 'c', 'd' ]; perl 5.6.1 ---------- $VAR1 = [ 'a', 'b', 'c', 'd' ]; $VAR1 = [ 'a', 'b', 'c', 'd' ];

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://313266]
Approved by diotalevi
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.