Sudarshan Raghavan <[EMAIL PROTECTED]> writes:
> [EMAIL PROTECTED] creates an anonymous arrayref by copying the values stored
> in @values. perldoc perlref, perldoc perllol
>
> If you are going use this statement
> push @{$ref_to_a}, [EMAIL PROTECTED];
> your while condition must be while (my @values = ...)
>
> If you are going to use
> push @{$ref_to_a}, [EMAIL PROTECTED];
> you can leave the while condition as while (@values = ...)
>
> Reason: 'shallow copying' vs 'deep copying'
> Read through this link
> http://www.stonehenge.com/merlyn/UnixReview/col30.html
To sort-of change the subject, I think the 'deep_copy' subroutine
quoted in this article contains a bug... the sub in question:
sub deep_copy {
my $this = shift;
if (not ref $this) {
$this;
} elsif (ref $this eq "ARRAY") {
[map deep_copy($_), @$this];
} elsif (ref $this eq "HASH") {
+{map { $_ => deep_copy($this->{$_}) } keys %$this};
} else { die "what type is $_?" }
}
The next-to-last line should be:
} else { die "what type is $this?" }
Since '$this' is the thing we're testing for what sort of reference it
is, not '$_'.
Test code:
#!/usr/bin/perl
use strict;
use warnings;
my $foo = 5;
my $bar = \$foo; # a scalar ref
my $baz = deep_copy($bar);
__END__
With the deep_copy sub above, gives the following error:
$ perl perl/deep_copy.pl
Use of uninitialized value in concatenation (.) or string at perl/deep_copy.pl line 19.
what type is ? at perl/deep_copy.pl line 19.
While with the corrected line gives the following (correct) error:
$ perl perl/deep_copy.pl
what type is SCALAR(0x8107e04)? at perl/deep_copy.pl line 20.
Unless I'm missing something. Comments?
-RN
--
Robin Norwood
Red Hat, Inc.
"The Sage does nothing, yet nothing remains undone."
-Lao Tzu, Te Tao Ching
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]