in reply to Re^2: Curious result of using "my ... if ..."
in thread Curious result of using "my ... if ..."

5.10 throws a warning for that type of construct

No it doesn't. I seem to recall that blead did, at some point, but it appears to no longer do so.

I constantly wonder why it is not an error at all. Whatever its behaviour could be in an actual implementation, I can see no logical nor intuitive semantics that could be attached to the construct. It has a meaning to do something upon a condition. How can have a meaning for a thingie to exist or not, upon a condition? Point is, declaration happens at compile time, condition is verified at runtime. "Ideally" one could expect that in statements following

my $var=whatever if condition;

$var would be the lexical one just declared if condition is true, and something else (as if the declaration just were not there) otherwise: clearly, any way you look at it, it's unsatisfactory.

Replies are listed 'Best First'.
Re^4: Curious result of using "my ... if ..."
by jdporter (Paladin) on May 16, 2007 at 14:35 UTC

    I don't see why this should be an error. Just as

    my $x = 5 if $t;
    could be semantically equivalent to
    my $x; $x = 5 if $t;
    then
    my $x if $t;
    could be semantically equivalent to
    my $x; $x if $t;
    If that throws a warning, then, fine. It certainly shouldn't be an error (unless, of course, you've asked that this type of warning be converted to an error).

    A word spoken in Mind will reach its own level, in the objective world, by its own weight

      I don't see why this should be an error. Just as

      my $x = 5 if $t;

      could be semantically equivalent to

      my $x; $x = 5 if $t;

      I think it all boils down to a matter of personal interpretation and taste; your example doesn't convince me at all: yes, it could, but I see no reason why it should. More precisely, it's true that the single

      my $x = 5;

      statement is semantically equivalent to the two

      my $x; $x = 5;

      statements. But is it true in general than when STATEMENT is equivalent to STATEMENT1; STATEMENT2; ... STATEMENTn; then also

      STATEMENT if condition;

      is equivalent to

      STATEMENT1; STATEMENT2; ... STATEMENTn if condition; # ?!?

      IMHO not only is the answer no but FWIW I see no aesthetical appeal of such an "algebraic" property.

      OTOH since

      STATEMENT if condition;

      is generally equivalent to

      if (condition) { STATEMENT }

      and

      if ($t) { my $x = 5 }

      is perfectly valid, albeit fundamentally useless, perhaps the "incriminated" construct should not trigger an error, but just be a happily useless thingy. Granted, Perl 5 already has so many ad hoc deviations from orthogonality for the sake of beauty, pragmatics and magic that one may want to throw in yet another one: yet I still fail to see how that particular construct could be of any good with any of those.

        I see no reason why it should

        Well, (a) because nobody has offered any other workable semantics for it, and (b) because it's what some programmers expect it to mean anyway.

        when STATEMENT is equivalent to STATEMENT1; STATEMENT2;

        But that will always break down with statement modifiers, even in cases not involving a combination of compile-time and run-time actions.

        A word spoken in Mind will reach its own level, in the objective world, by its own weight