Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: Go to perl::critic

by haukex (Archbishop)
on Oct 06, 2019 at 18:36 UTC ( [id://11107104]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^4: Go to perl::critic
by Anonymous Monk on Oct 07, 2019 at 02:26 UTC
    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...

        If you're really worried about the cost of a while loop then maybe Perl isn't the right tool for the job.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-19 02:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found