in reply to Open Error
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:
Becomes:select BEDROCK; # BEDROCK doesn't exist yet open BEDROCK, ">>/home/barney/.error_log";
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.open my $bedrock, ">>", "/home/barney/.error_log"; select $bedrock;
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:
(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.open my $bedrock, ">>", "/home/barney/.error_log" or die "Can't open f +ile: $!";
|
|---|