Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

$_[0] is not a copy. It's literally the same scalar that was passed as an argument. That means the first 1+1 produced a read-only scalar and the second one didn't.

We can see some differences in the generated code.

$ perl -MO=Concise,-exec -MDevel::Peek -we'Dump 1; sub foo{Dump $_[0]; +$_[0]=1} foo(1)' 1 <0> enter 2 <;> nextstate(main 44 -e:1) v:{ 3 <$> const[IV 1] s 4 <2> Devel_Peek_Dump vK/1 5 <;> nextstate(main 46 -e:1) v:{ 6 <0> pushmark s 7 <$> const[IV 1] sM 8 <#> gv[IV \&main::foo] s 9 <1> entersub vKS a <@> leave[1 ref] vKP/REFC -e syntax OK
$ perl -MO=Concise,-exec -MDevel::Peek -we'Dump 1+1; sub foo{Dump $_[0 +];$_[0]=1} foo(1+1)' 1 <0> enter 2 <;> nextstate(main 44 -e:1) v:{ 3 <$> const[IV 2] s/FOLD 4 <2> Devel_Peek_Dump vK/1 5 <;> nextstate(main 46 -e:1) v:{ 6 <0> pushmark s 7 <$> const[IV 2] sM/FOLD 8 <#> gv[IV \&main::foo] s 9 <1> entersub vKS a <@> leave[1 ref] vKP/REFC -e syntax OK

So it appears that a folded constant (FOLD) in lvalue context (M) returns a writable scalar.

We can also see the difference when using the referencing operator or foreach, as they also also impose an lvalue context.

$ perl -MDevel::Peek -e'Dump(${\1})' SV = IV(0x8549e4010) at 0x8549e4020 REFCNT = 1 FLAGS = (IOK,READONLY,PROTECT,pIOK) IV = 1 $ perl -MDevel::Peek -e'Dump(${\(1+1)})' SV = IV(0x7ed5d4d610) at 0x7ed5d4d620 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 2 $ perl -MDevel::Peek -e'Dump($_) for 1' SV = IV(0x151c4b8240) at 0x151c4b8250 REFCNT = 2 FLAGS = (IOK,READONLY,PROTECT,pIOK) IV = 1 $ perl -MDevel::Peek -e'Dump($_) for 1+1' SV = IV(0x567114cd90) at 0x567114cda0 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 2

I believe this is intentional. Addition normally produces a writable scalar, and those semantics are being preserved even when the operands of the addition are constants.


In reply to Re: When aliasing sub arguments to @_ elements, PADTMP, READONLY flags are copied inconsistently by ikegami
in thread When aliasing sub arguments to @_ elements, PADTMP, READONLY flags are copied inconsistently by vr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2021-12-01 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?