in reply to Re: Propose addition to use warnings?
in thread Propose addition to use warnings?

Indeed haukex, I agree that validation for a filehandle would be good if it was a trivial thing to do. I couldn't have told you whether it was or wasn't off the top of my head, so thanks for the link.

The possible re-use of the scalar $first_fh did occur to me, but that's why I initially figured a warning might have been appropriate since someone could explicitly ignore that warning if they decided to re-use the variable name. I sort of leaned toward the, "well wouldn't that be kind of sloppy to re-use it anyway?" argument when it occurred to me, but I wouldn't be surprised if there were cases where it made good sense to do so.

Again, I did just really want the "filehandle used after closing" warning, though I can easily accept the answer that it's impractical.

Just another Perl hooker - will code for food

Replies are listed 'Best First'.
Re^3: Propose addition to use warnings?
by haukex (Archbishop) on Nov 28, 2016 at 21:17 UTC

    Hi perldigious,

    ... I wouldn't be surprised if there were cases where it made good sense to do so.

    I think choroba showed an excellent example of code where it's perfectly ok to pass a closed filehandle to a sub. I think usually it's the job of modules like Perl::Critic to enforce a certain coding style, not the job of warnings.

    I agree that validation for a filehandle would be good if it was a trivial thing to do.

    The code I showed with Scalar::Util will work in many cases, but not every single one. So it really depends on whether you're coding a module which you intend to release to many users, where the chances are good that someone might try to pass in a valid object that fails the test, or whether you're coding "just" a script where you've got a good overview of the values that are being passed to the sub. If it's the latter, then my approach would probably be to just use the code I showed, and if someday I decide I want to pass in something that fails the check, I can always reevaluate then.

    Regarding your other reply about opening the files early in your scripts, note you could use file tests to check on the files at the start of the script so you don't have to open them right away. There is a small chance they might get deleted in between the file test and the open, but there's plenty of situations where one can safely assume that won't happen.

    Regards,
    -- Hauke D