Hi Dragonchild, actually this isn't quite right

Yes, it is a statement modifier, but it's also a looping construct with its own scope.

In fact modifiers of all kinds do not have their own scope. They execute in the scope of their enclosing block. This can be seen by my $x=1 if $y;$x++; where usage of the the $x after the if does not raise an exception.

The reason it seems like they do when you use the do BLOCK pseudo-function is because in this case the block itself has its own scope. This is illustrated by the code below where the only variable usage that throws use strict; is the one declared inside of a do block.

Another interesting point with the do usage is that the block gets executed an extra time, ie before the first evaluation of the conditional. This can be seen by the output line Do's pre-evaluation:DO{}while

The compound statement forms of the modifiers are different in that they have their own scopes and scoping rules (for instance the for my $i (0..10) {} where the my's scope is actually the contents of the block.

use strict; use warnings; use Tie::Cycle; tie my $cycle, 'Tie::Cycle', [ 1..3,0,reverse (1..3),0 ]; $\=":stmt while\n"; print my $statement=$_ while ($_=$cycle)>0; print defined($statement) ? "Defined" : "Notdefined"; $_="Do's pre-evaluation"; $\=":do{}while\n"; do { print my $do = $_ } while ($_=$cycle)>0; eval 'print defined($do) ? "Defined" : "Notdefined";'; print $@ if $@; $\=":stmt while 0\n"; print my $while="Test" while 0; print defined($while) ? "Defined" : "Notdefined"; $\=":stmt if\n"; print my $if="Test" if 0; print defined($if) ? "Defined" : "Notdefined"; $\=":stmt for\n"; print my $for=$_ for 0; print defined($for) ? "Defined" : "Notdefined"; __DATA__ 1:stmt while 2:stmt while 3:stmt while Notdefined:stmt while Do's pre-evaluation:do{}while 3:do{}while 2:do{}while 1:do{}while Global symbol "$do" requires explicit package name at (eval 1) line 1. :do{}while Notdefined:stmt while 0 Notdefined:stmt if 0:stmt for Notdefined:stmt for
Notice that in the non do {} simple statement form, even when the condition is 0 and one might expect Perl to optimize it away the variable is created in the outside scope.

Yves
--
You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)


In reply to Re:while modifier( and scoping) by demerphq
in thread while modifier by Anonymous Monk

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.