Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Perl as a second language

by sfink (Deacon)
on Oct 03, 2007 at 20:47 UTC ( #642506=note: print w/replies, xml ) Need Help??

in reply to Perl as a second language

First, there is hardly anything that you can usefully do with Perl after one day of learning. Perl is a five minutes a day language. Its syntax is different enough from other things that if you don't use it frequently, the memory of how to do things will evaporate from your brain.

I'm not saying that what you are attempting is impossible; I think you have a great idea as long as you approach it as a teaser that is going to interest them enough to start spending those 5 minutes a day.

That said, I'll try coming up with some examples. Note that these will mostly be one-liners, because that's mostly what is useful on a day-to-day basis. Unfortunately, they use lots of magic command-line options, so they tend to make Perl seem even more magic and filled with special cases than it actually is.

  • Grabbing out fields from a text file. Here's my standard /proc-based process memory monitor: while true; do perl -lne 'print $1 if /VmSize:\s*(\S.*)/' /proc/(pid)/status; sleep 5; done
  • The print $1 if /.../ idiom is nice because you can select lines as well as the field you care about. Here's something that will print out the GET URLs from an Apache access_log file: perl -lne 'print $1 if /GET (\S+)/' access_log
  • Summing up a bunch of numbers. This will sum up the sizes of all *.txt files in a directory: ls -l *.txt | perl -lane 'print $s += $F[4]' or with a nice output: ls -l *.txt | perl -lane '$s =+ $F[4]; END { print "Total size: $s" }'
  • Note that the above can fail with longer usernames or groups. If that is a potential problem, then these would be more correct: ls -ln *.txt | perl -lane 'print $s += $F[4]' or perl -le 'print $s += (-s $_) foreach @ARGV' *.txt or even perl -le 'print $s += (-s $_) foreach glob("*.txt")'
  • Ok, the previous example really isn't that good, since it can just be done with wc -c *.txt. Then again, maybe it's a good lead-in to a discussion of the usefulness of knowing how to use a hammer well.
  • Here's one I used recently: we have directories containing C++ files. The usage pattern is supposed to be acyclic (if files in directory A include files in directory B, then nothing in B should directly or indirectly include files in A.) The first step was to dump out all of the include relationships: perl -lne 'print "$ARGV -> $1" while m!^#\s*include\s+["<](\w+)/!g' */*.cpp */*.h | sort -u (the includes look like #include "Ac/Insert.cpp"). Step 2 was to boil it down to just the directory usage lines; step 3 was to feed it to dot to display the graph nicely. (Actually, I had an intermediate step where I simplified the relationships to remove indirect links, but that was a much more involved program. We have a lot of packages.)
  • I once wrote up a quick script to cross-verify Java API calls that were documented as deprecated but sometimes not marked as deprecated in the code. Or vice versa. I forget.
  • Do a diff between two (possibly compressed) tarballs. Or rpms. (I have simple utility scripts I wrote for that, one 65 lines, the other 39. I think they'd both be understandable and maybe writable after a day with Perl.)
  • Recursively add a directory tree to CVS.
  • I could probably just look through my ~/bin directory for examples, but I think I'll stop now.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2022-08-15 13:17 GMT
Find Nodes?
    Voting Booth?

    No recent polls found