What's the best way to open and read a file in Perl?

前端 未结 12 1940
醉话见心
醉话见心 2020-11-27 13:11

Please note - I am not looking for the \"right\" way to open/read a file, or the way I should open/read a file every single time. I am just interested to find out what way m

相关标签:
12条回答
  • 2020-11-27 13:12

    If these programs are just for your productivity, whatever works! Build in as much error handling as you think you need.

    Reading in a whole file if it's large may not be the best way long-term to do things, so you may want to process lines as they come in rather than load them up in an array.

    One tip I got from one of the chapters in The Pragmatic Programmer (Hunt & Thomas) is that you might want to have the script save a backup of the file for you before it goes to work slicing and dicing.

    0 讨论(0)
  • 2020-11-27 13:12

    Damian Conway does it this way:

    $data = readline!open(!((*{!$_},$/)=\$_)) for "filename";
    

    But I don't recommend that to you.

    0 讨论(0)
  • 2020-11-27 13:15

    If you want the entire file as a single string, there's no need to iterate through it.

    use strict;
    use warnings;
    use Carp;
    use English qw( -no_match_vars );
    my $data = q{};
    {
       local $RS = undef; # This makes it just read the whole thing,
       my $fh;
       croak "Can't open $input_file: $!\n" if not open $fh, '<', $input_file;
       $data = <$fh>;
       croak 'Some Error During Close :/ ' if not close $fh;
    }
    

    The above satisfies perlcritic --brutal, which is a good way to test for 'best practices' :). $input_file is still undefined here, but the rest is kosher.

    0 讨论(0)
  • 2020-11-27 13:17

    The || operator has higher precedence, so it is evaluated first before sending the result to "open"... In the code you've mentioned, use the "or" operator instead, and you wouldn't have that problem.

    open INPUT_FILE, "<$input_file"
      or die "Can't open $input_file: $!\n";
    
    0 讨论(0)
  • 2020-11-27 13:18

    Having to write 'or die' everywhere drives me nuts. My preferred way to open a file looks like this:

    use autodie;
    
    open(my $image_fh, '<', $filename);
    

    While that's very little typing, there are a lot of important things to note which are going on:

    • We're using the autodie pragma, which means that all of Perl's built-ins will throw an exception if something goes wrong. It eliminates the need for writing or die ... in your code, it produces friendly, human-readable error messages, and has lexical scope. It's available from the CPAN.

    • We're using the three-argument version of open. It means that even if we have a funny filename containing characters such as <, > or |, Perl will still do the right thing. In my Perl Security tutorial at OSCON I showed a number of ways to get 2-argument open to misbehave. The notes for this tutorial are available for free download from Perl Training Australia.

    • We're using a scalar file handle. This means that we're not going to be coincidently closing someone else's file handle of the same name, which can happen if we use package file handles. It also means strict can spot typos, and that our file handle will be cleaned up automatically if it goes out of scope.

    • We're using a meaningful file handle. In this case it looks like we're going to write to an image.

    • The file handle ends with _fh. If we see us using it like a regular scalar, then we know that it's probably a mistake.

    0 讨论(0)
  • 2020-11-27 13:18

    For OO, I like:

    use FileHandle;
    ...
    my $handle = FileHandle->new( "< $file_to_read" );
    croak( "Could not open '$file_to_read'" ) unless $handle;
    ...
    my $line1 = <$handle>;
    my $line2 = $handle->getline;
    my @lines = $handle->getlines;
    $handle->close;
    
    0 讨论(0)
提交回复
热议问题