![]() |
|
P is for Practical | |
PerlMonks |
Re^3: Text Analysis: given(){when(){}} block throws a 'useless use of private variable...etc' errorby TomDLux (Vicar) |
on Oct 18, 2010 at 19:05 UTC ( [id://866025]=note: print w/replies, xml ) | Need Help?? |
In my opinion, you're abusing the "given/when" construct. Yes, it works, because you have a boolean expression in the when() clauses. But you could put anything in the given part, and it would work just the same. The point of "given/when", is that the value in the given clause is assigned to $_, and then you can use invisible default $_ in all the when() tests. From "perldoc perlsyn":
As far as I can see, you're using a given/when block to produce an if/elsif block with an extra level of indentation. If you modify your foo/bar/baz example so that each print block has a new statement after it: pritn "\$_ is $_.\n";. That will print out what the value of $_ is, no matter which path you take. You will see that $_ is 'baz'. Your ($foo, $bar, $baz) evaluates the sequence of expressions and uses the last one as the chosen value. The earlier ones are evaluated only for their side-effect. Of course, $foo does not have a side-effect, but it could equally be $foo++, or $foo->(). That's why you're getting the message about "useless use of a private variable", because only the last one gets assigned to $_. As Occam said: Entia non sunt multiplicanda praeter necessitatem.
In Section
Seekers of Perl Wisdom
|
|