Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: The Null Mull (or, when OO needs more O)

by hardburn (Abbot)
on Nov 29, 2004 at 17:12 UTC ( #410969=note: print w/ replies, xml ) Need Help??


in reply to The Null Mull (or, when OO needs more O)

Since the undef SV is shared between all undefined values, one could modify undef using a feature in Perl 5.8. (I only thought of this because of the solution to #13 of How's your Perl? (II)).

At the beginning of your program (possibly in a BEGIN block) (completely untested):

&Internals::SvREADONLY(\undef, 0); undef = Object::EveryMethod->new; &Internals::SvREADONLY(\undef, 1);

And override Object::EveryMethod for string/num/bool to behave like undef normally does.

"There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.


Comment on Re: The Null Mull (or, when OO needs more O)
Select or Download Code
Re^2: The Null Mull (or, when OO needs more O)
by dragonchild (Archbishop) on Nov 29, 2004 at 18:55 UTC
    Hrmm ... 5.8.4/Solaris doesn't seem to do the right thing.
    BEGIN { &Internals::SvREADONLY(\undef, 0); undef = 42; &Internals::SvREADONLY(\undef, 1); } my $x = undef; print( ((undef) ? 'True' : 'False'), $/); print( ((defined undef) ? 'True' : 'False'), $/); print( (($x) ? 'True' : 'False'), $/); print( ((defined $x) ? 'True' : 'False'), $/); -------------- True True False False
    Maybe the code isn't actually affecting SV_UNDEF as you think ...

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      I just ran your code on 5.8.4 i686-Linux with the same result you did. I'm not sure what is going on here--someone with more understanding of the internals is needed. My guess is that undef isn't staying shared like I expected it would.

      It does work with taking a reference to undef, but this breaks the transparency I was hoping for:

      BEGIN { &Internals::SvREADONLY(\undef, 0); undef = 42; &Internals::SvREADONLY(\undef, 1); } my $x = ${ \undef }; print( ((undef) ? 'True' : 'False'), $/); print( ((defined undef) ? 'True' : 'False'), $/); print( (($x) ? 'True' : 'False'), $/); print( ((defined $x) ? 'True' : 'False'), $/); print $x, $/; __OUTPUT__ True True True True 42

      "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

        BEGIN { &Internals::SvREADONLY(\undef, 0); undef = 42; &Internals::SvREADONLY(\undef, 1); } use constant UNDEF => ${ \undef }; my $x = UNDEF;

        Which is probably better because you're commenting that you did something funky with undef. *shrugs*

        Being right, does not endow the right to be rude; politeness costs nothing.
        Being unknowing, is not the same as being stupid.
        Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
        Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re^2: The Null Mull (or, when OO needs more O)
by diotalevi (Canon) on Nov 29, 2004 at 21:49 UTC
    undef isn't normally an lvalue. How about trying that as ${ \ undef } = ... instead?

      Still no:

      BEGIN { &Internals::SvREADONLY(\undef, 0); ${ \undef } = 42; &Internals::SvREADONLY(\undef, 1); } my $x = undef; print( ((undef) ? 'True' : 'False'), $/); print( ((defined undef) ? 'True' : 'False'), $/); print( (($x) ? 'True' : 'False'), $/); print( ((defined $x) ? 'True' : 'False'), $/); print $x; __OUTPUT__ True True False False

      "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

        Aha. my $x = undef wasn't as simple as we thought. $x was being cleared without PL_undef actually being assigned. Swap the right side for something more complicated like $hash{'non-existant'} and you'll find that $x is now 42.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2014-08-23 08:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (173 votes), past polls