vr has asked for the wisdom of the Perl Monks concerning the following question:
I wrote a fragment of code dealing with passing pointers to pointers to data for some FFI calls. Reducing it now to SSCC(?)E: if written as
pack 'P', pack 'P', $data
then Perl warns "Attempt to pack pointer to temporary value", so it's easy to debug. But in my case, it was something like:
sub foo { pack 'P', $_[0] } foo pack 'P', $data;
-- no warnings, correct result for single call, but incorrect if return values are not used immediately but e.g. stored. Then I ran some tests:
>perl -MDevel::Peek -we "Dump 1; sub foo{Dump $_[0];$_[0]=1} foo(1)" SV = IV(0x653478) at 0x653488 REFCNT = 1 FLAGS = (IOK,READONLY,PROTECT,pIOK) IV = 1 SV = IV(0x653358) at 0x653368 REFCNT = 1 FLAGS = (IOK,READONLY,PROTECT,pIOK) IV = 1 Modification of a read-only value attempted at -e line 1. >perl -MDevel::Peek -we "Dump 1+1; sub foo{Dump $_[0];$_[0]=1} foo(1+1 +)" SV = IV(0x2612cf8) at 0x2612d08 REFCNT = 1 FLAGS = (PADTMP,IOK,READONLY,PROTECT,pIOK) IV = 2 SV = IV(0xfcb140) at 0xfcb150 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 2
Why is not PADTMP flag copied to $_[0], and why, if present, it also prevents copying READONLY?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: When aliasing sub arguments to @_ elements, PADTMP, READONLY flags are copied inconsistently
by ForgotPasswordAgain (Vicar) on Jan 18, 2020 at 18:36 UTC | |
by swl (Prior) on Jan 18, 2020 at 22:18 UTC | |
Re: When aliasing sub arguments to @_ elements, PADTMP, READONLY flags are copied inconsistently
by ikegami (Patriarch) on Jan 20, 2020 at 16:16 UTC | |
by vr (Curate) on Jan 20, 2020 at 18:59 UTC |