Dear Larry,

Thank you so much for such a complete reply!

Not to keep score here, it seems like 9/10 Perl5 'one-liner tricks'
are baked into Raku from the language's inception. [ The only feature
that might be missing is 'Trick #3' where Perl5 modifies a file
in-place at the bash command line (and can create a backup file
beforehand) ].

Two notes, inline:

>
> >    Trick #9: BEGIN and END
> >
> >        BEGIN { ... } and END { ... } let you put code that gets run entirely
> >        before or after the loop over the lines.
> >
> >        For example, I could sum the values in the second column of a CSV
> >        file using:
> >
> >        perl -F, -lane '$t += $F[1]; END { print $t }'
>
> Same trick, except you can omit the brackets:
>
>      raku -ne 'my $t += [1] given .split(","); END say $t'
>
> Note the 'my' is required because strict is the default.

The way I got this to work in Raku was to put a period before the
indexing '[1]', otherwise it appears Raku returns the total number of
lines read:

raku -ne 'my $t += .[1] given .split(","); END say $t'

The two lines below work also (TMTOWTDI), although one or both might
be less readable than Larry's code above. However, the second
one-liner below might look very familiar to R-users
(https://www.r-project.org), because it has a method call with
arguments enclosed by parentheses, followed directly by a
post-circumfix indexing call using square brackets:

raku -ne 'my $t += .split(",").[1]; END say $t'

#OR

raku -ne 'my $t += .split(",")[1]; END say $t'

>
> >    Trick #10: -MRegexp::Common
> >
> >        Using -M on the command line tells perl to load the given module
> >        before running your code. There are thousands of modules available
> >        on CPAN, numerous of them potentially useful in one-liners, but
> >        one of my favorite for one-liner use is Regexp::Common, which, as
> >        its name suggests, contains regular expressions to match numerous
> >        commonly-used pieces of data.
> >
> >        The full set of regexes available in Regexp::Common is available in
> >        its documentation, but here's an example of where I might use it:
> >
> >        Neither the ifconfig nor the ip tool that is supposed to replace it
> >        provide, as far as I know, an easy way of extracting information for
> >        use by scripts. The ifdata program provides such an interface, but
> >        isn't installed everywhere. Using perl and Regexp::Common, however,
> >        we can do a pretty decent job of extracing an IP from ips output:
> >
> >        ip address list eth0 | \
> >          perl -MRegexp::Common -lne 'print $1 if /($RE{net}{IPv4})/'
>
> I don't know if there's anything quite comparable.  And who's to say
> what's "common" anymore...   Certainly we have -M.  But Raku's regex
> and grammars are so much more powerful that these things are likely to
> kept in more specific Grammar modules anyway, or just hand-rolled for
> the purpose on the spot.
>
> >    ~nelhage Join the discussion Comments ( 7 )
>
> Larry

I took a quick look at Perl5's Regexp::Common module on CPAN and found
this list of patterns:  balanced, comment, delimited, lingua, list,
net, number, whitespace, zip. Many of the important patterns seem
baked into the Raku language already (whitespace, balanced,
delimited). I guess it's easy enough to load a Raku module providing
regex patterns in the same way as loading a Perl(5 or 7) module, since
the -M flag is identical. Or, someone savvy could load a Perl(5)
module in Raku via NativeCall.

But Raku one-liners with modules work fine. Below (if anyone wants
some code to play around with), I scraped the Raku.org home page using
Raku's "HTML::Strip" module. Works like a charm:

wget -O - "http://raku.org"; | raku -MHTML::Strip -ne 'strip_html($_).say'


Thank you again, Larry!

Best, Bill.

W. Michels, Ph.D.

Reply via email to