Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

To goto or not to goto

by PerlJam (Sexton)
on Jun 10, 2002 at 22:15 UTC ( [id://173321]=perlquestion: print w/replies, xml ) Need Help??

PerlJam has asked for the wisdom of the Perl Monks concerning the following question:

Hi Everyone, I am writing a program that is going to send sales quotes and receive data to and from a ftp server. I am trying make it so that if the computer gets disonnected from our RAS it'll redial and then resend the quote that it failed on and all other quotes left in the array. I am wondering what the best way to do this would be. As you can see in the code below, when the transfer fails I call the routine to connect to the RAS. Now I was thinking of using a goto command here to jump back to the beginning of the current for loop after the connection is made. I am hesitant only because I know how goto's should be avoided unless necessary. Is this a good place to use one? or is there another method I should look into?
# # Loop through quotes array and upload each file in the array. # foreach my $quote (@quotes) { system("cls"); emphasize("Sending quote: $quote"); my $return = $ftp->put("$quote"); # # If the upload completed $return should == $quote # and we can move the file to a backup folder. # if ($return == $quote) { + # Needs testing!!!!!!!!!!!!!! move("$quote", "${new_dir}\\$quote") or warn "move failed: + $!"; } else { while ( &connect_ras() ) {} # ShouldI place a goto here and a label at the beginning? } } disconnect_ftp(); }

----
Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated failures. Persistence and determination alone are omnipotent. --Calvin Coolidge (1872-1933)

Replies are listed 'Best First'.
Re: To goto or not to goto
by dsheroh (Monsignor) on Jun 10, 2002 at 22:37 UTC
    From man perlfunc:
    redo LABEL
    redo   The "redo" command restarts the loop block without
           evaluating the conditional again.  The "continue"
           block, if any, is not executed.  If the LABEL is
           omitted, the command refers to the innermost
           enclosing loop.  This command is normally used by
           programs that want to lie to themselves about what
           was just input:
    
Re: To goto or not to goto
by FoxtrotUniform (Prior) on Jun 10, 2002 at 22:37 UTC

    I'd rework the loop body like so:

    foreach my $quote (@quotes) { my $return = 0; do { system("cls"); emphasize("Sending quote: $quote"); $return = $ftp->put($quote); if($return != $quote) { while(&connect_ras()) {} } } while($return != $quote); move($quote, "${new_dir}\\$quote") or warn "move failed: $!\n"; }

    The inner "do...while" loop test could use some work, but I think the loop expresses the idea "keep trying to send this quote until we succeed" better than a goto would.

    --
    The hell with paco, vote for Erudil!
    /msg me if you downvote this node, please.
    :wq

Re: To goto or not to goto
by DamnDirtyApe (Curate) on Jun 10, 2002 at 22:38 UTC

    Perhaps a next would keep you from having to use a goto here.

    # # Loop through quotes array and upload each file in the array. # MY_LOOP: foreach my $quote (@quotes) { system("cls"); emphasize("Sending quote: $quote"); my $return = $ftp->put("$quote"); # # If the upload completed $return should == $quote # and we can move the file to a backup folder. # if ($return == $quote) { + # Needs testing!!!!!!!!!!!!!! move("$quote", "${new_dir}\\$quote") or warn "move failed: + $!"; } else { while ( &connect_ras() ) {} # ShouldI place a goto here and a label at the beginning? next MY_LOOP ; } } disconnect_ftp(); }
    Note: this is untested, but I think it fits with your intent.

    Update: After reading the above posts, I'm not sure if you want redo or next. Use redo if you want to skip back to the start of the block without re-evaluating the conditional. Use next if you want to skip to the end of the block, so that the conditional is re-evaluated for the next iteration. Hope that makes sense. :-)


    _______________
    D a m n D i r t y A p e
    Home Node | Email
Re: To goto or not to goto
by PerlJam (Sexton) on Jun 10, 2002 at 22:56 UTC
    Makes perfect sense Ape... Thanks!

    I can't do the next as it would leave the quote that failed to go, behind. I'll try out the redo and the do while loop proposed. Thanks for the ideas guys!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://173321]
Approved by ichimunki
Front-paged by RhetTbull
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-19 15:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found