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":

given($_) { when (/^abc/) { $abc = 1; } when (/^def/) { $def = 1; } when (/^xyz/) { $xyz = 1; } default { $nothing = 1; } } # and similarly given($foo) { when (undef) { say '$foo is undefined'; } when ("foo") { say '$foo is the string "foo"'; } when ([1,3,5,7,9]) { say '$foo is an odd digit'; continue; # Fall through } when ($_ < 100) { say '$foo is numerically less than 100'; } when (\&complicated_check) { say 'a complicated check for $foo is true'; } default { die q(I don't know what to do with $foo); } }

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 reply to Re^3: Text Analysis: given(){when(){}} block throws a 'useless use of private variable...etc' error by TomDLux
in thread Text Analysis: given(){when(){}} block throws a 'useless use of private variable...etc' error by biohisham

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.