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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |