in reply to Why reftype and blessed are making my life harder than it needs to be

Since the empty string is a valid blessed class, reftype and blessed are doing the right thing there by using undef when the value is not blessed. Otherwise, how would you distinguish the empty blessing from the unblessed, religious overtones notwithstanding?

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re^2: Why reftype and blessed are making my life harder than it needs to be
by Perl Mouse (Chaplain) on Feb 09, 2006 at 22:40 UTC
    Since the empty string is a valid blessed class
    No, it's not. If the second argument of bless is the empty string, the thing being blessed is blessed into main. If you turn on warnings, Perl will tell you so.
    Otherwise, how would you distinguish the empty blessing from the unblessed, religious overtones notwithstanding?
    That's easy. blessed returns main in the first case, and so does ref. ref returns the empty string if its argument isn't a reference - and that's never ambigious, and hence there would be any ambiguity if blessed would do the same.
    #!/usr/bin/perl use strict; use warnings; use Scalar::Util 'blessed'; my $ref = bless [], ""; print ref $ref, "\n"; print blessed $ref, "\n"; __END__ Explicit blessing to '' (assuming package main) at "..." line 8. main main
    Perl --((8:>*

      It's easy to bless into "". This trades on knowledge that package names are null terminated strings.

      bless( ..., "\0" )

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        It's easy to bless into "": bless(..., "\0")
        This seems pathological. All you end up with is a ref that is blessed, but can only execute methods in UNIVERSAL. What practical and legitimate purpose would this serve? And better yet, what functions/modules/applications/etc. does it break in the process? For example, if you use Data::Dumper on such an object, and then eval that string, you won't get the same results (because the eval will create an object in package 'main').

        Just because you can do something, doesn't mean you should.
        That sounds like a bug to me. Internally, all Perl strings are zero terminated, but on the language level, Perl is supposed to be better than C, and be able to deal with "\0" correctly. Here it doesn't. It seems that bless [], "\0" and bless [], "\0foo" are blessed into the same package.
        Perl --((8:>*
      Since the empty string is a valid blessed class
      No, it's not. If the second argument of bless is the empty string, the thing being blessed is blessed into main. If you turn on warnings, Perl will tell you so.
      I'm pretty sure you can bless into "" from XS code. Didn't go so far as to try it though.
Re^2: Why reftype and blessed are making my life harder than it needs to be
by dragonchild (Archbishop) on Feb 09, 2006 at 21:02 UTC
    That's fair enough. So, how would you recommend I handle the cases I bring up?

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?