While your code works (edit: well, when you add the missing ';' at least) there are some things you can do to make your life easier. The first one is to use a scalar instead of a bareword for the file handle (use a variable starting with a $ rather than the word BEDROCK). Second, you can have the open mode in a separate argument rather than with the file path. Third, while selecting the handle before opening it works, it's clearer if you do it after. So:

select BEDROCK; # BEDROCK doesn't exist yet open BEDROCK, ">>/home/barney/.error_log";
Becomes:
open my $bedrock, ">>", "/home/barney/.error_log"; select $bedrock;
It will make it much easier for perl to tell you when you have made a typo (if you use strict). Otherwise, typing BEDROCK once and BERDOCK the second time will compile but fail to work.

Also, it's considered good practice to avoid having things happen inside the condition of an if statement (it's ok to check things, but not to change them). The open line could be rewritten like this:

open my $bedrock, ">>", "/home/barney/.error_log" or die "Can't open f +ile: $!";
(as a matter of fact, that's how I do it most of the time). But you could also add use autodie; at the top of your program, and it will make report errors automatically, without you having to write the die part by hand.


In reply to Re: Open Error by Eily
in thread Open Error by catfish1116

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, details, 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, summary, 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.