Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3394494
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:10:37+00:00 2026-05-18T04:10:37+00:00

I have some subroutines that I call like this myWrite($fileName, \@data) . myWrite() opens

  • 0

I have some subroutines that I call like this myWrite($fileName, \@data). myWrite() opens the file and writes out the data in some way. I want to modify myWrite so that I can call it as above or with a filehandle as the first argument. (The main reason for this modification is to delegate the opening of the file to the calling script rather than the module. If there is a better solution for how to tell an IO subroutine where to write, i’d be glad to hear it.)

In order to do this, I must test whether the first input var is a filehandle. I figured out how to do that by reading this question.

Now here’s my question: I also want to test whether I can write to this filehandle. I can’t figure out how to do that.

Here’s what I want to do:

sub myWrite {
  my ($writeTo, $data) = @_;
  my $fh;
  if (isFilehandle($writeTo)) { # i can do this
    die "you're an immoral person\n" 
      unless (canWriteTo($writeTo)); # but how do I do this?
    $fh = $writeTo;
  } else {
    open $fh, ">", $writeTo;
  }
  ...
}

All I need to know is if I can write to the filehandle, though it would be nice to see some general solution that tells you whether you’re filehandle was opened with “>>” or “<“, or if it isn’t open, etc.

(Note that this question is related but doesn’t seem to answer my question.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:10:37+00:00Added an answer on May 18, 2026 at 4:10 am

    Detecting Openness of Handles

    As Axeman points out, $handle->opened() tells you whether it is open.

    use strict;
    use autodie;
    use warnings qw< FATAL all >;
    use IO::Handle;
    use Scalar::Util qw< openhandle >;
    
    our $NULL = "/dev/null";
    open NULL;
    printf "NULL is %sopened.\n", NULL->opened() ? "" : "not ";
    printf "NULL is %sopenhandled.\n", openhandle("NULL") ? "" : "not ";
    printf "NULL is fd %d.\n", fileno(NULL);
    

    produces

    NULL is opened.
    NULL is not openhandled.
    NULL is fd 3.
    

    As you see, you cannot use Scalar::Util::openhandle(), because it is just too stupid and buggy.

    Open Handle Stress Test

    The correct approach, if you were not using IO::Handle->opened, is demonstrated in the following simple little trilingual script:

    eval 'exec perl $0 ${1+"$@"}'
                   if 0;
    
    use 5.010_000;
    use strict;
    use autodie;
    use warnings qw[ FATAL all ];
    
    use Symbol;
    use IO::Handle;
    
    #define exec(arg)
    BEGIN { exec("cpp $0 | $^X") } #!/usr/bin/perl -P
    #undef  exec
    
    #define SAY(FN, ARG) printf("%6s %s => %s\n", short("FN"), q(ARG), FN(ARG))
    #define STRING(ARG)  SAY(qual_string, ARG)
    #define GLOB(ARG)    SAY(qual_glob, ARG)
    #define NL           say ""
    #define TOUGH        "hard!to!type"
    
    sub comma(@);
    sub short($);
    sub qual($);
    sub qual_glob(*);
    sub qual_string($);
    
    $| = 1;
    
    main();
    exit();
    
    sub main { 
    
        our $GLOBAL = "/dev/null";
        open GLOBAL;
    
        my $new_fh = new IO::Handle;
    
        open(my $null, $GLOBAL);
    
        for my $str ($GLOBAL, TOUGH) {
            no strict "refs";
            *$str = *GLOBAL{IO};
        }
    
        STRING(  *stderr       );
        STRING(  "STDOUT"      );
        STRING(  *STDOUT       );
        STRING(  *STDOUT{IO}   );
        STRING( \*STDOUT       );
        STRING( "sneezy"       );
        STRING( TOUGH );
        STRING( $new_fh        );
        STRING( "GLOBAL"       );
        STRING( *GLOBAL        );
        STRING( $GLOBAL        );
        STRING( $null          );
    
        NL;
    
        GLOB(  *stderr       );
        GLOB(   STDOUT       );
        GLOB(  "STDOUT"      );
        GLOB(  *STDOUT       );
        GLOB(  *STDOUT{IO}   );
        GLOB( \*STDOUT       );
        GLOB(  sneezy        );
        GLOB( "sneezy"       );
        GLOB( TOUGH );
        GLOB( $new_fh        );
        GLOB(  GLOBAL        );
        GLOB( $GLOBAL        );
        GLOB( *GLOBAL        );
        GLOB( $null          );
    
        NL;
    
    }
    
    sub comma(@) { join(", " => @_) }
    
    sub qual_string($) { 
        my $string = shift();
        return qual($string);
    } 
    
    sub qual_glob(*) { 
        my $handle = shift();
        return qual($handle);
    } 
    
    sub qual($) {
        my $thingie = shift();
    
        my $qname = qualify($thingie);
        my $qref  = qualify_to_ref($thingie); 
        my $fnum  = do { no autodie; fileno($qref) };
        $fnum = "undef" unless defined $fnum;
    
        return comma($qname, $qref, "fileno $fnum");
    } 
    
    sub short($) {
        my $name = shift();
        $name =~ s/.*_//;
        return $name;
    } 
    

    Which when run produces:

    string    *stderr        => *main::stderr, GLOB(0x8368f7b0), fileno 2
    string    "STDOUT"       => main::STDOUT, GLOB(0x8868ffd0), fileno 1
    string    *STDOUT        => *main::STDOUT, GLOB(0x84ef4750), fileno 1
    string    *STDOUT{IO}    => IO::Handle=IO(0x8868ffe0), GLOB(0x84ef4750), fileno 1
    string   \*STDOUT        => GLOB(0x8868ffd0), GLOB(0x8868ffd0), fileno 1
    string   "sneezy"        => main::sneezy, GLOB(0x84169f10), fileno undef
    string   "hard!to!type"  => main::hard!to!type, GLOB(0x8868f1d0), fileno 3
    string   $new_fh         => IO::Handle=GLOB(0x8868f0b0), IO::Handle=GLOB(0x8868f0b0), fileno undef
    string   "GLOBAL"        => main::GLOBAL, GLOB(0x899a4840), fileno 3
    string   *GLOBAL         => *main::GLOBAL, GLOB(0x84ef4630), fileno 3
    string   $GLOBAL         => main::/dev/null, GLOB(0x7f20ec00), fileno 3
    string   $null           => GLOB(0x86f69bb0), GLOB(0x86f69bb0), fileno 4
    
      glob    *stderr        => GLOB(0x84ef4050), GLOB(0x84ef4050), fileno 2
      glob     STDOUT        => main::STDOUT, GLOB(0x8868ffd0), fileno 1
      glob    "STDOUT"       => main::STDOUT, GLOB(0x8868ffd0), fileno 1
      glob    *STDOUT        => GLOB(0x8868ffd0), GLOB(0x8868ffd0), fileno 1
      glob    *STDOUT{IO}    => IO::Handle=IO(0x8868ffe0), GLOB(0x84ef4630), fileno 1
      glob   \*STDOUT        => GLOB(0x8868ffd0), GLOB(0x8868ffd0), fileno 1
      glob    sneezy         => main::sneezy, GLOB(0x84169f10), fileno undef
      glob   "sneezy"        => main::sneezy, GLOB(0x84169f10), fileno undef
      glob   "hard!to!type"  => main::hard!to!type, GLOB(0x8868f1d0), fileno 3
      glob   $new_fh         => IO::Handle=GLOB(0x8868f0b0), IO::Handle=GLOB(0x8868f0b0), fileno undef
      glob    GLOBAL         => main::GLOBAL, GLOB(0x899a4840), fileno 3
      glob   $GLOBAL         => main::/dev/null, GLOB(0x7f20ec00), fileno 3
      glob   *GLOBAL         => GLOB(0x899a4840), GLOB(0x899a4840), fileno 3
      glob   $null           => GLOB(0x86f69bb0), GLOB(0x86f69bb0), fileno 4
    

    That is how you test for open file handles!

    But that wasn’t even your question, I believe.

    Still, I felt it needed addressing, as there are too many incorrect solutions to that problem floating around here. People need to open their eyes to how these things actually work. Note that the two functions from Symbol use the caller’s package if necessary—which it certainly often is.

    Determining Read/Write Mode of Open Handle

    This is the answer to your question:

    #!/usr/bin/env perl
    
    use 5.10.0;
    use strict;
    use autodie;
    use warnings qw< FATAL all >;
    
    use Fcntl;
    
    my (%flags, @fh);
    my $DEVICE  = "/dev/null";
    my @F_MODES = map { $_ => "+$_" } qw[ < > >> ];
    my @O_MODES = map { $_ | O_WRONLY }
            O_SYNC                          ,
                     O_NONBLOCK             ,
            O_SYNC              | O_APPEND  ,
                     O_NONBLOCK | O_APPEND  ,
            O_SYNC | O_NONBLOCK | O_APPEND  ,
        ;
    
       open($fh[++$#fh], $_, $DEVICE) for @F_MODES;
    sysopen($fh[++$#fh], $DEVICE, $_) for @O_MODES;
    
    eval { $flags{$_} = main->$_ } for grep /^O_/, keys %::;
    
    for my $fh (@fh) {
        printf("fd %2d: " => fileno($fh));
        my ($flags => @flags) = 0+fcntl($fh, F_GETFL, my $junk);
        while (my($_, $flag) = each %flags) {
            next if $flag == O_ACCMODE;
            push @flags => /O_(.*)/ if $flags & $flag;
        }
        push @flags => "RDONLY" unless $flags & O_ACCMODE;
        printf("%s\n",  join(", " => map{lc}@flags));
    }
    
    close $_ for reverse STDOUT => @fh;
    

    Which, when run, produces this output:

    fd  3: rdonly
    fd  4: rdwr
    fd  5: wronly
    fd  6: rdwr
    fd  7: wronly, append
    fd  8: rdwr, append
    fd  9: wronly, sync
    fd 10: ndelay, wronly, nonblock
    fd 11: wronly, sync, append
    fd 12: ndelay, wronly, nonblock, append
    fd 13: ndelay, wronly, nonblock, sync, append
    

    Happy now, Schwern? ☺

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some classes layed out like this class A { public virtual void
We have some input data that sometimes appears with &nbsp characters on the end.
I have some code like this in a winforms app I was writing to
Let's say I have a subroutine/method that a user can call to test some
I have a perl array of to-do tasks that looks like this: @todos =
I have created a user defined type to contain some data that I will
I often have a subroutine in Perl that fills an array with some information.
I have some UI in VB 2005 that looks great in XP Style, but
I have some kind of test data and want to create a unit test
We have some files on our website that users of our software can download.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.