Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document warnings etc if nested, and workarounds (sugar only) #6

Open
n1vux opened this issue Jun 20, 2024 · 2 comments
Open

document warnings etc if nested, and workarounds (sugar only) #6

n1vux opened this issue Jun 20, 2024 · 2 comments

Comments

@n1vux
Copy link

n1vux commented Jun 20, 2024

when using match::simple::sugar (with Data::Enum that now supports match::simple !),
when nesting the switch for () { when ..., then{}; ...} non-looping loop block
within an outer for(), foreach() or while(<>){} actual loop as one does, whether as an event loop or per-line-input loop,
there are three implications that should be documented; one of which might be fixable in module code.

  1. the outer loops must use a named loop variable, or (minimally invasive) have a local my $line=$_; just before the SWITCH: for when, and any and all implicit m{}, //, etc or explicit references to $_ must be changed to $line =~ m{}.
  2. outer and inner loops need LINE: while(<>){...} and SWITCH: for ($STATE){when .. statement labels, as when...then will do next on the inner for as if it were a loop, so script's own loop control next, redo, last must name next LINE;.
  3. to avoid a flurry nay blizzard of Exiting subroutine via next at warnings on STDERR, must insert no warnings 'exiting'; after SWITCH: for ($var){ .

I would not put the no warnings outside the for-when lexical block, so as to not block warnings caused by other errors.

It might be practical to inject the no warnings 'exiting'; into the anon-subs made from the then {block} ?

(Near as i can tell, there's no danger of an innermost loop inside the then{} clause having a problem, as the hidden next SWITCH by the when occurs after the then{} anon sub returns to when, if i don't next LINE; out of it.)

@n1vux
Copy link
Author

n1vux commented Jun 27, 2024

Additional warning required

that return in the then {BLOCK} won't do what is expected, but rather returns from the when-then anon-sub (lambda).

Would need to return up the call stack to return from my visible sub.

(Instead i just use save a $ret value the way we used to do when one in one out structured programming forbade return from anywhere except the bottom of the subroutine. )

(Aside: Probably helped that I'm old enough to have suffered under that formalism. Which did let us reason over programs, and I have proved several production loops correct according to precondition, invariant, progress, postcondition, which benefited from no early exits / no gotos. But readable code is more useful most of the time. 🤣 )

@n1vux n1vux closed this as completed Jul 10, 2024
@n1vux n1vux reopened this Jul 10, 2024
@n1vux
Copy link
Author

n1vux commented Jul 10, 2024

sorry, copied text and accidentally hit close with comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant