http://qs321.pair.com?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

  • Comment on Perl Dead (Again). tie() Burns Corpse. Incommpotent Monk Assists Sacrilage. News At Eleven.
  • Select or Download Code

Replies are listed 'Best First'.
Re: Perl Dead (Again). tie() Burns Corpse. Incommpotent Monk Assists Sacrilage. News At Eleven.
by sauoq (Abbot) on Jul 23, 2002 at 22:50 UTC

    After fixing several typos ( '[' instead of '{' in two places, 'sun FETCH' instead of 'sub FETCH', a missing single quote around OVERLOAD, and a \n in single quotes instead of doubles) it ran on a Solaris 5.7 box without crashing. It printed exactly what I would expect.

    Speak OverloadObj::Test=HASH(0x11c16c)

    Why bother to include the code for OverloadObj.pm if you aren't going to exercise it at all? I can only suggest that you prepare your question better. Update: By forcing a call to OverloadObj::constructor, I've also managed to segfaul on Solaris 5.7. By making constructor() return a reference, I seem to have eliminated the segfault. I have to leave for a meeting though, so I'm done playing with this for now.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Perl Dead (Again). ...
by dws (Chancellor) on Jul 23, 2002 at 22:08 UTC
    If you're going to include code fragments, please copy and past them so that we can get right to the underlying problem, rather than haggling over transcription typos (such as '[' where '{' is called for).

    If that line in import that overloaded bless wasn't commented out, I would suspect the problem has something to do with tie doing a bless under the covers, perhaps triggering recursion.

Re: Perl Dead (Again). tie() Burns Corpse. Incommpotent Monk Assists Sacrilage. News At Eleven.
by rbc (Curate) on Jul 23, 2002 at 22:07 UTC
    This just off the top of my head
    I havn't try to run your code or look at
    it in depth. But

    sub speak [
    print 'Speak\n';
    }

    might be a typo in your post.
    If not that migt be your problem.