in reply to Filehandle open?

You'll get a warning if you try to tell() on that filehandle but that warning can be suppressed, for we know what we're doing. An unopened handle will return -1 as position:

sub opened($) { my $fh = shift; no warnings; #turn 'em off locally return tell($fh)==-1 ? undef : 1 }

HTH

--
http://fruiture.de

Replies are listed 'Best First'.
Re^2: Filehandle open?
by Aristotle (Chancellor) on Sep 24, 2002 at 13:43 UTC
    And since we're using the pragma, we can also be more specific about which warnings we don't want. Then we can lose the prototype for errorchecking too (which it only does shoddily anyway). And lastly, undef is a one-element list and evaluates as true in list context, so we just let Perl decide what value the boolean expression should return.
    sub is_opened { no warnings qw(closed unopened); return tell $_[0] != -1; }

    Update: Oops. s/==/!=/ - otherwise the truth value is reversed of course. Thanks, blakem.

    Update 2: and of course, check perldoc perllexwarn about the detailed warning names you can use to enable/disable specifically.

    Update 3 (the neverending story): fruiture informs me that you have to disable not only the closed category, but unopened as well. Code updated accordingly.

    Makeshifts last the longest.

Re: Re: Filehandle open?
by maclypse (Initiate) on Sep 24, 2002 at 13:37 UTC
    no warnings;

    Kinky. I never saw that piece of code before. I like it though. Thank you.

    -- Bow before Zod!