Ian said:
...
If I use another variable to drive my loop, all is well. I don't
believe a subroutine should be modifying $_, should it?
Ian
As for whether a sub can modify $_, that is easily tested:
D:\junk>type junk450b.pl
$_='something';
sub1();
print "\$_ is now $_\n";
sub sub1{
$_='something else';
}
D:\junk>perl junk450b.pl
$_ is now something else
D:\junk>
And the answer is: Yes, a sub can modify $_. $_, along with all other
"punctuation" variables, is a true global -- if modified somewhere, it
is modified everywhere. A *good* *nice* sub would avoid using $_, or if
it does use it, would preserve the value of $_ and any other globals it
might change, through the use of local(). But how many folks actually
take the time and energy to do that in their subs (or even know they
should)? And how easy is it to mess up? Better to never count on $_
being preserved over a call. But nonetheless, a good module *should*
preserve globals. So I probably owe you an apology -- the bug probably
was in Net::SMTP. But it is still counting on a lot to assume that all
the stuff involved with a complicated module will actually preserve $_,
and it is still better to not count on it. What if the module takes an
execution path the testing didn't test, for example?
Hmmmmmmmm...a test program on my system does not duplicate your
behavior. Your Perl is pretty ancient (5.005_02) -- you might try a
modern version and see if the problem has been fixed. Here is a test
program almost identical to what you posted and its output:
D:\junk>type junk450a.pl
use Net::SMTP;
use Data:

umper;
$MAILSERVERS[0]=[
'sendmail',
'ondar',
'(e-mail address removed)',
'nobody@localhost',
150,
];
foreach (@MAILSERVERS) {
my($type,$server,$from,$to,$defer,$blackout)=@$_;
$statfile=$MAILSERVER_STATE . $server;
print "Connecting to $type server $server...";
$t0=[gettimeofday];
# $failed=do_smtp($server,$from,$to);
#$smtp = Net::SMTP->new('ondar');
Net::SMTP->new('ondar');
print Dumper(@MAILSERVERS);
# @MAILSERVERS is gone now
# $td=tv_interval($t0); #commented because undefined
}
D:\junk>perl junk450a.pl
Connecting to sendmail server ondar...$VAR1 = [
'sendmail',
'ondar',
'(e-mail address removed)',
'nobody@localhost',
150
];
D:\junk>ver
Windows 98 [Version 4.10.2222]
D:\junk>perl -v
This is perl, v5.8.0 built for MSWin32-x86-multi-thread
(with 1 registered patch, see perl -V for more detail)
Copyright 1987-2002, Larry Wall
Binary build 806 provided by ActiveState Corp.
http://www.ActiveState.com
Built 00:45:44 Mar 31 2003