Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

++ For making this :)

Unless you are using autodie, I'd say: always use a temporary variable for the filename, to use both in the call to open and in the error message. This is to avoid having an error message that says that file A is missing, or can't be read, when you were actually trying to read file B, and to avoid looking in the wrong folder. Two examples (which I've both been guilty of):

open my $input, '<', "my_input.xml" or die "Can't open my_input.csv: $ +!"; # Whoops, my_input.xml does not exist by my_input.csv file does my $input_file = "my_input.xml"; open my $input, '<', $input_file or die "Can't open $input_file: $!"; +# Still doesn't work but the error message is correct
my $file = get_file(); my $folder = get_folder(); # The function returned an empty string by +mistake open my $data, '<', "$folder/$file" or die "Can't open $file; $!"; # B +ut I see the file my folder, why does it say it doesn't exist? my $input_file = "$folder/$input"; open my $data, '<', $input_file or die "Can't open $input_file: $!"; # + You'll see straightaway that you the folder is empty

Also, that's not really specific to open but try to give meaningful names to your variables. Having to use numbers is nearly always a sign that the name is wrong. $input and $output is always better than $file and $file2. $reference_data and $new_data is also better than $fh1 and $fh2.


In reply to Re: "open" Best Practices by Eily
in thread "open" Best Practices by haukex

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (8)
    As of 2020-02-19 17:48 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      What numbers are you going to focus on primarily in 2020?










      Results (84 votes). Check out past polls.

      Notices?