Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

goto in subroutine?

1 view
Skip to first unread message

Mark Harrison

unread,
Sep 17, 1991, 12:05:18 PM9/17/91
to
In cleaning up some perl programs, I have moved some code into a
subroutine. The problem: The code has some goto's, the target of
which cannot be in a subroutine. Here is the snippet:

> EDIT:
> system("$editor $edopts $tmpfile");
>
> QUERY:
> print "\nsend, continue editing, discard [s,c,d]? ";
> $ans = <STDIN>;
>
> if ($ans =~ /^\s*[Ss]/ ) { &send($tmpfile); }
> elsif ($ans =~ /^\s*[Dd]/ ) { &discard($tmpfile); }
> elsif ($ans =~ /^\s*[Cc]/ ) { goto EDIT; }
> else { goto QUERY; }


1. What is the possibility of this being added in the future?

2. What is the best way to recode around this? Put in blocks
after each label and s/goto/next/?

Thanks in advance,
Mark.


--
Mark Harrison | Note: harr...@ssd.dl.nec.com and
harr...@csl.dl.nec.com | necssd!harrison are not operating at
(214)518-5050 | present. Please forward mail through the
| above address. Sorry for the inconvenience.

Larry Wall

unread,
Sep 19, 1991, 1:19:33 PM9/19/91
to
In article <1991Sep17.1...@csl.dl.nec.com> harr...@csl.dl.nec.com (Mark Harrison) writes:
: In cleaning up some perl programs, I have moved some code into a

: subroutine. The problem: The code has some goto's, the target of
: which cannot be in a subroutine. Here is the snippet:
:
: > EDIT:
: > system("$editor $edopts $tmpfile");
: >
: > QUERY:
: > print "\nsend, continue editing, discard [s,c,d]? ";
: > $ans = <STDIN>;
: >
: > if ($ans =~ /^\s*[Ss]/ ) { &send($tmpfile); }
: > elsif ($ans =~ /^\s*[Dd]/ ) { &discard($tmpfile); }
: > elsif ($ans =~ /^\s*[Cc]/ ) { goto EDIT; }
: > else { goto QUERY; }
:
:
: 1. What is the possibility of this being added in the future?

In the near future, the probability is close to zero. In the distant
future, I'll be dead, and posterity can do whatever they like... :-)

: 2. What is the best way to recode around this? Put in blocks


: after each label and s/goto/next/?

There are two basic ways to do do state diagrams in Perl. Both involve
keeping a state variable. One way uses the state variable in a case
structure inside a loop:

while ($state ne 'EXIT') {
$state eq 'EDIT' && do {
...
};
$state eq 'QUERY' && do {
...
};
}

The other way just calls out to subroutines based on the state:

while ($state ne 'EXIT') {
&$state;
}

sub EDIT {
...
}

sub QUERY {
...
}

I prefer the latter.

Larry

0 new messages