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

I've scoured my llama book and my camel book for a clue as to why this code is giving me a syntax error. I'm fairly new to perl coding, so I hope I'm not overlooking something painfully obvious. Also, this is my first post here, so if I've done something wrong, please go easy on me. Here is the code:
(defined $debug) && foreach my $key (sort keys %option) { defined ($option{$key}) && (print (__LINE__ . ": $ +key = $option{$key}\n")) };
and it gives me this error ...
syntax error at ./documents/scripts/dspr.pl line 92, near "foreach "
perl 5.6.0, and so on. This makes no sense to me... does it make sense to you? Thanks!

Replies are listed 'Best First'.
Re: why doesn't this foreach loop work?
by chipmunk (Parson) on Jan 07, 2001 at 22:58 UTC
    foreach is a statement, not an expression. It cannot be used as an operand of && or any other operator. Try this instead:
    if (defined $debug) { foreach my $key (sort keys %option) { print __LINE__ . ": $key = $option{$key}\n" if defined($option{$key}); } }
      thank you and bless you. that works. :-)
A reply falls below the community's threshold of quality. You may see it by logging in.