Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: (Ovid) Re: Why does this dump core?

by danger (Priest)
on Dec 12, 2001 at 01:25 UTC ( [id://131052]=note: print w/replies, xml ) Need Help??


in reply to (Ovid) Re: Why does this dump core?
in thread Why does this dump core?

That's not the same problem. Your new problem is that you aren't using an iterator (keys(), values(), each()), but just directly evaluating the hash in list context --- and in 5.6.1 that results in the values part of the returned list being the actual values in the hash (instead of copies, as per my previous post). This shouldn't be a real problem because the normal way to do what you are trying to do would be to use keys() in the foreach loop to just iterate over the keys (and thus never be in the position of having an alias to a hash-value that no longer exists). There is no 'forgetting' of keys going on in your problem, your code is deleting a *value* that the foreach-list already has an alias to (because you used foreach (%hash) instead of foreach (keys %hash)).

Granted, this shouldn't segfault --- it doesn't when warnings are turned on (at least on 5.6.1 on linux). However, you can cause the same sort of behaviour on an array if you foreach over a list of array elements and remove (splice()) an element from the array before we get there in the list (segfaults on 5.00503 and 5.6.1, linux):

my @array = (0,1,2); foreach my $el ($array[0],$array[1],$array[2]){ print ">>$el<<\n"; splice(@array,1,1) if $el == 0; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://131052]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-03-28 14:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found