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?

 
 
J Krugman
Guest
Posts: n/a
 
      03-13-2005



[] 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".

TIA,

jill

--
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
 
 
 
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      03-13-2005
Also sprach J Krugman:

> [] 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".


Perl lacks an anonymous scalar reference constructor, so in order to be
not read-only, the scalar ref must initially refer to a readable scalar
which then necessarily has a name.

Tassilo
--
use bigint;
$n=71423350343770280161397026330337371139054411854 220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($ m+=<=200);
 
Reply With Quote
 
 
 
 
Peter Scott
Guest
Posts: n/a
 
      03-13-2005
On Sun, 13 Mar 2005 12:04:11 +0000, J Krugman wrote:
> 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 \do { my $foo }, 'Foo';

--
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/

 
Reply With Quote
 
kj
Guest
Posts: n/a
 
      03-13-2005
In <d11a7r$8rm$> J Krugman <> writes:

>[] 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".


\$_ is not exactly anonymous (after all, it's the scalar formerly
known as foo), but at least you wouldn't have to go through the
mind-scarring experience of having to come up with a name for no
good reason.

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
 
Brian McCauley
Guest
Posts: n/a
 
      03-13-2005


kj wrote:

> In <d11a7r$8rm$> J Krugman <> writes:
>
>
>>[] 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".

>
>
> \$_ is not exactly anonymous (after all, it's the scalar formerly
> known as foo), but at least you wouldn't have to go through the
> mind-scarring experience of having to come up with a name for no
> good reason.


Boggle!

But then you are blessing \$foo (or whatever $_ happens to be aliased to
at the time). That's hideous!

 
Reply With Quote
 
kj
Guest
Posts: n/a
 
      03-13-2005
In <d12530$t0g$> Brian McCauley <> writes:

>kj wrote:


>> In <d11a7r$8rm$> J Krugman <> writes:
>>
>>
>>>[] 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".

>>
>>
>> \$_ is not exactly anonymous (after all, it's the scalar formerly
>> known as foo), but at least you wouldn't have to go through the
>> mind-scarring experience of having to come up with a name for no
>> good reason.


>Boggle!


>But then you are blessing \$foo (or whatever $_ happens to be aliased to
>at the time). That's hideous!


Aw, c'mon, it's not so bad! All we need is one of all those scalars
who are standing around doing little or nothing, to serve as a
reference in a little blessing ceremony. Now, is that so hard?

OK, how about \$% ?

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-14-2005
Peter Scott <> wrote in comp.lang.perl.misc:
> On Sun, 13 Mar 2005 12:04:11 +0000, J Krugman wrote:
> > 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 \do { my $foo }, 'Foo';


Why the do{}?

bless \ my $foo, 'Foo';

works just as well.

Anno
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      03-14-2005
J Krugman <> 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;

Anno
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      03-14-2005
Abigail <> wrote in comp.lang.perl.misc:
> Anno Siegel () wrote on MMMMCCXIII
> September MCMXCIII in <URL:news:d13hgq$9he$>:
> () Peter Scott <> wrote in comp.lang.perl.misc:
> () > On Sun, 13 Mar 2005 12:04:11 +0000, J Krugman wrote:
> () > > 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 \do { my $foo }, 'Foo';
> ()
> () Why the do{}?
> ()
> () bless \ my $foo, 'Foo';
> ()
> () works just as well.
>
>
> Without the do, the scalar isn't anonymous - it's known as '$foo'.
> The do creates a scope, and the scalar no longer has a name.


Oh, okay...

Usually this happens in a ->new method (under whatever name), whose
body already provides a sufficiently small scope.

Anno
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      03-14-2005
kj wrote:

> In <d12530$t0g$> Brian McCauley <> writes:
>
>
>>kj wrote:

>
>
>>>In <d11a7r$8rm$> J Krugman <> writes:
>>>
>>>
>>>
>>>>[] 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".
>>>
>>>
>>>\$_ is not exactly anonymous (after all, it's the scalar formerly
>>>known as foo), but at least you wouldn't have to go through the
>>>mind-scarring experience of having to come up with a name for no
>>>good reason.

>
>
>>Boggle!

>
>
>>But then you are blessing \$foo (or whatever $_ happens to be aliased to
>>at the time). That's hideous!

>
>
> Aw, c'mon, it's not so bad!


No, it really is very very bad.


for ( @some_array ) {
my $object = SomeThing->new; # Corrupts @some_array
}

sub SomeThing::new {
bless \$_;
}

> All we need is one of all those scalars
> who are standing around doing little or nothing, to serve as a
> reference in a little blessing ceremony.


No, this is completely bogus. You need a modifyable scalar value that
is not used past, present, or future for anything else - not even
subsquent iterations of the same code.

> Now, is that so hard?


No it's no hard, it's trivial do{\my $o}.

> OK, how about \$% ?


Now you are just being silly.

 
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
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57