in reply to Propose addition to use warnings?

This isn't likely to go anywhere. Here's why:

Unless a prototype disallows it, it's always legal to pass an in-scope lexical scalar variable to a subroutine call. Perl doesn't really care at compiletime what the subroutine actually does with parameters passed into it. So the only safeguard Perl can reasonably do is complain if you make some system call that requires a filehandle, and pass in a closed one. That's a runtime check though, because the compiler doesn't grok the logic enough to know that a given variable is in some particular state.

But Perl already does complain when you attempt to do some file operation on a closed filehandle -- that already generates a warning. In your case, the situation comes up infrequently, so the warning is latent, and not likely to appear right away. But I do think Perl is doing the best it can in that regard already. At what line in the code you posted should Perl kknow you're doing something nutty?

my_sub($first_fh,...

...that's not a reasonable place, because Perl doesn't really know the purpose of my_sub, and doesn't know at this point what you are planning to do with $first_fh.


Dave

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

    Thanks davido, I figured I may be asking for too much, but I wasn't sure so I asked the Monastery.

    ...that's not a reasonable place, because Perl doesn't really know the purpose of my_sub, and doesn't know at this point what you are planning to do with $first_fh.

    That is where I would have requested the warning to be given, basically a "you already closed this filehandle, are you sure you still want to be using it for anything at all?" sort of warning. If it's not reasonable it's not reasonable. I suppose I'm just being the guy trying to blame his tools when it's actually his own ineptitude at using them that's the problem. :-)

    Just another Perl hooker - will code for food

      I didn't mean for "not reasonable" to sound like your request is unreasonable. Just that setting the warning there isn't ideal because my_sub could be something like:

      sub my_sub { my $closed_handle = shift; die "For some reason our handle isn't closed yet." if $closed_handle->opened; }

      ...or

      close $fh or my_sub($fh); sub my_sub { my $handle = shift; my $error = $handle->error; die "Got this error code: $error\n" if $error; }

      ...or even...

      # Open a handle only if it's not opened. sub my_sub { open $_[0], '<', $_[1] if !$_[0]->opened; }

      Dave