Idle thought about bless

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
perldoc -f bless says:

       This function tells the thingy referenced by REF that it is now
       an object in the CLASSNAME package."

But that's not true! All bless does is associate an arbitrary string
with a variable; in such a way that the string can be recovered via ref.

The CLASSNAME does not even have to conform to the syntax of a valid
package name. The code

   my $struct = {};
   bless($struct, "text with embedded spaces");
works exactly like it should; yet that string is not a valid package

In other words, objects are an application of bless, but bless really
has nothing to do with objects. It just maps a variable to a string.

I was wondering, has anyone ever found some clever use for bless, other
than for doing OO? It might be handy to put your variables into named
categories that don't correspond to packages, for example.

Re: Idle thought about bless

Quoted text here. Click to load it

The *intended* use of bless is to construct objects. If the
implementation is flexible enough to allow other uses, that's just a
bonus. (This happens a lot in Perl :).)

Quoted text here. Click to load it

It is, actually, it's just rather hard to create subs in that package.

Quoted text here. Click to load it

See e.g. Text::Balanced.


'Deserve [death]? I daresay he did. Many live that deserve death. And some die
that deserve life. Can you give it to them? Then do not be too eager to deal
out death in judgement. For even the very wise cannot see all ends.'

Re: Idle thought about bless

fishfry schreef:

Quoted text here. Click to load it

OOB - out of band data for any data structure in Perl

Affijn, Ruud

"Gewoon is een tijger."

Re: Idle thought about bless

Quoted text here. Click to load it

Perhaps more precisely: it maps a referent to a string.  The variable
just contains a reference to the referent.


Site Timeline