Bitten!

grantm on 2006-06-02T02:40:51

I just 'tidied up' some code from the form in sub_a (below) to the form in sub_b ...

my $sub = shift or die "must specify 'A' or 'B'\n";

if(uc($sub) eq 'A') {
    sub_a();
}
else {
    sub_b();
}

exit;

sub sub_a {
    my $hashref = hashref();
    while(my($k, $v) = each %{ $hashref }) {
        print "$k => $v\n";
    }
}

sub sub_b {
    while(my($k, $v) = each %{ hashref() }) {
        print "$k => $v\n";
    }
}

sub hashref {
  return { a => 1, b => 2, c => 3 };
}

I was surprised when my modified routine behaved in a different way to the original. When I thought about it, I realised what was going on and slapped myself. It's probably obvious to everyone else though.


Re:

Aristotle on 2006-06-02T13:27:09

:-)