On Thu, Jan 08, 2009 at 06:47:05PM -0800, John W. Krahn wrote: > root wrote: > > The following script gives me confusing results. > >I've not delved into OOP before and am surprised when something > >appears to work but gives wrong answers.
<snip> > > > >foreach( @ARGV) > >{ $target = $_; > > More proper as: > > foreach my $target ( @ARGV ) > You're absolutely right. My excuse (you knew I'd have one) is that this was cut from a much larger application (dir compare) where $target needed broader scope. > > if( $useOO) > > { open(FILE, $target) or die "Can't open '$target': $!"; > > binmode(FILE); > > With modern versions of Perl you can use lexically scoped filehandles > and the three argument form of open() which can include binmode in the mode: > > { open(my $FILE, '<:raw', $target) or die "Can't open '$target': > $!"; > I knew about the three argument form of open() but somehow missed ':raw'. Thanks for pointing it out. <snip> > > local $/; # slurp mode > > > > return '0' if( ! -e $file); > > open INFILE, "<$file" || die "Unable to open $file: $! stopped "; > > You have a precedence problem as the high precedence of the '||' > operator means that this will not die() even if open() fails. You need > to either use parentheses for open's arguments or use the low precedence > 'or' operator. I've had that (|| vs or) in my notes (Camel p.191) for three or four weeks now but hadn't run across it when I first wrote the script. > Also, I wouldn't use a file test operator because the result from open() > will already tell you whether the file exists or not. > If I recall, the test is in there as protection against running md5sum against a broken link. That part didn't make it into the part you saw. Oops, my bad. > > > binmode(INFILE); > > my $data = <INFILE> ; > > close INFILE; > > if ( $myDebug ) # this gives wrong answers > > { $digest = Digest::MD5->md5_hex($data); } > > else > > { $digest = md5_hex($data); } > > $digest; > >} > >__END__ > > > John > -- > Those people who think they know everything are a great > annoyance to those of us who do. -- Isaac Asimov One of my favorite authors. Thanks for the critique, Mike -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/