in reply to Re^4: postfix syntax enlightenment
in thread postfix syntax enlightenment
It may be that you're reading more into the given example than is intended in the documentation. Examples tend to be short and to the point: complex examples can often obscure the main idea that the example is intending to convey. "my $x if ..." would be one of the shortest examples that showed a lexical declaration with a statement modifier.
"... my, state, or our ..." are all keywords that introduce a lexical declaration. There's nothing in the documentation that says anything about how those keywords might be used: it mentions no restrictions or exemptions. Just because the example only shows "my $x", I see nothing that suggests more complex examples would behave any differently (with respect to producing in an undefined result which should not be relied upon). All of these would be equally valid examples but tend to (increasingly) hide the point being made (and might even suggest exclusion of simpler examples):
my ($x) if ... my $x = 1 if ... my ($x, $y) = (1, 2) if ... my ($x, $y, $z) = (split /\s+/ => $spaced_tokens)[3 .. 5] if ...
Similarly, "... modified with a statement modifier conditional or loop construct ..." mentions no restrictions or exemptions. The example uses "if"; however, there's nothing to suggest that any of the other modifiers (unless, while, until, for, foreach or when) would behave any differently (with respect to producing in an undefined result which should not be relied upon).
For what it's worth, I played around with the OP code a bit when it was first posted. In one test, I added a check for whether $test1 was defined after "my $test1 = ... if ...;". In the first call to test(), $test1 was undefined; in the second call it was defined:
$test1 is not defined. Use of uninitialized value $test1 in concatenation (.) or string at ./ +pm_example.pl line 13. test1 after hash assignment: '' i = 0 test1 after defaulting to i: '0' $test1 is defined. test1 after hash assignment: '0' i = 1 test1 after defaulting to i: '0'
That looks like exactly the unpredictable results described in: "The value of the my variable may be undef, any previously assigned value, or possibly anything else. Don't rely on it."
-- Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: postfix syntax enlightenment
by Laurent_R (Canon) on Mar 29, 2014 at 10:23 UTC |