1

I've created a subroutine named "MENU" with a label named "INIT_MENU" at the top of the subroutine, but when i call this label i got a error : Can't goto subroutine outside a subroutine at program.pl line 15

Here is an example:

sub MENU {INIT_MENU: print "blah blah";}

and here is the line 15:

goto &MENU, INIT_MENU;

Sorry if it is a duplicated question, i searched in all possible places and even in the official site of Perl

13
  • Doesn't look like there is enough information is available. Try to see if you can create a small example. Commented Oct 22, 2017 at 3:28
  • 4
    Usually when I see a goto in someone's perl code, I think its either genius or madness, but I cannot tell which.
    – xxfelixxx
    Commented Oct 22, 2017 at 10:11
  • 1
    @xxfelixxx All geniuses are mad anyway. When I see a goto, I think the programmer either doesn't understand the structure of the data or does not know the best representation of the data inside a program. Taking time to figure out what is going on will remove any goto. Commented Oct 22, 2017 at 12:53
  • 2
    Please don't make more work for others by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under the CC BY-SA 4.0 license, for SE to distribute the content (i.e. regardless of your future choices). By SE policy, the non-vandalized version is distributed. Thus, any vandalism will be reverted. Please see: How does deleting work? …. If permitted to delete, there's a "delete" button below the post, on the left, but it's only in browsers, not the mobile app.
    – Makyen
    Commented Sep 13, 2019 at 1:07
  • 1
    @Chipster The comment is intended to cover both situations where the OP has the capability to delete and those situations where the OP won't be able to delete the post. In this case, the OP won't be able to delete this question due to multiple criteria preventing it. The Q&A I linked in my first comment provides a much more detailed description of what is possible for the Op and the post within the SE system wrt. deleting.
    – Makyen
    Commented Sep 13, 2019 at 1:23

1 Answer 1

5

The goto expects a single argument, so this code first executes goto &MENU and then after the comma operator it evaluates the constant INIT_MENU in a void context (drawing a warning).

The purpose of goto &NAME is to replace the subroutine in which it is encountered with subroutine NAME; it doesn't make sense calling it outside of a subroutine and this is an error. From perldiag

  • Can't goto subroutine outside a subroutine

(F) The deeply magical "goto subroutine" call can only replace one subroutine call for another. It can't manufacture one out of whole cloth. In general you should be calling it out of only an AUTOLOAD routine anyway. See goto.


A comment on the purpose of this.

A lot has been written on the harmfulness of goto LABEL. Not to mention that INIT_MENU: cannot be found when hidden inside a routine. The upshot is that there are always better ways.

A sampler

  • Call the function and pass the parameter

    MENU(INIT_MENU);
    
    sub MENU {
        my $menu = shift;
        if ($menu eq 'INIT_MENU') { ... }
        ...
    }
    
  • If, for some reason, you want to 'hide' the call to MENU use goto &MENU as intended

    sub top_level {   # pass INIT_MENU to this sub
        ...
        goto &MENU;   # also passes its @_ as it is at this point
    
        # the control doesn't return here; MENU took place of this sub
    }
    

    This altogether replaces top_level() with MENU() and passes on its @_ to it. Then process input in MENU as above (for example) to trigger the chosen block. After this "not even caller will be able to tell that this routine was called first," see goto. Normally unneeded

  • Why even have MENU()? Instead, have menu options in separate subs and their references can be values in a hash where keys are names of options. So you'd have a dispatch table and after the logic (or user choice) selects a menu item its code is run directly

    my %do_menu_item = ( INIT_MENU => sub { code for INIT_MENU }, ... );
    ...
    $do_menu_item{$item_name}->();
    
  • Menu systems tend to grow bigger and more complex with development. It would make sense to adopt OO from start, and then there are yet other approaches for this detail

If you find yourself considering goto it may be time to reconsider (some of) the design.


See for instance this post and this post. It's (even) worse than that in Perl since there are specific constructs (and restrictions) for situations where goto can be argued acceptable, so there is even less room for it. One example is jumping out of nested loops: there are labels in Perl.

1
  • Another alternative to dispatch tables is OO polymorphism. Commented Oct 22, 2017 at 12:51