perl - (empty?) return of readline is not caught by control structure -
i have multidimensional hash containing opened file handles on seek_end intention read latest line without getting i/o (what tail).
i'm going through of these handles for loop , calling readline on them.
it looks this:
for $outer ( keys %config ) { $line = readline($config{$outer}{"filehandle"}); if (not defined $line || $line eq '' ){ next; } else{ print "\nline: -->".$line."<--\n"; $line =~ m/(:)(\d?\.?\d\d?\d?\d?\d?)/; $wert = $2; } } if new content written these files, script reads , behaves planned.
the problem readline return nothing because there nothing @ end of file, if doesn't seem identify empty return of readline undef empty -- prints nothing, right because there nothing in string, don't want processed @ all.
this operator precedence problem. have used mixture of low-priority not high-priority || condition
not defined $line || $line eq '' is parsed as
not( defined($line) || ($line eq '') ) which wrongly negates $line eq '' part
it safer use lower-priority and , or, , not on &&, ||, , !, mixture bad idea
you can write either
if (not defined $line or $line eq '' ) { ... } or
if ( ! defined $line || $line eq '' ) { ... } then well
i prefer see written this, because loses unnecessary else clause , next statements, , discards lines contain space characters
also note iterate on values of hash. using keys wasteful when used access values. able think of better name loop control variable $item
and there's no need concatenation operator when perl interpolate variables directly double-quoted strings
for $item ( values %config ) { $line = readline( $item->{filehandle} ); if ( defined $line , $line =~ /\s/ ) { print "\nline: -->$line<--\n"; $line =~ m/(:)(\d?\.?\d\d?\d?\d?\d?)/; $wert = $2; } }
Comments
Post a Comment