in reply to Preserve the value of original array
Hai, Don't dereference the original array. Instead of dereference just store it in different array in the second subroutine.
use strict; my @num = qw(1 2 3 4 5); first(); sub first { print "\@num before: @num\n"; # prints 1 2 3 4 5 my $result = second(\@num); print "result: $result\n"; print "\@num after: @num\n"; # prints 1 2 3 4 5 6 # But I would need it to still print # 1 2 3 4 5 } sub second { my @num_ref = shift; push(@num_ref, 6); my $sum = 0; foreach (@num_ref) { $sum += $_; } return $sum; }
|
|---|