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

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



Here's my favourite <<HERE idiom for inline SQL from a snazzy mod_perl
shopping cart.

    $query = (unindent ($quantity < 1)
    ? <<""
        DELETE FROM Contents
        WHERE cartnum = '$cartnum'
          AND itemid = '$itemid';

    : <<""
        UPDATE Contents
        SET quantity = $quantity
        WHERE cartnum = '$cartnum'
          AND itemid = '$itemid';

    );

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 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';

    );

Can anyone comment on how operator precedence will affect this?  I
wouldn't want $query to ever end up being just 'DELETE FROM Contents'!

Also, do I really save any CPU time with the <<'' rather than <<"" in
the first part of this last example?

Cheers,

Tom

-- 
--    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