in reply to Propose addition to use warnings?

Hi perldigious,

So as I understand it you would like the warning to be triggered when you use $first_fh as a parameter to my_sub? Note that's it's perfectly fine to re-use $first_fh after it was closed, so the warning can't just be "filehandle used after closing" or something similar, the trigger needs to be something more specific. But how Perl should know that the first parameter to my_sub needs to be an opened filehandle? This sounds like a case for parameter validation to me. At the top of my_sub: my $fh = shift; croak "argument must be an opened filehandle" unless ref $fh eq 'GLOB' && $fh->opened;. Update: Probably better: At the top of your script use Scalar::Util qw/openhandle/;, and at the top of my_sub: croak "argument must be an open filehandle" unless openhandle($fh); (Update 2: But see also Best way to check if something is a file handle?, identifying every kind filehandle-like thing appears to be nontrivial.)

Regards,
-- Hauke D

Replies are listed 'Best First'.
Re^2: Propose addition to use warnings?
by perldigious (Priest) on Nov 28, 2016 at 18:58 UTC

    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

      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