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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.