Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: Best way to compare range of characters in two text files

by sundialsvc4 (Abbot)
on Jul 12, 2018 at 16:54 UTC ( #1218391=note: print w/replies, xml ) Need Help??


in reply to Re: Best way to compare range of characters in two text files
in thread Best way to compare range of characters in two text files

While I am quite sure that this terse bit of code has been tested and that it does what it is supposed to do, it is nonetheless ... terse.   Why not simply say what you mean?

(P.S. Oops, read .. or doesn’t actually work.   Me bad, but you get the idea.)

open(my $fh1, '<', $file1) or die("Can't open $file1"); open(my $fh2, '<', $file2) or die("Can't open $file2"); my $offset = 0; my $length = 50; read($fh1, my $rec1, $length, $offset) or die("Can't read $file1"); read($fh2, my $rec2, $length, $offset) or die("Can't read $file2"); if ($rec1 eq $rec2) { ... }
Yes, it’s a few lines longer.   No doubt some would say it is less efficient.   But you don’t have to scramble for perldoc perlvar to know what it’s doing.   Any programmer would recognize it, no matter how familiar they were with Perl-5, and it would be easy to selectively patch it.

Replies are listed 'Best First'.
Re^3: Best way to compare range of characters in two text files
by AnomalousMonk (Bishop) on Jul 12, 2018 at 17:26 UTC

    Somebody needs to scramble for  perldoc -f read because

    read FILEHANDLE,SCALAR,LENGTH,OFFSET
    ... Returns the number of characters actually read, 0 at end of file, or undef if there was an error (in the latter case $! is also set). ... [emphases added]

    In this particular case, you're probably not going to be sitting at the end of the file just after opening it (unless the file's empty) and only one read is ever done, but a statement like
        read($fh1, my $rec1, $length, $offset) or die("Can't read $file1");
    will die at the end of the file, and that's not proper handling of an error condition. (It would have been nice to have had  $! in the die message, too. Some study of the purpose of the OFFSET parameter might also be profitable.)


    Give a man a fish:  <%-{-{-{-<

      A reply falls below the community's threshold of quality. You may see it by logging in.
Re^3: Best way to compare range of characters in two text files
by Anonymous Monk on Jul 12, 2018 at 17:26 UTC

    Just because it's possible to write Pascal in Perl doesn't mean it's advisable.

      A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1218391]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2019-12-15 12:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?