SPUG: Should I not do while (<FOO> !~ /^mailboxes/) { } ?
Dean Hudson
dean at ero.com
Fri Feb 22 16:41:43 CST 2002
If you want $_ to get set you should probably do something more like:
while (<FOO>) {
next if /^mailboxes/;
}
while (<FOO> !~ /^mailboxes/) does not set implicitly set $_ the way
you hope.
It's not really clear to me what you're trying to do exactly. Do you
want to print the mailboxes line with a prepended # or the other lines
with a # prepended.
Here's another way you could do either:
dean at apogee:~ > perl -e 'grep {/^mailboxes/ && print "#$_" } <>';
this
is
a
test
mailboxes
test
#mailboxes
dean at apogee:~ > perl -e 'grep {!/^mailboxes/ && print "#$_" } <>';
this
is
a
test
mailboxes
test
#this
#is
#a
#test
#test
dean at apogee:~ >
dean.
On Fri, Feb 22, 2002 at 02:11:44PM -0800, Chris Wilkes wrote:
> 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
>
>
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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