Perl: Why doesn't eval '/(…)/' set $1?

南楼画角 提交于 2019-11-29 13:14:09

Documentation proof that localized variables are the issue here is in perlvar of 5.14.0:

These variables are read-only and dynamically-scoped, unless we note otherwise.

The dynamic nature of the regular expression variables means that their value is limited to the block that they are in [...]

Note that this bit of documentation is absent from the 5.12.4 perldoc.


The problem is localized variables. My copy of perldoc -f eval (5.12.4) has this to say:

The assignment to $@ occurs before restoration of localised
variables, which means a temporary is required if you want to
mask some but not all errors: [...]

The manpage doesn't make an explicit statement for all such special global variables (like $1, $&, and probably others), but block localization and subsequent restoration is what appears to happen here.

The variables are assigned to inside the eval and the original values are restored once the eval block is left.

use strict; use warnings;
use Test::More;
use constant V => 'hello';

$_ = V;

note '*** block eval';
eval {
        is $_, V, 'input ok';
        /(.*)/;
        is $&, V, 'in eval'; is $1, V, 'in eval';
};
is $&, V, 'after eval'; is $1, V, 'after eval';

note '*** without eval';
is $_, V, 'input ok';
/(.*)/;
is $&, V; is $1, V;

done_testing;

The actual question is already answered, so here's the answer how you achieve the task.

Simply use the return values of eval, which in turn uses the return values of the match operator. $1 sucks, avoid.

use strict; use warnings;
$_ = "hello";
my @r = eval '/(.*)/';
# (
#     "hello"
# )

Any lexical variables declared within an eval will be lost after the eval ends.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!