http://www.perlmonks.org?node_id=184615

erikharrison has asked for the wisdom of the Perl Monks concerning the following question:

I have a feeling I'm gonna be flamed for even writing this, but . . .man am I outta my depth.

Here's a module I wrote:

package OverloadObj use Want; sub import [ my $calling_package = caller; #{$calling_package . '::bless'} = \&constructor; } sub constructor { my $self; tie $self, 'OverloadObj', @_; return $self; } sub TIESCALAR { CORE::bless $_[1]; } sun FETCH { return $_[0] if want('REF'); $class = ref $_[0]; goto &{$class . 'OVERLOAD} (@_); } sub STORE { my $self = shift; untie $$self; } 1;

Now for a game of obfuscation! Can you guess what this prints?

package OverloadObj::Test; use OverloadObj; sub new { bless {}; } sub speak [ print 'Speak\n'; } sub OVERLOAD { return 'Bob'; } package main; $test = new OverloadObj::Test; $test->speak; print "$test";

Give up? On my Mandrake Linux 8.2 box, running 5.6.1, it print "Segmentation Fault.

First off let me say that I know the module doesn't qork, even if it didn't segfault. The want call is naive and I know that the STORE method isn't doing what I want it to do (and I'll explain that part in a bit). Frankly, I'm more than a little embarassed to show this bit of incomplete code. I've learned alot about Perl, but I don't get enough chances to code it - meaning I can fix other people's bugs faster and easier than I fix my own.

Disclaimer out of the way, a bit of background. The code pretty much came to me in a flash while thinking about something :Larry said in one of the Apocolypes, namely that every first class object would be able to define it's own numify and stringify overloadings. I saw lots of nifty uses for this technique, and decided I'd try to code up a modeule which did something similar in Perl 5 and make it a gift to the community and the Monastery in particular. After the pure Perl implementation proved unworkable and fragile (Although I came amazingly close, and it was pretty clever if I have to say so myself) I downloaded WAnt.pm took it to me Linux box, ran make and had this thing ready to begin serious debugging about 20 minutes. The idea is to replace the object itself with a smart tie()d object that knows when to return the object itself and when to call $obj->OVERLOAD to allwo the object to provide numification/stringification. So, I wrote the quick test above for the preliminary debugging. Then segfault.

I'll be frank, I'm an idiot. I'm not a coder. I've been running Linux for a couple of weeks, and I can barely login to bash. I don't now what a segfault is. I've read Oh my God! Tie killed Perl! (everyone should) but I don't even now where to look to begin figuring out the source of the problem. Is it my bug? Perl's? Want? Linux? All of us?

/me bows his head in submission to authority and wisdom of other monks.

PS Sorry I'm not as funny as Petruchio. Maybe I'll think of an elaborate song and dance for whoever helps me fix the problem.

Cheers,
Erik

Light a man a fire, he's warm for a day. Catch a man on fire, and he's warm for the rest of his life. - Terry Pratchet