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

Guidance on keyword and scope at end of for loop

Thread Next
From:
David Mertens
Date:
December 12, 2016 14:58
Subject:
Guidance on keyword and scope at end of for loop
Message ID:
CA+4ieYW=gapuYODEkhy9f4G=Li3r=Q-CHru+iW57yKWJstYFHw@mail.gmail.com
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;
    other_perl_expression();
    my_keyword_that_modifies_hints_hash();
    # ...
    other_keyword_that_uses_content_of_hints_hash();

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

    for {
        # ...
    }
    my_keyword_that_modifies_hints_hash();
    # ...
    other_keyword_that_uses_content_of_hints_hash();

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.

Thanks!
David

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


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About