Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to make a blessable anonymous scalar ref?

Reply
Thread Tools

How to make a blessable anonymous scalar ref?

 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      03-14-2005
J Krugman <(E-Mail Removed)> wrote:
> [] and {} are blessable anonymous refs. I.e., both
>
> bless [], 'Foo';
>
> and
>
> bless {}, 'Bar';
>
> Is there a way to get a blessable anonymous ref to a scalar? The
> typical example of an anonymous scalar ref is something like \3,
> but if one tries to bless such a ref, the compiler chokes on the
> "attempt to modify a read-only value".


I see no reason this won't work:

bless \\undef, "Foo";

(Which does not mean that there is no reason it won't work.)

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
 
 
 
kj
Guest
Posts: n/a
 
      03-14-2005
In <d14hre$43v$(E-Mail Removed)> Brian McCauley <(E-Mail Removed)> writes:

>Now you are just being silly.


Finally.

kj
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
 
Reply With Quote
 
 
 
 
J Krugman
Guest
Posts: n/a
 
      03-14-2005
In <20050314145658.526$(E-Mail Removed)> http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

>J Krugman <(E-Mail Removed)> wrote:
>> [] and {} are blessable anonymous refs. I.e., both
>>
>> bless [], 'Foo';
>>
>> and
>>
>> bless {}, 'Bar';
>>
>> Is there a way to get a blessable anonymous ref to a scalar? The
>> typical example of an anonymous scalar ref is something like \3,
>> but if one tries to bless such a ref, the compiler chokes on the
>> "attempt to modify a read-only value".


>I see no reason this won't work:


>bless \\undef, "Foo";


I like this one a lot. And also Anno's

bless \ "$_", 'Foo' for 3;

but I'm mystified by both. Specifically, I don't understand why
the following:

bless \undef, 'Foo' ===> Modification of a read-only value attempted
bless \\undef, 'Foo' ===> [ no error ]
bless \1, 'Foo' ===> Modification of a read-only value attempted
bless \\1, 'Foo' ===> Modification of a read-only value attempted

Why are the behaviors for undef and 1 different?

I can only guess at why Anno's idea works. It looks like interpolation
causes a new memory location to be created. But I have never seen
anything like this documented anywhere, so I'm just guessing wildly.

Anyway, many thanks!

kj




createdLikewise, I don't understand why Anno's idea works, but

bless \"3", 'Foo'

triggers an error. After all, "$_"

bless \"3", 'Foo' ===> Modification of a read-only value attempted
bless \"$_", 'Foo' for 3 ===> [ no error ]


--
To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.

 
Reply With Quote
 
kj
Guest
Posts: n/a
 
      03-15-2005
In <d13hm0$9he$(E-Mail Removed)-Berlin.DE> http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Anno Siegel) writes:

>J Krugman <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>>
>>
>>
>> [] and {} are blessable anonymous refs. I.e., both
>>
>> bless [], 'Foo';
>>
>> and
>>
>> bless {}, 'Bar';
>>
>> Is there a way to get a blessable anonymous ref to a scalar? The
>> typical example of an anonymous scalar ref is something like \3,
>> but if one tries to bless such a ref, the compiler chokes on the
>> "attempt to modify a read-only value".


> bless \ "$_", 'Foo' for 3;



Why do you need the "for 3"?

bless \"$_", 'Foo'

would a reference to a (possibly empty) string scalar. No harm in
that. \"$_" is succinct enough to be a good candidate for idiomhood.
Or else \"$$", \"$]", etc.

kj

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      03-15-2005
J Krugman <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> In <20050314145658.526$(E-Mail Removed)> (E-Mail Removed) writes:


> >> Is there a way to get a blessable anonymous ref to a scalar? The


[...]

> >I see no reason this won't work:

>
> >bless \\undef, "Foo";

>
> I like this one a lot. And also Anno's
>
> bless \ "$_", 'Foo' for 3;
>
> but I'm mystified by both. Specifically, I don't understand why
> the following:
>
> bless \undef, 'Foo' ===> Modification of a read-only value attempted
> bless \\undef, 'Foo' ===> [ no error ]
> bless \1, 'Foo' ===> Modification of a read-only value attempted
> bless \\1, 'Foo' ===> Modification of a read-only value attempted
>
> Why are the behaviors for undef and 1 different?


I think it shouldn't work in the case of undef either. A spelled-out
reference is basically a memory address. It depends on whatever the
reference is taken of and cannot be changed.

> I can only guess at why Anno's idea works. It looks like interpolation
> causes a new memory location to be created. But I have never seen
> anything like this documented anywhere, so I'm just guessing wildly.


So am I, I must confess. I don't know if it is documented anywhere.
I have used it (in variants) for so long, it doesn't bother me anymore.
In a similar way

for ( qw( fie foe fum) ) {
$_ .= 'X';
}

doesn't work, but

for ( map "$_", qw( fie foe fum) ) {
$_ .= 'X';
}

does.

I added "my" solution

bless \ "$_", 'Foo' for 3;

in the spirit of TIMTOWTDI, but it has disadvantages. For one, it doesn't
return a value ("for" doesn't), so if you need the value (you will) it must
be assigned inside the for "loop". Then you can't declare the variable
right there (not in a modified statement), so it becomes

my $obj;
$obj = bless \ "$_", 'Foo' for 3;

If it's the last thing happening in a sub,

return bless \ "$_", 'Foo' for 3;

is okay, but return is necessary. In general I'd go with

my $obj = bless \ do { my $x }, 'Foo';

The do {} block is optional, but with it you strictly have an anonymous
scalar ref.

Anno
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      03-15-2005
kj <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> In <d13hm0$9he$(E-Mail Removed)-Berlin.DE>
> (E-Mail Removed)-berlin.de (Anno Siegel) writes:


[...]

> > bless \ "$_", 'Foo' for 3;

>
> Why do you need the "for 3"?


It gives a working equivalent of "bless \ 3, 'Foo'". "for" isn't needed.

Anno
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      03-15-2005
J Krugman <(E-Mail Removed)> wrote:
>
> bless \undef, 'Foo' ===> Modification of a read-only value attempted
> bless \\undef, 'Foo' ===> [ no error ]
> bless \1, 'Foo' ===> Modification of a read-only value attempted
> bless \\1, 'Foo' ===> Modification of a read-only value attempted
>
> Why are the behaviors for undef and 1 different?


I don't know why they behave the precise way they do, but I can rationalize
why they don't behave the same way. 1 is a constant, while undef is
actually a function invokation, even thought it is often used as if it were
a constant.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      03-15-2005
<(E-Mail Removed)> wrote in comp.lang.perl.misc:
> J Krugman <(E-Mail Removed)> wrote:
> >
> > bless \undef, 'Foo' ===> Modification of a read-only value attempted
> > bless \\undef, 'Foo' ===> [ no error ]
> > bless \1, 'Foo' ===> Modification of a read-only value attempted
> > bless \\1, 'Foo' ===> Modification of a read-only value attempted
> >
> > Why are the behaviors for undef and 1 different?

>
> I don't know why they behave the precise way they do, but I can rationalize
> why they don't behave the same way. 1 is a constant, while undef is
> actually a function invokation, even thought it is often used as if it were
> a constant.


Ah, but being a function doesn't prevent it from returning a read-only
value, or an alias to one. Constants (as in the pragma) do that, and
so does

sub const { return $_ for 3 }

Anno
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
reference to anonymous scalar Peng Yu Perl Misc 1 06-05-2010 07:00 PM
Is this a local anonymous class or a member anonymous class Reporter Java 3 05-12-2007 05:23 AM
Replace scalar in another scalar Mark Perl Misc 4 01-27-2005 02:48 PM
Shorthand for($scalar) loops and resetting pos($scalar) Clint Olsen Perl Misc 6 11-13-2003 12:50 AM



Advertisments