Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

array copy question

by smackdab (Pilgrim)
on Dec 08, 2003 at 22:02 UTC ( #313266=perlquestion: print w/replies, xml ) Need Help??
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 Roger (Parson) on Dec 08, 2003 at 22: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' ];
Re: array copy question
by Roy Johnson (Monsignor) on Dec 08, 2003 at 22: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 duff (Parson) on Dec 08, 2003 at 22: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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://313266]
Approved by diotalevi
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2018-02-24 22:12 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (311 votes). Check out past polls.