SPUG:Best One-Liners and Scripts for UNIX
Cantrall, Christopher W
christopher.w.cantrall at boeing.com
Thu Apr 17 15:38:10 CDT 2003
I occasionally copy code from perlmonks.org (there are special links to download code, but that isn't available on scratchpads). The problem is that I have my preferences set to precede all code samples with line numbers, so code (or anything formatted like code) looks like this:
001: EXTERIOR: DAGOBAH--DAY
002:
003: With Yoda strapped to his back, Luke climbs up one of the
004: many thick vines that grow in the swamp until he reaches the
005: Dagobah statistics lab. Panting heavily, he continues his
006: exercises--grepping, installing new packages, logging in as
007: root, and writing replacements for two-year-old shell
008: scripts in Python.
009:
010: YODA:
011: Code! Yes. A programmer's strength flows from code
012: maintainability. But beware of Perl. Terse syntax... more
013: than one way to do it... default variables. The dark side
014: of code maintainability are they. Easily they flow, quick to
015: join you when code you write. If once you start down the
016: dark path, forever will it dominate your destiny, consume
017: you it will.
018:
019: LUKE:
020: Is Perl better than Python?
021:
022: YODA:
023: No... no... no. Quicker, easier, more seductive.
024:
025: LUKE:
026: But how will I know why Python is better than Perl?
027:
028: YODA:
029: You will know. When your code you try to read six months
030: from now.
Which isn't so bad for reading jokes, but tends to make code do funny things. Like fail to compile.
So I banged out this Llama-level number stripper:
#!/usr/bin/perl -w
use strict;
my $filename = shift @ARGV;
open (FH, $filename) or die $!;
my @file = <FH>;
my @out;
push @out, "#!/usr/bin/perl\nuse strict;\n\n";
foreach my $line (@file) {
$line =~ s/^\s*\d{3}:\s//;
push @out, $line;
}
print @out;
It's the sort of thing I love about perl: solve a quick problem in a readable, maintable way. And I don't need to add the she-bang line manually.
________________________________________
Christopher Cantrall
Structural Engineer, 767 Fuselage
phone: 425-342-4131
fax: 425-717-3174
M/C 0Y-12 -- 40-83 E7
Christopher.W.Cantrall at Boeing.com
> -----Original Message-----
> From: Tim Maher [mailto:tim at consultix-inc.com]
> Sent: Wednesday, April 16, 2003 5:50 PM
> To: spug-list at pm.org
> Subject: SPUG:Best One-Liners and Scripts for UNIX
>
>
> SPUGsters,
>
> Seems like most people stayed home to work on their taxes last
> night, judging from the turnout at our April SPUG meeting 8-{
>
> But I gave my talk anyway -- well, at least *part* of it.
>
> It took me the full two hours to cover "Perl as a Better
> Grep, Sed, and Awk", so I didn't delve into the other
> advertised topics, which were:
>
> * 5 Perl One-liners All Unix/Linux Users Should Know
> * 3 Perl Scripts UNIX/Linux Users Shouldn't Live Without
> * How Perl's Looping Facilities Compare to the Shell's
>
> I've got my own ideas about what the "5" and the "3" are, but I'm
> very interested to get input from you folks on your favorites!
>
> So please give some thought as to what Perl one-liners or small
> scripts you value for your UNIX/Linux work, and post them to the
> list so we can discuss them. If I like your submissions, with
> your permission, I'll include them in my book, and you'll get
> "your foot-noted". 8-}
More information about the spug-list
mailing list