Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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 ( #529220=note: print w/replies, xml ) Need Help??


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

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:>*
  • Comment on Re^2: Why reftype and blessed are making my life harder than it needs to be
  • Download Code

Replies are listed 'Best First'.
Re^3: Why reftype and blessed are making my life harder than it needs to be
by diotalevi (Canon) on Feb 10, 2006 at 05:16 UTC

    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.

        It breaks nothing, it's just a string. I think, maybe, the only time you'd ever bless into "" is when you wanted to have an object pretend it wasn't an object in the eyes of something else. Most anything else that uses ref() isn't going to be able to tell that this is an object.

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      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:>*

        Yes, I suppose so, but it's long standing and doesn't impede most people. Packages are the only null terminated string in perl. You might be able to get perl to leak memory if you blessed into things with lots of data after the null.

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re^3: Why reftype and blessed are making my life harder than it needs to be
by ysth (Canon) on Feb 10, 2006 at 04:43 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.
    I'm pretty sure you can bless into "" from XS code. Didn't go so far as to try it though.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2020-10-24 18:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (246 votes). Check out past polls.

    Notices?