Does Perl have PHP-like dynamic variables?

前端 未结 5 1635
北恋
北恋 2020-12-03 06:16

In PHP, I can write:

$vname = \'phone\';
$$vname = \'555-1234\';
print $phone;

... And the script will output \"555-1234\".

Is ther

5条回答
  •  -上瘾入骨i
    2020-12-03 06:24

    Read Mark-Jason Dominus's rants against doing this in Why it's stupid to `use a variable as a variable name'.

    You would limit the scope of your changes to $phone by starting the block with

    local $phone;
    

    or even

    local $$vname;
    

    (Though either changes $phone for any subs called from your block too, so it's not the same as the lexical scope of a my() declaration.)

提交回复
热议问题