Вопрос по regex, perl – Perl: Почему eval '/ (…) /' не устанавливает $ 1?

13

Error: User Rate Limit Exceeded

perlop а такжеperlreError: User Rate Limit Exceeded

Например:

 use strict; use warnings;
 $_ = "hello";
 eval '/(.*)/';
 print "GOT: $1\n";

дает:

Use of uninitialized value $1 in concatenation (.) or string at -e line 1.
GOT:

Более краткая демонстрация:

perl -we '$_="foo"; eval q(/(.*)/;) ; print "GOT:$1\n";'

Ваш Ответ

3   ответа
2

Error: User Rate Limit Exceeded
Error: User Rate Limit ExceededdeclaringError: User Rate Limit ExceededpackageError: User Rate Limit Exceededneed for declaration or full qualificationError: User Rate Limit ExceededstrictError: User Rate Limit ExceededmyError: User Rate Limit ExceededourError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded$1Error: User Rate Limit Exceeded$&Error: User Rate Limit ExceededglobalError: User Rate Limit ExceededlexicalError: User Rate Limit Exceeded
10

Error: User Rate Limit ExceededlocalError: User Rate Limit Exceededperlvar 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 [...]

Error: User Rate Limit ExceededError: User Rate Limit Exceeded.


Error: User Rate Limit ExceededlocalError: User Rate Limit Exceededperldoc -f eval (5.12.4)Error: User Rate Limit Exceeded

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

Error: User Rate Limit Exceeded$1, $&Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededevalError: User Rate Limit ExceededevalError: User Rate Limit Exceeded

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;
Error: User Rate Limit Exceeded$1Error: User Rate Limit Exceeded
3

$1

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

Похожие вопросы