SPUG: Should I not do while (<FOO> !~ /^mailboxes/) { } ?
Chris Wilkes
cwilkes-spug at ladro.com
Fri Feb 22 16:11:44 CST 2002
Hello everyone,
I wanted to read in a mutt configuration file and only pull out one of
the lines that starts with mailboxes so I did this:
#!/usr/bin/perl -w
$file = ".muttrc";
open (FOO, $file) || die "Can't open $file\n";
while (<FOO> !~ /^mailboxes/) {
print "#$_\n";
}
close FOO;
print "$_\n";
To my surprise just a bunch of pound signs show up. $_ has been
replaced with nothing in the loop; also exiting the loop $_ is set to
nothing. There are warnings about "use of an uninitialized value in
concatentation"
Then I decided to go about the easy way of fixing this:
while (($in = <FOO>) !~ /^mailboxes/) { } # Style 2
which gave me $in on the exit. However being the experimental type I
decided to do this:
while (($_ = <FOO>) !~ /^mailboxes/) { } # Style 3
now in the loop and exiting $_ is set to the right thing, that is each
line of the .muttrc file.
I think the warning happens as the magic variable <FOO> doesn't match
^mailboxes so it complains on the "print $_" line as there is nothing in
there. However it does exit at the right time when it hits ^mailboxes
so I would expect it to be in $_ upon exit.
Can someone shed some light on this?
Also when I did this with <DATA> and __DATA__ instead of the
filehandle FOO it complains about a pattern match in addition to the
concatenation error when doing it in Style #3, where it didn't complain
before with the opened file. The script also loops on forever if it
can't (not) match the ^mailboxes.:
#!/usr/bin/perl -w
while (($_ = <DATA>) !~ /^mailboxes/) {
print "#$_\n";
}
__DATA__
The quick brown fox
jumped over
the lazy dog
That boggles me too.
Chris
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
Subscriptions; Email to majordomo at pm.org: ACTION LIST EMAIL
Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
For daily traffic, use spug-list for LIST ; for weekly, spug-list-digest
Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org
More information about the spug-list
mailing list