You might consider these examples for some clues:
perl6 -e 'my $c; BEGIN { $c = 1 }; say $c;' 1
As you can see, the declaration of the $c variable can be before the BEGIN block.

Or you can have:

perl6 -e 'my $c; BEGIN $c = 1; say $c;' 1
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:

perl6 -e 'my $c = BEGIN 3 - 2; say $c;' 1
Or you can do even this:
perl6 -e 'my $c; say $c; BEGIN $c = 1' 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 -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!';
And this also works the same way:
perl6 -ne 'my $c = BEGIN qw<while say>.Set; my $line = $_; $line.say i +f $_ (elem) $c for $line.words' while_done.pl6
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 reply to Re: perl6 phasers and a 1 liner by Laurent_R
in thread perl6 phasers and a 1 liner by RichardJActon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.