Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Go to perl::critic (goto considered awesome)

by Anonymous Monk
on Oct 06, 2019 at 18:19 UTC ( #11107103=note: print w/replies, xml ) Need Help??


in reply to Re: Go to perl::critic (updated)
in thread Go to perl::critic

A C<goto LABEL> is considered a bad practice by a number of people, as it can lead to hard-to-understand and brittle spaghetti code.

Who cares what management thinks? Perl's goto LABEL is an awesome tool when one wants loop-like flow control outside of loops. It's the easy way to implement interactive shell scripts:

INPUT: print 'number> '; chomp(my $input = <STDIN>); if ($input and $input =~ /[0-9]+/) { print $input, "\n" } else { goto INPUT }

Replies are listed 'Best First'.
Re^3: Go to perl::critic
by haukex (Bishop) on Oct 06, 2019 at 18:36 UTC
    Perl's goto LABEL is an awesome tool when one wants loop-like flow control outside of loops.

    But unnecessary in many cases:

    my $input; INPUT: { print 'number> '; chomp($input = <STDIN>); redo INPUT unless $input && $input=~/\A[0-9]+\z/; } print $input, "\n";

    (Or just use a proper prompting module, such as IO::Prompter.)

    TIMTOWTDI - I'm not saying that goto isn't sometimes an acceptable solution. But IMHO those cases are much more rare than an actual need for goto, especially in a language like Perl that has many other nice options for flow control. And of course if you want to use goto, all Perl::Critic policies are optional; this one just happens to be what the OP wanted. I'm not going to start a debate about whether goto is good or not. Note my wording: "A goto LABEL is considered a bad practice by a number of people, as it can lead to hard-to-understand and brittle spaghetti code." There was a good line by a comedian, paraphrasing: "I love the phrase 'a number of' because it can mean anything. 'A number of supermodels want to sleep with me.' That number is zero."

    Update: Fixed the regex.

      But a labeled block is a single loop, that redo makes infinite. I said outside loops is where goto comes in really handy. I only seem to ever want goto when extending the handling of STDIN of an existing script. Maybe my mind was poisoned by early exposure to BASIC or I just like that Perl has a feature of FORTRAN. Sometimes out of sheer boredom I'll write sweet spaghetti code on purpose because it's fun and easy and, as we all know, good Perl scripts tend to be so perfect they don't require maintenance. =)
        But a labeled block is a single loop, that redo makes infinite. I said outside loops is where goto comes in really handy.

        But the goto means the above code also contains a loop. Think assembly! No blocks, no loop constructs, no ifs, just conditional jumps...

Re^3: Go to perl::critic (goto considered awesome)
by Anonymous Monk on Oct 06, 2019 at 22:13 UTC

    Who cares what management thinks?

    Employees? Duh :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11107103]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2020-11-29 07:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?