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 161869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:17:24+00:00 2026-05-11T11:17:24+00:00

You are given either an IO::File object or a typeglob ( \*STDOUT or Symbol::symbol_to_ref(main::FH)

  • 0

You are given either an IO::File object or a typeglob (\*STDOUT or Symbol::symbol_to_ref('main::FH')); how would you go about determining if it is a read or write handle? The interface cannot be extended to pass this information (I am overriding close to add calls to flush and sync before the actual close).

Currently I am attempting to flush and sync the filehandle and ignoring the error 'Invalid argument' (which is what I get when I attempt to flush or sync a read filehandle):

eval { $fh->flush; 1 } or do {         #this seems to exclude flushes on read handles         unless ($! =~ /Invalid argument/) {                 croak 'could not flush $fh: $!';         } };  eval { $fh->sync; 1 } or do {         #this seems to exclude syncs on read handles         unless ($! =~ /Invalid argument/) {                 croak 'could not sync $fh: $!';         } }; 
  • 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. 2026-05-11T11:17:25+00:00Added an answer on May 11, 2026 at 11:17 am

    Have a look at the fcntl options. Maybe F_GETFL with O_ACCMODE.

    Edit: I did a little googling and playing over lunch and here is some probably non-portable code but it works for my Linux box, and probably any Posix system (perhaps even Cygwin, who knows?).

    use strict; use Fcntl; use IO::File;  my $file; my %modes = ( 0 => 'Read only', 1 => 'Write only', 2 => 'Read / Write' );  sub open_type {     my $fh = shift;     my $mode = fcntl($fh, F_GETFL, 0);     print 'File is: ' . $modes{$mode & 3} . '\n'; }  print 'out\n'; $file = new IO::File(); $file->open('> /tmp/out'); open_type($file);  print '\n';  print 'in\n'; $file = new IO::File(); $file->open('< /etc/passwd'); open_type($file);  print '\n';  print 'both\n'; $file = new IO::File(); $file->open('+< /tmp/out'); open_type($file); 

    Example output:

    $ perl test.pl  out File is: Write only  in File is: Read only  both File is: Read / Write 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.