Robin Norwood wrote:


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?



You are right, the last line should be } else { die "what type is $this?" }


-RN







-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to