> From: Michael Weber [mailto:[EMAIL PROTECTED]
> Sent: Friday, 5 March 2004 2:16 AM
> To: [EMAIL PROTECTED]
> Subject: Handling blank lines in while(<CONF>) construct
>
> I wrote a perl script that adds colors to text streams on the
> fly. It's
> really handy for watching log files as they run past. I watch my mail
> log files after making config changes and can mark "reject" in red,
> "spam" in blue, discard is red and my console beeps, etc. It's REALLY
> nice.
>
> However, in the configuration file, the script balks if there is a
> blank line in it. My script reads the config file into an
> array before
> beginning to parse any data. If there is a blank line, it gives this
> error:
>
> Use of uninitialized value in pattern match (m//) at
> /usr/local/bin/filter.pl line 43, <CONF> line 10.
>
Ok, which line is line 43?
I would suggest that you simply loop when detecting an empty line.
All my config file readers (since they are all different :-( )
have
next if /^$/;
and
next if /^#/;
to skip blank lines, and comment lines.
Note that the above assumes you have 'chomp()'ed the imput
otherwise the '/m' option is required.
> I can probably come up with a kludge to get it to work, but
> what is the
> correct "perl" way to handle it?
>
> Here's the loop where I read in the config file:
>
> open (CONF, $ARGV[0]) || die "Can't open config file $ARGV[0], $!\n";
>
> while (<CONF>) {
> @conf_line=split(",");
>
> push(@trigger_array, "$conf_line[0]");
> if ( $conf_line[1] =~ "red" ) {push(@color_array, "$red")}
> elsif ( $conf_line[1] =~ "yellow" ) {push(@color_array,
> "$yellow")}
> elsif ( $conf_line[1] =~ "blue" ) {push(@color_array,
> "$blue")}
> elsif ( $conf_line[1] =~ "green" ) {push(@color_array,
> "$green")}
> elsif ( $conf_line[1] =~ "cyan" ) {push(@color_array,
> "$cyan")}
> elsif ( $conf_line[1] =~ "purple" ) {push(@color_array,
Do you realise that this will match 'alonglinewiththewordpurpleinit'
and that includes 'ltpurple' which you think gets matched below??
Do you intentionally use the pattern match operator '=~' against
a string? This is actually a substring match, hence
$conf_line[1] =~ "cyan" will match cyan AND ltcyan!!
> "$purple")}
> elsif ( $conf_line[1] =~ "gray" ) {push(@color_array,
> "$gray")}
> elsif ( $conf_line[1] =~ "ltred" ) {push(@color_array,
> "$ltred")}
> elsif ( $conf_line[1] =~ "yellow" ) {push(@color_array,
> "$yellow")}
> elsif ( $conf_line[1] =~ "ltblue" ) {push(@color_array,
> "$ltblue")}
> elsif ( $conf_line[1] =~ "ltgreen" ) {push(@color_array,
> "$ltgreen")}
> elsif ( $conf_line[1] =~ "ltcyan" ) {push(@color_array,
> "$ltcyan")}
will never match..
> elsif ( $conf_line[1] =~ "ltpurple" ) {push(@color_array,
and again..
> "$ltpurple")}
> elsif ( $conf_line[1] =~ "white" ) {push(@color_array,
> "$white")}
> elsif ( $conf_line[1] =~ "ltgray" ) {push(@color_array,
> "$ltgray")}
> elsif ( $conf_line[1] =~ "beep" ) {push(@color_array,
> "$beep")}
> else {push(@color_array, "$white")}
> }
May I suggest:
%color_map = ();
@color_map{ qw( red yellow green blue ) } =
( $red, $yellow, $green, $blue );
Then when comparing :-
push @color_array, (defined $conf_line[1] &&
exists $color_map{$conf_line[1])) ?
$color_map{$conf_line[1]) :
$white;
I'd love you to post the finished product. Please put the
color information in a config file in a section based on
terminal type. I would like to use it on vt320 vt100, xterm
and linux console :-)
Cheers.
>
> close (CONF);
>
> BTW. If anyone wants this script, I will re-post it after fixing this
> bug. I gave it as a gift to this list last year as a present for all
> the help I get from y'all.
>
> Thanx!
>
> -Michael
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>