Merit Network
Can't find what you're looking for? Search the Mail Archives.
  About Merit   Services   Network   Resources & Support   Network Research   News   Events   Home

Discussion Communities: Merit Network Email List Archives

North American Network Operators Group

Date Prev | Date Next | Date Index | Thread Index | Author Index | Historical

Re: casual programming and network operations

  • From: Andy Dills
  • Date: Sun Sep 08 12:36:12 2002

On 8 Sep 2002, Adam Atkinson wrote:

> Does what I propose seem more useful or dangerous? I think my main
> aim is to show useful non-artificial examples as quickly as possible.
> Talking about loop structures, scalars vs arrays vs hashes etc. seems
> like exactly the wrong way to do.
>
> It seems to me that
>
> while (<>) {
>
> (stuff)
>
> }
>
> can be used as the basis of useful stuff. (Also, one of the main
> messages would be that the most important skill is taking an existing
> script and tweaking it, even if you don't know how some of it works.)
>
> Of course, I'd tell people that knowing other features as well might
> allow more to be done, but I wouldn't want to push people into using
> modules, objects, references, etc.


You might want to spend a little time getting them familiar with perl's
command line switches, as there lies the granddaddy of quick perl
usefulness.

For instance, you're not the only one who finds 90% of their scripts end
up in a while(<>) loop. It's so common they created command line switches
to allows you to easily embed a block of code inside that very loop.
perl -p -e '<block of perl code>' <filename> places the block of perl code
inside the while (<>) loop you mention, with the supplied filename(s)
being the stdin. Even cooler is adding -i to that, which specifies that
files processed by the <> construct are to be edited in-place. You can
even specify an extension for backup of files. For instance, this is
commonly used way to apply an RE against an entire directory, with
creating a filename.bak of the previous contents:

cd /var/named
perl -pi'.BAK' -e 's/1\.2\.3\.4/4.5.6.7/' *

(or perhaps you're changing MX records)

perl -pi'.BAK' -e 's/MX\s*(\d*)\s*mail.oldcompany.com/MX $1 mail.newcompany.com/' *

<checks to see if everything went well>

rm *.BAK

Read `man perlrun`, it details some of the most useful and least used
features.

Andy

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Andy Dills                              301-682-9972
Xecunet, LLC                            www.xecu.net
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Dialup * Webhosting * E-Commerce * High-Speed Access





Discussion Communities


About Merit | Services | Network | Resources & Support | Network Research
News | Events | Contact | Site Map | Merit Network Home


Merit Network, Inc.