Perl in its set­ting

Linux Format - - TUTORIAL -

Perl ( see LXF145/151/206) is some­times re­ferred to as a ‘glue lan­guage’. That is, the aim was to stick to­gether oth­er­wise in­com­pat­i­ble pieces to pro­duce a work­ing some­thing. If this sounds like a shell script, it is. Of course, Perl wasn’t cre­ated in a vac­uum, and its author, Larry Wall, thinks the lan­guage bor­rows “some of the best fea­tures of sed, Awk, and sh”. Take hashes. Of­fi­cially called as­so­cia­tive ar­rays, they are the same as in Awk. There are less ob­vi­ous sim­i­lar­i­ties as well. Re­mem­ber how you do a sum­ma­tion in Awk? A typ­i­cal so­lu­tion would be: awk ‘{sum += $1}; END {print sum}’

$1 ex­ists in Perl as well, but that’s a false com­par­i­son. Here, it refers to the first record’s field; in Perl, it’s the first cap­ture group in a reg­u­lar ex­pres­sion. Yet Perl also has a no­tion of in­put records and out­put fields. END a block of code, which gets ex­e­cuted as late as pos­si­ble in both lan­guages.

Per­haps you don’t use Awk too of­ten, but you al­most cer­tainly know grep. It ex­ists as a func­tion in Perl, but it’s not lim­ited to reg­u­lar ex­pres­sions. One can use the grep func­tion to fil­ter a list by an ar­bi­trary code block.

This com­bi­na­tional na­ture makes Perl a rather pow­er­ful lan­guage, yet no sin­gle pack­age can do ev­ery­thing. Third-party mod­ules in Perl come through CPAN ( www.cpan.org) – a Com­pre­hen­sive Perl Archive Net­work. If you need any­thing in Perl, be it a PDF reader or an Awk-to-Perl trans­la­tor, this should be your first stop.

Newspapers in English

Newspapers from Australia

© PressReader. All rights reserved.