It struck me the other day that I, in my quest to make my code ever shorter (which I understand isn't always a good thing, but that's not the point here), wanted to be able to uc a string automatically with "uc $foo;" as opposed to the longer, more drawn out "$foo = uc $foo;". "Why doesn't it do this already?" I said. "It should just change the variable, like chomp!"

Then I realized that might be a bit troublesome - I don't really want to change my strings with something like "if (uc $foo eq uc $bar)"... and making copies of each would be quite annoying as well. But I still couldn't get over the fact that I didn't like the long version... it just seemed un-Perlish to me.

After a few seconds, I realized something - the statement "uc $foo;" is in void context... nothing's being done with the return value... so why not tinker with uc so that it would recognize the calling context, and if it was void, change the variable?

So, of course, I went and cooked up a quick and dirty module to do this, which is only about 25 lines long and is fairly easily expandable. But the point of all this (this IS a meditation, not me advertising bad code) is that I'm left wondering... is there some reason that I shouldn't be doing this? And why isn't it this way already?

The second question relates a bit more to the title... After all, "lc $foo;" certainly doesn't DWIM now... and I'm wondering if it's unreasonable to expect Perl to work this way. Certainly, I've been known to want crazy things from Perl before... is this an exception, or just more uneducated rambling?

His Royal Cheeziness


In reply to DWIM Part Nineteen: Unary Operators in Void Context by CheeseLord

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.