[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [Fun With Perl] Does this count as a one-liner?



MJD et al.:

On Tue, Jun 15, 1999 at 06:22:54PM -0400, mjd-list-fwp@plover.com wrote:
> > For compactness, I believe it could be written as:
> > 
> >     $query = (unindent ($quantity < 1) ? <<""
> >         DELETE FROM Contents
> >         WHERE cartnum = '$cartnum'
> >           AND itemid = '$itemid';
> > 
> >     : <<"");
> >         UPDATE Contents
> >         SET quantity = $quantity
> >         WHERE cartnum = '$cartnum'
> >           AND itemid = '$itemid';
> > 
> > Of course that is a bit more dangerous since the dangling ); visually
> > closed the line. 
> 
> I agree.  I would suggest:
> 
>      $query = (unindent ($quantity < 1) ? <<""  : <<"");
> 
>          DELETE FROM Contents
>          WHERE cartnum = '$cartnum'
>            AND itemid = '$itemid';
>  
>          UPDATE Contents
>          SET quantity = $quantity
>          WHERE cartnum = '$cartnum'
>            AND itemid = '$itemid';
>  
> It's funny how people always forget that the <<TAG doesn't have to be
> the last thing on the line.  They always write
> 
> 	print (...., <<EOM
> 	..
> 	..
> 	..
> 	EOM
> 	);
> 
> When they should have written
> 
> 	print (...., <<EOM);
> 	..
> 	..
> 	..
> 	EOM

Yes, that's odd. Perhaps this practice is encouraged by the fact that
vim's Perl syntax highlighting behaves in the former manner.  That is,
_everything_ between the <<"EOF" and the ^EOF$ is coloured as if it is
inside the HERE.

The thing I love about using <<"" is that changing the indenting of a
block of code with '<<' or '>>' in vim does not break anything.

> > I suppose I could eliminate cut-and-pasted redundancy with the
> > more-entertaining:
> > 
> >     $query = (unindent ($quantity < 1)
> >     ? <<''
> >         DELETE FROM Contents
> > 
> >     : <<""
> >         UPDATE Contents
> >         SET quantity = $quantity
> > 
> >     . <<""
> >         WHERE cartnum = '$cartnum'
> >           AND itemid = '$itemid';
> > 
> >     );
> 
> Try this:
> 
>      $query = (unindent ($quantity < 1) ? <<'' : <<"") . <<"";
> 
>          DELETE FROM Contents
>  
>          UPDATE Contents
>          SET quantity = $quantity
>  
>          WHERE cartnum = '$cartnum'
>            AND itemid = '$itemid';

Yeah, that works great with one less blank line and a rearrangement of
the parens to feed the _final_ query to unindent:
    $query = unindent ((($quantity < 1) ? <<'' : <<"") . <<"");

Confuses the hell out of vim, though! As usual, only Perl can parse
Perl.

> > Also, do I really save any CPU time with the <<'' rather than <<""
> > in the first part of this last example?
> 
> I humbly suggest that if you're really worried about that, you're
> programming in the wrong language.

Indeed.

I think the reason I had the stuff split over multiple lines was that
I had a bit of a mental block from when I first used <<"Here" in Perl.
On p.44 of the blue camel, the example reads:

    print <<"dromedary", <<"camelid"; # you can stack them
I said bactrian.
dromedary
She said llama.
camelid

Never having actually tried this, I got it into my head that the
value of <<"camelid" would be
"I said bactrian.\ndromedary\nShe said llama.\n" rather than just
"She said llama.\n". Of course, this makes absolutely no sense, but
that's how it got wired into my brain. I think the fact that the
"Here" tags "dromedary" and "camelid" were related to the content
confused me a bit.

Thanks to everyone who responded and helped me clear this silliness
from my head!

Cheers,

Tom "What was I thinking??" Rathborne

-- 
--    Tom Rathborne    tomr@aceldama.com
--                     http://www.aceldama.com/~tomr/
--    "I seem to be having tremendous difficulty with my life-style."

==== Want to unsubscribe from this list? (Don't you love us anymore?)
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org