Perl6 Unable to initialize state variable. Help is needed

I want to use a single-line font to print the middle part of a file, using a state variable to indicate whether the current line is in the desired section of the file. But I can not initialize the state variable. Initialization is so simple and I just can't find what the problem is. Please help. Thank.

The file is named testFile.txt and has the following lines:

section 0; state 0; not needed
= start section 1 =
state 1; needed
= end section 1 =
section 2; state 2; not needed

And my single line file

cat testFile.txt | perl6 -ne ' state $x = 0; say "$x--> "; if $_ ~~ m/ "start" / { $x=1; }; if $x == 1 { .say; }; if $_ ~~ m/ "end" / { $x = 2; }'

And the result showed that $ x = 0 does not perform initialization:

Use of uninitialized value $x of type Any in string context.
Methods .^name, .perl, .gist, or .say can be used to stringify it to something meaningful.
  in block  at -e line 1
--> 
Use of uninitialized value of type Any in numeric context
  in block  at -e line 1
Use of uninitialized value $x of type Any in string context.
Methods .^name, .perl, .gist, or .say can be used to stringify it to something meaningful.
  in block  at -e line 1
--> 
= start section 1 =
1--> 
state 1; needed
1--> 
= end section 1 =
2--> 
2--> 
+4
source share
1 answer

It looks like a mistake to me. Apparently -nmisconfigured the lexical environment.

, , do { ... } { ... }.

, , , , --,

cat testFile.txt | perl6 -ne '.say if / "start" / ff / "end" /'
+5

Source: https://habr.com/ru/post/1664263/


All Articles