Cloning Perl Arrays

Continuous script interrupted by comments …

Basic infrastructure:

#!/usr/bin/perl

use strict;
use warnings;
use English;

my $ref_array = [1, 2, 3, 4, 6, 5];
&print_array('ref_array', $ref_array);

sub print_array {
    my $name = shift;
    my $ref_array = shift;

    print $name . ' = [';
    if (@$ref_array) {
        print $ref_array->[0];
        for (my $i = 1; $i < @$ref_array; ++$i) {
            print ', ' . $ref_array->[$i];
        }
    }
    print ']' . "n";
}

Cloning of array contents into a new array

my @array = @$ref_array;
$array[4] = 5;
&print_array('ref_array', $ref_array);
&print_array('array', @array);

Cloning of array contents into a new array reference

my $ref2_array;
@$ref2_array = @$ref_array;
$ref_array->[5] = 6;
&print_array('ref_array', $ref_array);
&print_array('ref2_array', $ref2_array);
Feedback is always welcome! If you'd like to get in touch with me concerning the contents of this article, please use Twitter.