How to make a blessable anonymous scalar ref?

J

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

TIA,

jill
 
T

Tassilo v. Parseval

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
 
P

Peter Scott

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';
 
K

kj

In said:
[] and {} are blessable anonymous refs. I.e., both
bless [], 'Foo';

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
 
B

Brian McCauley

kj said:
[] and {} are blessable anonymous refs. I.e., both

bless [], 'Foo';


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!
 
K

kj

In said:
kj wrote:
[] and {} are blessable anonymous refs. I.e., both

bless [], 'Foo';


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.

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
 
A

Anno Siegel

J Krugman said:
[] 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
 
A

Anno Siegel

Abigail said:
Anno Siegel ([email protected]) wrote on MMMMCCXIII
September MCMXCIII in <URL:() > 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
 
B

Brian McCauley

kj said:
kj 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".


\$_ 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.


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.
 
X

xhoster

J Krugman said:
[] 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
 
J

J Krugman

In said:
J Krugman said:
[] 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 ]
 
K

kj

In said:
J Krugman said:
[] 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
 
A

Anno Siegel

J Krugman said:
[...]
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
 
X

xhoster

J Krugman said:
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
 
A

Anno Siegel

J Krugman said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top