Come for the quick hacks, stay for the epiphanies. | |
PerlMonks |
Re: perl6 phasers and a 1 linerby Laurent_R (Canon) |
on Feb 13, 2018 at 21:49 UTC ( [id://1209088]=note: print w/replies, xml ) | Need Help?? |
You might consider these examples for some clues:
As you can see, the declaration of the $c variable can be before the BEGIN block. 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. In some cases at least, the BEGIN phaser can be even a simple expression: Or you can do even this: 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. And this also works the same way: I guess the line would be printed twice if either of the searched words appears twice, but that was just a quick example. 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.
In Section
Seekers of Perl Wisdom
|
|