in reply to nested loops
You can easily combine those all into one:print TOHOST "To: $your_email \nFrom: $your_email\n"; print TOHOST "Subject: Name Change\n"; print TOHOST "$email_send\n";
or if you want to see more clearly what it is sending:print TOHOST "To: $your_email \nFrom: $your_email\n", "Subject: Name Change\n", "$email_send\n";
(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)print TOHOST <<EOT; To: $your_email From: $your_email Subject: Name Change $email_send EOT
Also... you don't seem to be using
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.use strict; use warnings;
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:
all defined before your example here you might end up with something like:$sock, $name, $match, @email, $sendmail, $your_email
#!/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 ) ); } }
|
|---|