John W. Krahn wrote:
> Rob Dixon wrote:
>>
>> John W. Krahn wrote:
>>>
>>> You should _always_ verify that the file opened successfully.
>>
>> Sure, but that's not what the question was about. You should always
>> add 'use strict' and 'use warnings' too, but I didn't put that in
>> either.
>>
>>> my $contents = do {
>>>         open my $fh, 'file.txt' or die "Cannot open 'file.txt' $!";
>>>         local $/; <$fh> };
>>
>> Yes, so would I. But it's more likely to confuse than assist I think.
>
> So, reading from an unopened filehandle would not be confusing?  IHMO
> when dealing with a person who is not as familiar with Perl, it is
> better to use "correct" code examples[1].  If the OP does not
> understand they can always ask subsequent questions here.

It's hard to judge how far to stray from 'plain and simple' code. 'use
warnings' will indicate a read from a closed filehandle, but I mostly
meant the stuff you introduced in your code:

- do { .. }
- declaration of $fh at the point of use
- anonymous filehandles
- implicit file closure

Now while that's about what I would have written myself, and none of it
may be a problem for the original poster, it's not directly relevant to
the question. I chose to code it all more explicitly for that reason.
Having said that, I've often posted saying 'it may be of interest to see
how concise that could be...', so I guess I agree with you over all.
Even so, if it came to a choice I'd prefer to push 'use warnings'
(together with 'use strict') over 'open or die'.

All IMHO ;-)

Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to