in reply to The perils of being tricky

You don't have warnings turned on, do you?

Here's a way not to have to put parentheses around everything. Use the unary + operator:

print +( 'b == a', 'b < a', 'b > a', )[$a <=> $b], "\n";

Replies are listed 'Best First'.
Re^2: The perils of being tricky
by mstone (Deacon) on Jun 04, 2005 at 19:12 UTC

    You don't have warnings turned on, do you?

    Not for the quick and dirty stuff. I use -w and strict at specific points in my development process, but usually I stick to techniques that I know to be clean.

    It's probably just a personal thing, but I find that constantly using warnings is worse for me than checking my work at intervals. If I get a warning during continuous use, I just correct it and move on. My primary goal at that point is to make sure my latest piece of code produces the result that I want. When I use warnings at intervals, each warning pass is a specific test of the code's integrity. My only concern at that point is whether the code is clean, so I take the time to find ways to make sure my code stays clean in the future.

    Mistakes like this one show me when I'm playing with things I shouldn't play with at all. If I need mechanical assitance to write a piece of code correctly, I probably won't be able to read it six weeks from now. And IMO, writing code that even I can't read without assistance is a problem. If I can't find a version that works, is clean, and is easy for me to remember, I write the technique off as being too tricky and look for other solutions.

      However, the +(..) "trick" is very well-known and comes up very often (for me at least). It's documented in perlop and is worth knowing.

      Cool trick, though!

        However, the +(..) "trick" is very well-known ...

        No arguments there, and if you're used to it, by all means run with it. That just means it's part of your personal style.

        Me, I prefer not to think about syntax precendence if I can avoid it. That isn't a 'better' way of looking at things, it just means we have different regional dialects. ;-)

      That's like saying you only write code you know to be bug-free :-)

      I know I make mistakes all the time. That's why I use as much unintrusive help as I can get. I'm not making an ideology of sloppiness, of course, but I don't chastise myself unduly for things that can't be helped. Oh, perl found a problem in my code? My bad; let's move on.

      (Of course I'm a better coder now than I used to be. I do learn from my mistakes; I just try not to spend overly much time on the learning itself. Also, warnings in this case and in many like it would have saved you from a real trap.)

Re^2: The perils of being tricky
by ysth (Canon) on Jun 05, 2005 at 07:50 UTC
    Note that the code cited by the OP doesn't give a warning; it dies with a syntax error (as the OP stated) first.

    Update: yikes, it does so give a warning; how did I miss it?!