Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Argument list item alias-ing

by Ido (Hermit)
on Apr 16, 2003 at 10:47 UTC ( [id://250834]=perlquestion: print w/replies, xml ) Need Help??

Ido has asked for the wisdom of the Perl Monks concerning the following question:

sub m{ $_[0]=~s///; } m("String");
Will, obviously, not work, as it tries to modify a read-only value.
Same is the case with:
*_=\"Hello"; s///;

But:
sub m{ local *_=\$_[0]; s///; } m("String");
Will run. Which is weird, as $_ is an alias to $_[0] which is an alias to a readonly value. $_[0] won't change, but $_ will, which is again weird, as $_ should be an alias to it. Can someone clarify what's going on to me?

Replies are listed 'Best First'.
Re: Argument list item alias-ing
by broquaint (Abbot) on Apr 16, 2003 at 11:02 UTC
    Will run. Which is weird
    You're actually matching the string as you're using the balanced delimiters syntax of a regex match e.g
    shell> perl -MO=Deparse - sub m{ local *_=\$_[0]; s///; } m("String"); sub m { local *_ = \$_[0]; s///; } /"String"/; - syntax OK
    But if you change that sub name to replace then you get the expected error message
    sub replace { local *_=\$_[0]; s///; } replace("String"); Modification of a read-only value attempted at pmsopw_250834.pl line 3 +.

    HTH

    _________
    broquaint

Re: Argument list item alias-ing
by Ido (Hermit) on Apr 16, 2003 at 11:11 UTC
    Oooops. Wrong subname..Sorry about that... Anyway, here(ActivePerl 5.8.0, Build 805), neither:
    sub subname{ local *_=\$_[0]; s///; } subname("String");
    Gives any error message. So..is it a bug?A feature?Another stupid mistake of myself?
      Sounds like a bug to me as I get an error message here
      sub subname { local *_=\$_[0]; s///; } subname("String"); Modification of a read-only value attempted at - line 3.
      This error should occurs because you're 'aliasing' $_ to $_[0], which essentially equates to both pointing to the same scalar value e.g
      sub { print "\$_[0]: ",\$_[0], $/; local *_= \$_[0]; print "\$_: ",*main::_{SCALAR}, $/; s///; }->("foo"); __output__ $_[0]: SCALAR(0x8107ec8) $_: SCALAR(0x8107ec8) Modification of a read-only value attempted at - line 5.

      HTH

      _________
      broquaint

Re: Argument list item alias-ing
by nothingmuch (Priest) on Apr 16, 2003 at 12:14 UTC
    That shouldn't happen. If you're dealing with possible read only's perhaps you should consider using Scalar::Util::readonly();. The behaviour you have witnessed is, i think, some kind of bug. Any guts guru care to share h(er|is) thoughts?

    -nuffin
    zz zZ Z Z #!perl

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://250834]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2024-03-28 14:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found