Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: perl6 phasers and a 1 liner

by Laurent_R (Canon)
on Feb 13, 2018 at 21:49 UTC ( [id://1209088]=note: print w/replies, xml ) Need Help??


in reply to perl6 phasers and a 1 liner

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1209088]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-25 21:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found