in reply to Re^4: Please help me understand string eval better
in thread Please help me understand string eval better
It looks like your question isn't so much about eval than about magical string-to-number conversion.
With regards to option 3 one difference seems to be that $$var will only be replaced by it's value when the eval is invoked. Is there any other behavioural difference when using option 3 instead of option 1?
No, there is no difference. It's all a matter of what eval gets to evaluate.
$var = '09'; $trans = '/3+5'; eval "$var$trans"; # eval gets '09/3+5' # Illegal octal digit '9' eval $var.$trans; # eval gets '09/3+5' # Illegal octal digit '9' eval '$var'.$trans; # eval gets '$var/3+5' # result: 8
What happens in the last case? Well, eval evaluates $var in a numerical operation (division), in which case the number slot of the variable is used. Its string value still is 09, but its numerical value is 9.
use Devel::Peek; $var = '09'; Dump $var; $var + 0; Dump $var; $var / 3; Dump $var; __END__ SV = PV(0x2e352a0) at 0x2e24fc0 REFCNT = 1 FLAGS = (POK,IsCOW,pPOK) PV = 0x2e503f0 "09"\0 CUR = 2 LEN = 10 COW_REFCNT = 0 SV = PVIV(0x254b610) at 0x2e24fc0 REFCNT = 1 FLAGS = (IOK,POK,IsCOW,pIOK,pPOK) IV = 9 PV = 0x2e503f0 "09"\0 CUR = 2 LEN = 10 COW_REFCNT = 0 SV = PVNV(0x2ddfc90) at 0x2e24fc0 REFCNT = 1 FLAGS = (IOK,NOK,POK,IsCOW,pIOK,pNOK,pPOK) IV = 9 NV = 9 PV = 0x2e503f0 "09"\0 CUR = 2 LEN = 10 COW_REFCNT = 0
As you can see, by adding 0 to $var, the integer slot (IV) of the variable is populated with 9. Dividing $var fills the number slot (NV), so the variable is fit for division. The string value (PV) is still 09.
|
|---|