Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Advice on goto usage?

by Nevtlathiel (Friar)
on May 05, 2005 at 09:08 UTC ( [id://454278]=note: print w/replies, xml ) Need Help??


in reply to Advice on goto usage?

Using goto is generally considered to be bad style as it can make your code hard to follow. Consider re-coding the above snippet without using a goto, eg:

#! /usr/bin/perl use strict; use warnings; sub get_input { print "Type R to re-enter the values or Type E to exit:\n"; chomp (my $value = <STDIN>); $value; } my $value = get_input(); while ($value ne 'E') { if ($value eq 'R') { $value = get_input; } else { print 'Invalid entry, please try again\n'; get_input; } } exit();
----------
My cow-orkers were talking in punctuation the other day. What disturbed me most was that I understood it.

Replies are listed 'Best First'.
Re^2: Advice on goto usage?
by Limbic~Region (Chancellor) on May 05, 2005 at 13:19 UTC
    Nevtlathiel,
    Using goto is generally considered to be bad style...

    I agree with what you have said but feel it is important to share more of the story. There are 2* forms of goto in Perl and 1 isn't so bad at all. From TFM:

    The "goto-&NAME" form is quite different from the other forms of "goto". In fact, it isn't a goto in the normal sense at all, and doesn't have the stigma associated with other gotos.

    Read Pure Perl tail call optimization if you want to see how this is useful.

    Cheers - L~R

    * Really there are 3, but 2 equate to the same thing.
Scheduling
by imperl (Novice) on May 05, 2005 at 10:03 UTC
    Was wondering, if anybody knows what wrong with this code. I want a certain set of batch files to be executed every 15 minutes for a period of 1 hour (or any number of hours inputed). I executed the script last evening and scheduled it for 2 hours but the script went on all night, for 15 hours. Any help would be appreciated.
    $seconds=900/$num; print "Enter the total period in hours:\n"; $periodhr=<STDIN>; $periodsec=$periodhr*3600; $output=1; for($p=0; $p<=$periodmin; $p++) { for ($i=$f;$i<=$l; $i++) { $x="$t$i"; chomp($x); $file="$x.bat"; chomp($file); unless (-x $file) #-x here checks to see if the f +ile is executable { warn "File $file does not appear to exist or is n +ot executable!!\n"; next(); } system("$file"); $time=localtime; print FOUT "$output \t"; print FOUT "$file \t"; print FOUT "$time\n"; $output++; sleep($seconds); } sleep($seconds); $i=$f; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-24 10:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found