in reply to Re: "if" in a my declaration statement causes problem
in thread "if" in a my declaration statement causes problem

My dense skull isn't penetrating this issue. The original poster wrote ...

Update: No, the original poster did not write what I claimed. The first response did.

my $x; # one statement $x = $y if {condition;} # two statement
the perlsyn you quoted is different
my $x if ... presuming my $x = $y if {condition;}
in his case. These seem to be different. You are holding up the perlsyn snip as an example says the two cases are synonyms. Why?

In my testing of this, it works (perhaps version dependent?):

sunorccws04 ~$ cat tpl ; ./tpl #!/usr/bin/perl sub showme; foreach $y ( 1..10) { $foo = $y; print showme(), "is the x value\n"; } sub showme { my $x; $x = $y if ($foo %2); # might as well mix up some T/F } 1 is the x value 0 is the x value 3 is the x value 0 is the x value 5 is the x value 0 is the x value 7 is the x value 0 is the x value 9 is the x value 0 is the x value
getting strict "breaks" the behavior,
sunorccws04 ~$ cat tpl ; ./tpl #!/usr/bin/perl use strict; my ($foo, $y); sub showme; foreach $y ( 1..10) { $foo = $y; print showme(), " is the x value\n"; } sub showme { my $x; $x = $y if ($foo %2); # might as well mix up some T/F } is the x value 0 is the x value is the x value 0 is the x value is the x value 0 is the x value is the x value 0 is the x value is the x value 0 is the x value
predefining x as in  my $x = 0;, doesn't change the results. Changing the declaration of $y, $foo from my to our causes the code to behave like the non-strict version. But in all cases the results are consistant and predictable.

??

Be Appropriate && Follow Your Curiosity

Replies are listed 'Best First'.
Re^3: "if" in a my declaration statement causes problem
by ysth (Canon) on Feb 22, 2006 at 19:15 UTC
    My dense skull isn't penetrating this issue. The original poster wrote
    my $x; # one statement $x = $y if {condition;} # two statement
    Both when I posted and now, the OP reads: my $x = $y if ( <some condition> ); Are you looking at someone else's reply?

    With the my $x separate and unconditional, the warning in perlsyn doesn't apply.

    The output from your strict version doesn't make sense to me, offhand.