creating scalar references via eval

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

Threaded View
Data::Dumper uses code similar to this

        bless(do{\(my $o = "lonweb.mwam.remote")},'MES::Hostname')

when serializing scalar references. It is possible to achieve something
apparently equivalent by calling a suitable, locally-bound function, eg

        local *scalarr_ = sub { $_[0] };
        bless(scalarr_(""), 'MES::Hostname'),
        bless(scalarr_(""), 'MES::Hostname'),
        bless(scalarr_(""), 'MES::Hostname'),
        bless(scalarr_(""), 'MES::Hostname')

which has less textual overhead per reference and (IMHO) looks somewhat
less like "dark magic" to the casual observer. A side effect would be
that all scalar references created in this way end up as PVMGs. This
doesn't really concern me because I'm creating objects, anyway, and also
doesn't seem to be something to worry about if this wasn't the case.

Are there other, less pleasant, side effects?

Re: creating scalar references via eval

There is absolute no problem at your code , but I am wondering how  
usefull can be blessing multiple refs at the same package.
I mean whatever the internal crazyness the "new" conrstruct methods  
usually return one ! Thats how the human brain works.

Ok you could return many , but who is going to use it , Mr.Spok maybe !

Re: creating scalar references via eval

Quoted text here. Click to load it

When evaluated, the do { } returns a references to an anonymous array
containing references to scalars blessed into a class MES::Hostnames,
ie, an array of 'hostname' objects. The example was a 'real-world'
serialization file of the 'hostname database' of some program.

Site Timeline