bless REF,CLASSNAME
...
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
name.
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.
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 :).)
> 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
> name.
It is, actually, it's just rather hard to create subs in that package.
> 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.
See e.g. Text::Balanced.
Ben
--
'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.'
b...@morrow.me.uk
> 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.
++clever:
http://search.cpan.org/~elizabeth/OOB/
OOB - out of band data for any data structure in Perl
--
Affijn, Ruud
"Gewoon is een tijger."
Perhaps more precisely: it maps a referent to a string. The variable
just contains a reference to the referent.
--
Brad