How to make a blessable anonymous scalar ref?

Discussion in 'Perl Misc' started by J Krugman, Mar 13, 2005.

  1. J Krugman

    J Krugman Guest

    [] 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.
     
    J Krugman, Mar 13, 2005
    #1
    1. Advertising

  2. 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=71423350343770280161397026330337371139054411854220053437565440;
    $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
     
    Tassilo v. Parseval, Mar 13, 2005
    #2
    1. Advertising

  3. J Krugman

    Peter Scott Guest

    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/
     
    Peter Scott, Mar 13, 2005
    #3
  4. J Krugman

    kj Guest

    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.
     
    kj, Mar 13, 2005
    #4
  5. 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!
     
    Brian McCauley, Mar 13, 2005
    #5
  6. J Krugman

    kj Guest

    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.
     
    kj, Mar 13, 2005
    #6
  7. J Krugman

    Anno Siegel Guest

    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
     
    Anno Siegel, Mar 14, 2005
    #7
  8. J Krugman

    Anno Siegel Guest

    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
     
    Anno Siegel, Mar 14, 2005
    #8
  9. J Krugman

    Anno Siegel Guest

    Abigail <> wrote in comp.lang.perl.misc:
    > Anno Siegel (-berlin.de) wrote on MMMMCCXIII
    > September MCMXCIII in <URL:news:d13hgq$9he$-Berlin.DE>:
    > () 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
     
    Anno Siegel, Mar 14, 2005
    #9
  10. 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.
     
    Brian McCauley, Mar 14, 2005
    #10
  11. J Krugman

    Guest

    J Krugman <> 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
     
    , Mar 14, 2005
    #11
  12. J Krugman

    kj Guest

    In <d14hre$43v$> Brian McCauley <> 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.
     
    kj, Mar 14, 2005
    #12
  13. J Krugman

    J Krugman Guest

    In <20050314145658.526$> writes:

    >J Krugman <> 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.
     
    J Krugman, Mar 14, 2005
    #13
  14. J Krugman

    kj Guest

    In <d13hm0$9he$-Berlin.DE> -berlin.de (Anno Siegel) writes:

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



    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.
     
    kj, Mar 15, 2005
    #14
  15. J Krugman

    Anno Siegel Guest

    J Krugman <> wrote in comp.lang.perl.misc:
    > In <20050314145658.526$> 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
     
    Anno Siegel, Mar 15, 2005
    #15
  16. J Krugman

    Anno Siegel Guest

    kj <> wrote in comp.lang.perl.misc:
    > In <d13hm0$9he$-Berlin.DE>
    > -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
     
    Anno Siegel, Mar 15, 2005
    #16
  17. J Krugman

    Guest

    J Krugman <> 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
     
    , Mar 15, 2005
    #17
  18. J Krugman

    Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > J Krugman <> 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
     
    Anno Siegel, Mar 15, 2005
    #18
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dmitri Zakharov
    Replies:
    2
    Views:
    6,559
    Dmitry Roslyakov
    Jun 11, 2004
  2. Clint Olsen
    Replies:
    6
    Views:
    405
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  3. Dmitri Zakharov

    Expanding scalar hash element in array ref.

    Dmitri Zakharov, Jun 10, 2004, in forum: Perl Misc
    Replies:
    5
    Views:
    155
    Dmitry Roslyakov
    Jun 11, 2004
  4. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    193
    Arndt Jonasson
    Jan 27, 2005
  5. Justin C
    Replies:
    1
    Views:
    202
    Justin C
    Oct 7, 2013
Loading...

Share This Page