in reply to perl6 phasers and a 1 liner
As you can see, the declaration of the $c variable can be before the BEGIN block.perl6 -e 'my $c; BEGIN { $c = 1 }; say $c;' 1
Or you can have:
Here we see that the BEGIN phaser (or other phasers) does not need to be a block, it can be a simple statement.perl6 -e 'my $c; BEGIN $c = 1; say $c;' 1
In some cases at least, the BEGIN phaser can be even a simple expression:
Or you can do even this:perl6 -e 'my $c = BEGIN 3 - 2; say $c;' 1
Now a more complete example somewhat looking like what you're trying to do. I have a short Perl 6 script (file while_done.pl6) in my current directory and want to print the lines where either of the words say and while is present.perl6 -e 'my $c; say $c; BEGIN $c = 1' 1
And this also works the same way:perl6 -ne 'my $c; BEGIN {$c = qw<while say>.Set}; my $line = $_; $line +.say if $_ (elem) $c for $line.words' while_done.pl6 while True { say $line; say 'Done!';
I guess the line would be printed twice if either of the searched words appears twice, but that was just a quick example.perl6 -ne 'my $c = BEGIN qw<while say>.Set; my $line = $_; $line.say i +f $_ (elem) $c for $line.words' while_done.pl6
BTW, as already mentioned by Anonymous Monk, note that it is probably better to populate a set within the BEGIN block, rather than an array, because, otherwise, Perl will have to coerce the array into a set for each call to the (elem) operator and this might be inefficient if your input file is large.
|
|---|