in reply to Re: 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.

It has changed the behaviour, now the assignment just becomes undefined, but the previous value is not recovered from the pad.

#! /usr/local/bin/perl5.9.5 use strict; use warnings; a(10); a(0); a(20); sub a { my $num = shift; my $x = $num * 100 if $num; print $x, $/; } # produces # 1000 # Use of uninitialized value $x in print at ./myif line 13. # 2000

Real state variables are now available with the state declarator:

sub c { state $count = 10; return $count++; } print c(), $/; # 10 print c(), $/; # 11 print c(), $/; # 12

• another intruder with the mooring in the heart of the Perl

Replies are listed 'Best First'.
Re^3: Curious result of using "my ... if ..."
by blazar (Canon) on May 16, 2007 at 14:30 UTC
    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.

      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.