SPUG:Best One-Liners and Scripts for UNIX

Cantrall, Christopher W christopher.w.cantrall at boeing.com
Mon Apr 21 12:26:04 CDT 2003


> There was one code sample posted early on in this thread that spent
> about 7 lines to implement the generic input reading loop that -n
> gives you for free; I followed up with a one-line version of that
> part, that showed it for what it really was, a Perl implementation
> of a simple sed editing command.  Is this magic?  Not in my book
> (if you'll pardon the pun); instead, it's just a worthwhile
> simplification that lets the reader focus on the data processing
> task, instead of having to wade through boilerplate infrastructure
> that has already been factored out by The Larry and memorialized in
> "-n".

When I wrote that, and later when I posted it, I had a feeling that it could be made into a one-liner, but I didn't know how clean that one-liner would be.  That example exemplifies for me one of the things I love about Perl - you can ignore the dirty, boring, and easy to mess up details if you want to: they're taken care of already.  


You want to loop through your file, substitute based upon a regex, and then print that file back out with the same name, but keep a backup?  No problem, use -p, and -i.bak on the command line.  You say you want to see where the magic comes from?  Read perldoc perlrun, it's all in there.  (Almost all, there are some tricks in perlfaq.)

You need to use a module to do that clever file manipulation?  OK, see the -M switch in perlrun, and you can use Quantum::Superpositions in a one-liner.  :)  I've seen it done on perlmonks. 

Heh, Randal Schwartz posted a snippet to perlmonks ( http://perlmonks.org/index.pl?node_id=224272 ) which could probably be a one-liner  -  it's HTML tidy using XML::LibXML.  Type one line into your shell, and *poof* you've cleaned up some HTML files.  Is that power?  I think that's a great chainsaw.  


> > What's the other way to say it? Write Once, Read Never?

Using command line switches and some shortcuts seems to be an intelligent use of programmer resources.  Why write (and debug) loops and file interaction if you can let the language take care of that for you?  Read how it performs those actions, then use that functionality.  (Read once, write many. :)  If you think your maintenance programmer won't know a trick, put in a pointer comment to some instruction (# see perldoc -f map).  


I've known that Perl was really cool, and as I learn more about it, I'm seeing more coolness.  And I'm realizing that I'm the kind of person who finds a programming language cool. :)  


________________________________________
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: SPUG-list-owner [mailto:tim at consultix-inc.com]
> Sent: Sunday, April 20, 2003 11:06 AM
> To: Bob Hiltner
> Cc: spug-list at pm.org
> Subject: Re: SPUG:Best One-Liners and Scripts for UNIX
> 
> 
> On Sat, Apr 19, 2003 at 01:22:59PM -0700, Bob Hiltner wrote:
> 
> > Seeing all the one-liners (not this post particularly) brings to
> > mind one of the great things about PERL. You don't need code
> > obfuscation utilities for IP protection. It's done automatically at
> > the source code level! ;^) What's the other way to say it? Write
> > Once, Read Never?

 ... (yadda, yadda, yadda :)

> > On a more serious note... that is one of the things that 
> does make me shy
> > away from getting into perl in a serious way... too much 
> magic.  This stuff
> > that (perhaps) makes sense to the initiated and the high 
> priests is like
> > reading binaries to the casual perler.   
> 
> I always wonder what people are specifically talking about when
> they see "magic" in the kind of code we've been slinging around here.
> 
> Is it magic that perl "-n" gives you a free loop?  If so, is it also
> magic that "grep -i" ignores case, or "sed 's///g'" does multiple
> substitutions?  Not in my book.  There's plenty of "magical"
> (surprising and automatic) activity in Perl, but I don't recall
> seeing any of it here.
> 
> There was one code sample posted early on in this thread that spent
> about 7 lines to implement the generic input reading loop that -n
> gives you for free; I followed up with a one-line version of that
> part, that showed it for what it really was, a Perl implementation
> of a simple sed editing command.  Is this magic?  Not in my book
> (if you'll pardon the pun); instead, it's just a worthwhile
> simplification that lets the reader focus on the data processing
> task, instead of having to wade through boilerplate infrastructure
> that has already been factored out by The Larry and memorialized in
> "-n".
> 
> As a case of what most of us might call "magic" in Perl, but
> I'd perhaps call "tomfoolery" instead, "eof()" and "eof" do very
> different things, as do "use Abc()" and "use Abc".  This is the
> kind of thing that I personally find very reprehensible in Perl,
> because programmers are led by other aspects of the language to
> believe that parentheses around argument lists are optional, and
> without any intrinsic meaning of their own.  That's "bad magic", IMHO.
> 
> And of course there's "autovivification" as another example of
> magic, but in Perl's defense I'd say it usually does what you meant
> it to anyway, and in any case you can skirt the issue by
> initializing on your own the objects it would create.
> 
> > I've been doing more .Net stuff for a while, and I feel the
> > understandability of perl quickly slipping away when I (try to) read
> > stuff like this. I don't see the same issues at all when I get away
> > from, say, C for a while. Is that just the idioms of the language?
> 
> Raw C provides no shortcuts, so one must always code all the routine
> data processing minutiae (unless you extend the language with
> macros) that we're allowed to invoke through invocation options
> like -n in Perl.  Sure, that makes it more readable, in the sense
> that every tiny detail is spelled out, but IMHO that also makes it
> less comprehendible, because it's too easy to miss the view of the
> trees for that of the forest.

 ...  (yadda, yadda, yadda)

> Happy Easter, everybody!  Eat plenty of chocolate. . .
> 
> -Tim
> *------------------------------------------------------------*
> |  Tim Maher (206) 781-UNIX  (866) DOC-PERL  (866) DOC-UNIX  |
> |  CEO, JAWCAR ("Just Another White Camel Award Recipient")  |
> |  tim at Consultix-Inc.Com  TeachMeUnix.Com  TeachMePerl.Com   |
> *+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-*
> |  Watch for my  Book: "Minimal Perl for Shell Programmers"  |
> *------------------------------------------------------------*
> _____________________________________________________________
> Seattle Perl Users Group Mailing List  
> POST TO: spug-list at mail.pm.org
> ACCOUNT CONFIG: http://mail.pm.org/mailman/listinfo/spug-list
> MEETINGS: 3rd Tuesdays, U-District, Seattle WA
> WEB PAGE: www.seattleperl.org



More information about the spug-list mailing list