in reply to Re^3: Control Structures
in thread Control Structures

I disagree - that's not a problem with the trailing if(). That's a problem with how my() is implemented as both a compile-time and run-time actor. The trailing if() is doing exactly what it's supposed to do, which is execute the statement at run-time if the conditional is true.

The reason it's counter-intuitive is because my() is the only statement that affects the current scope with both compile-time and run-time actions. If you use a standard if-block with {}, you create a new scope and everything acts as expected.


  • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
  • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"

Replies are listed 'Best First'.
Re^5: Control Structures
by perrin (Chancellor) on May 10, 2005 at 16:26 UTC
    Try explaining that to a mid-level perl developer or an experienced programmer who is still learning perl and watch their eyes glaze over. The problem goes away if you don't use trailing if() statements.
      The problem goes away if you don't use trailing if() statements modifying my() statements. The trailing if() isn't the problem; the problem is when it's modifying a my() statement. Disallow that. Later, when they're ready, they'll ask why that specific prohibition and you'll explain it to them and they'll understand. In fact, you can almost consider that a rite of passage from journeyman to master.

      • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
      • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"
        I consider myself an "average" perl developer. I've never been a full-time developer, but I instantly understood the my $xx if () problem when I meet it, and avoid it since then.
        Sure, I could do it that way, but it's one more thing to remember, and since I don't actually need trailing if() for anything it's easier to just not use it. I tend to favor a coding style based on the fewest traps to remember and am willing to sacrifice lots of things that are cool but not really necessary in order to get there.