Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: why am I getting odd behavior on DESTROY

by Corion (Patriarch)
on Mar 20, 2015 at 09:39 UTC ( [id://1120721]=note: print w/replies, xml ) Need Help??


in reply to why am I getting odd behavior on DESTROY

Adding

use strict;

after

package it_item;

will make Perl point out your error.

You (re)use a global variable $self in various places in that package.

This is actually a point where the new signatures feature (only available in 5.20+) could be useful as it makes it easier to avoid accidentially using global variables. On the other hand, you can still forget to list $self in the formal parameters of your subroutine...

Replies are listed 'Best First'.
Re^2: why am I getting odd behavior on DESTROY
by einhverfr (Friar) on Mar 20, 2015 at 13:28 UTC
    As a note, if anyone needs an example of why use strict is so important even for little things, this is a pretty good example and I will not be unhappy if you share it as an example of what not to do.

      As a quick write-up as to why this happens and why use strict helps here:

      The destructor reassigns the garbage-collected object to a global package variable, whichof course cancels the destruction. The next one gets copied over the existing package variable, which of course throws the first object back into garbage collection, which gets copied over the package variable, etc. in a loop.

      What is suprising here si that Perl doesn't go into an endless loop. Naturally this is hard to debug using standard tools because it is not a misnamed variable but a side effect of a scoping error. Strict prevents this sort of error.

      Again, anyone wants to use it for presentations or anything regarding why strict, feel free.

      If I could triple ++ this post I would. Out with the Ego, in with the Honest Man.

Re^2: why am I getting odd behavior on DESTROY
by einhverfr (Friar) on Mar 20, 2015 at 10:09 UTC
    Right. Teach me to say "just a proof of concept, not going to worry about use strict and use warnings...." :-P

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-24 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found