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

greetings wise monks...
is there a better way of controlling the loops in this?? I need to exit out of last piece of code & return to while (1) loop..
I have tried putting in next; after each block but can't get it to maintain my main while(1) loop..
thanks steve
my $n =50; while(1) { my $time = time(); print $sock "$name\r\n"; my @output; $output = join('', @output); $|=1; while (<$sock>) { if ( $output = /$match/){ open(filelog, ">>searchlog.txt") || die $!; print filelog "$time $name\n"; close filelog; open(tmpl, "email.txt.asc") || print $!; @tmpl = <tmpl>; foreach $line (@tmpl) { $line =~ s/XnameX/$name/g; push @email, $line; $email_send = join('', @email); } open (TOHOST, "|$sendmail $your_email")|| print $!; print TOHOST "To: $your_email \nFrom: $your_email\n"; print TOHOST "Subject: Name Change\n"; print TOHOST "$email_send\n"; close(TOHOST); } else { open(filelog, ">>log.txt") || die $!; print filelog "$time $name\n"; close filelog; } sleep(int(43200/$n) - (time() - $time)); }

Replies are listed 'Best First'.
Re: nested loops
by lidden (Curate) on Dec 11, 2005 at 10:05 UTC
    Do:
    Loop : while(1){ # Later next Loop; }
Re: nested loops
by saintmike (Vicar) on Dec 11, 2005 at 10:05 UTC
      cheers... I just put last; in my last looped block & it returns me to while(1) loop
Re: nested loops
by serf (Chaplain) on Dec 11, 2005 at 15:50 UTC
    Not in answer to your question, (which has been answered) but where you have the block of print statements:
    print TOHOST "To: $your_email \nFrom: $your_email\n"; print TOHOST "Subject: Name Change\n"; print TOHOST "$email_send\n";
    You can easily combine those all into one:
    print TOHOST "To: $your_email \nFrom: $your_email\n", "Subject: Name Change\n", "$email_send\n";
    or if you want to see more clearly what it is sending:
    print TOHOST <<EOT; To: $your_email From: $your_email Subject: Name Change $email_send EOT
    (as this is an email you're going to need to make sure you have a blank line before the body to seperate it from the header too - but perhaps you already have this in @email before the loop)

    Also... you don't seem to be using

    use strict; use warnings;
    In the top of your script... you will do yourself big favours by using them as a matter of habit, because there are a number of errors in your example which they will pick up for you.

    You'll also massively aid your debuggging by putting in context relevent info in your error messages including filenames (quoted so you can see if they're empty or have spaces at the end or anything silly) and a short, specific description of what you were trying to do when it died.

    Presuming that you have:

    $sock, $name, $match, @email, $sendmail, $your_email
    all defined before your example here you might end up with something like:
    #!/usr/bin/perl use strict; use warnings; # (the code before your example here...) my $n = 50; while (1) { my $time = time(); print $sock "$name\r\n"; my @output; my $output = join( '', @output ); $| = 1; while (<$sock>) { if ( $output = /$match/ ) { my $searchlog = "searchlog.txt"; open( FILELOG, ">>$searchlog" ) || die "Can't write to '$searchlog': $!\n"; print FILELOG "$time $name\n"; close FILELOG; my $tmpl = "email.txt.asc"; my @tmpl; open( TMPL, $tmpl ) || print "Can't read '$tmpl': $!\n"; @tmpl = <TMPL>; close(TMPL); my ( @email_send, $email_send ); foreach my $line (@tmpl) { $line =~ s/XnameX/$name/g; push @email, $line; $email_send = join( '', @email ); } open( TOHOST, "|$sendmail $your_email" ) || print "Can't run '$sendmail': $!\n"; print TOHOST "To: $your_email \nFrom: $your_email\n", "Subject: Name Change\n", "$email_send\n"; close(TOHOST); } else { my $log_txt = "log.txt"; open( FILELOG, ">>$log_txt" ) || die "Can't write to '$log_txt': $!\n"; print FILELOG "$time $name\n"; close FILELOG; } sleep( int( 43200 / $n ) - ( time() - $time ) ); } }