I happened to notice an SoPW node where the OP's code passed
$ARGV[0] to "open()" without checking if there was any value there, and I was intrigued about what Perl does with this sort of potential error. When I tried it out, I got a surprise:
#!/usr/bin/perl -w
## test 1: open for input
open( I, $ARGV[0] ) or die $!;
while(<I>) {print}
If I run that with no args (@ARGV empty), it sends a warning ("use of uninitialized value in open") and dies ("no such file or directory"). But then:
#!/usr/bin/perl -w
## test 2: open for output
open( O, '>', $ARGV[0] ) or die $!;
print O "this is a test\n";
If I run that with no args, I get no warning, no die, and no output file -- no output of any sort, anywhere.
Now, if I rephrase that second open statement to the 2-arg version -- open( O, ">$ARGV[0]" ) or die $!; -- then I get the expected warning and dying messages.
For that matter, if I convert the first test to use a three-arg open -- open( I, '<', $ARGV[0] ) -- I don't get any warning or die (and "use warnings" doesn't help, either).
I find this a bit disturbing (same behavior on macosx 5.8.1 and freebsd 5.8.6). Am I missing something?
update: special thanks to Transient for an illuminating tour of the relevant C functions in the Perl code base.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.