develooper Front page | perl.perl5.porters | Postings from December 2016

Guidance on keyword and scope at end of for loop

Thread Next
David Mertens
December 12, 2016 14:58
Guidance on keyword and scope at end of for loop
Message ID:
Hello everyone,

As mentioned in a previous email, I am working on a keyword-based module.
Some of the keywords modify the contents of the hints hash. The problem
I've encountered (apart from the one I just mentioned) is that the hints
hash seen by the keyword depends on whether it is at the end of a for loop.
Basically if I have this code:

    my $var;
    # ...

then the hints hash is modified as expected the the other_keyword has
access to those changes. However, if I have this code:

    for {
        # ...
    # ...

then the changes to the hints hash are not visible to other_keyword. I have
tested this with a number of block-like constructs, and the problem appears
to show up whenever my_keyword_that_modifies_hints_hash() follows a loop
(for, foreach, while, until) or a conditional block that could be followed
by else or elsif. Interestingly, this is not a problem with a bare block,
even one that contains a "redo" or "next" statement.

My best guess about what's going on here is that the parser is open to the
possibility that there could be a flow-control statement/block (continue or
else), so it does not close the lexical scope of the previous block until
it's examined the token following the block.

What is the best way for my keyword to handle this? How would I detect this
case? Could I reasonably instruct the parser that the scope has closed, so
please give update PL_curcop to the outer scope? If that would be
unreasonable, I would consider walking up the op tree until I found an op
in the outer lexical scope, but I would appreciate some guidance or
pointers on how to do that.


 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About