SPUG:Re: Readable, well-written code (was: Best One-Liners and Scripts for UNIX)

Scott Blachowicz scott+spug at mail.dsab.rresearch.com
Mon Apr 21 11:29:44 CDT 2003


Jeremy Kahn <kahn at cpan.org> wrote:

> Yikes. I have to agree with Brian (and as a teacher) that phrases[1] like
> 
>    ($x = $_) =~ ...
> 
> are very very hard. Where does a novice even start to look that up?  At 
> least it won't blow up my shell when I type "perldoc -f map". By 
> contrast, have you ever tried `perldoc -f $_` in a bash shell? Do it 
> *only* immediately after doing something safe, like "echo foo".  Caveat 
> user...

It may be "hard", but it's a standard idiom for altering a copy of
something in perl. Just like natural languages, where does one normally go
to find out about idiomatic use like that? One of those things that is
learned through experience, I guess?

You can do similar things in C++ if your operator=()'s return refs to their
objects (as is normally recommended):

	(a = b) = ...;   // [1]

Since the 'a = b' returns a reference to 'a', you can assign into it.  Of
course, I think it looks a bit strange to do that and for some reason I
think of it as being more natural in perl.  Ah well...

Scott

[1] I am using the C++0x operator '...', pronounced "yadda yadda 
    yadda", in this example. :))



More information about the spug-list mailing list