Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Determine read/write status of filehandles connected to memory objects.

Reply
Thread Tools

Determine read/write status of filehandles connected to memory objects.

 
 
Sisyphus
Guest
Posts: n/a
 
      03-16-2006
Hi,

On unix, with filehandles connected to normal files, we can query the
read/write status of the filehandle by examining the return value of the
fcntl() function:

use Fcntl;
# some code that creates the open $filehandle.
my $fmode = fcntl($filehandle, F_GETFL, my $slush = 0);

The value of $fmode will allow us to determine whether the filehandle is
readonly, writeonly, or readable/writable.

But with perl 5.8, it's possible to create filehandles connected to memory
objects:

use warnings;
use strict;

my ($fh1, $fh2, $var1, $var2);
open $fh1, '>', \$var1 or die $!;
print $fh1 "hello"; # $var1 contains "hello"

open $fh2, '<', \$var1 or die $!;
$var2 = <$fh2>; # $var2 contains "hello";

close $fh1 or die $!;
close $fh2 or die $!;

print $var1, " ", $var2, "\n"; # prints "hello hello"
__END__

But now the fcntl() function is unable to provide information that I can use
to determine the read/write status of $fh1and $fh2.
For both $fh1 and $fh2 the fcntl() function will return undef - and fileno()
will return -1.

The question:
How can I determine the read/write status of an open filehandle that's
connected to a memory object ?

Cheers,
Rob




 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      03-16-2006
Sisyphus <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Hi,
>
> On unix, with filehandles connected to normal files, we can query the
> read/write status of the filehandle by examining the return value of the
> fcntl() function:
>
> use Fcntl;
> # some code that creates the open $filehandle.
> my $fmode = fcntl($filehandle, F_GETFL, my $slush = 0);
>
> The value of $fmode will allow us to determine whether the filehandle is
> readonly, writeonly, or readable/writable.
>
> But with perl 5.8, it's possible to create filehandles connected to memory
> objects:
>
> use warnings;
> use strict;
>
> my ($fh1, $fh2, $var1, $var2);
> open $fh1, '>', \$var1 or die $!;
> print $fh1 "hello"; # $var1 contains "hello"
>
> open $fh2, '<', \$var1 or die $!;
> $var2 = <$fh2>; # $var2 contains "hello";
>
> close $fh1 or die $!;
> close $fh2 or die $!;
>
> print $var1, " ", $var2, "\n"; # prints "hello hello"
> __END__
>
> But now the fcntl() function is unable to provide information that I can use
> to determine the read/write status of $fh1and $fh2.
> For both $fh1 and $fh2 the fcntl() function will return undef - and fileno()
> will return -1.
>
> The question:
> How can I determine the read/write status of an open filehandle that's
> connected to a memory object ?


I don't know of a built-in method. You can attempt to write an empty
string to the filehandle and catch the warning if it fails:

sub can_write {
my $fh = shift;
eval {
use warnings FATAL => 'io';
print $fh '';
};
not $@;
}

Alternatively one could try to use 4-arg select().

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
 
 
 
Sisyphus
Guest
Posts: n/a
 
      03-16-2006

"Anno Siegel" wrote
..
..
>
> I don't know of a built-in method. You can attempt to write an empty
> string to the filehandle and catch the warning if it fails:
>
> sub can_write {
> my $fh = shift;
> eval {
> use warnings FATAL => 'io';
> print $fh '';
> };
> not $@;
> }
>


Any script containing that subroutine that was run on pre-5.6 perl would, I
believe, fail to compile - even if can_write() was never going to be called
unless $] >= 5.008. (I know this shouldn't be a consideration, but I was
hoping to do it in a way that wouldn't break pre-5.6 perls, if possible.) Is
there perhaps a workaround to "use warnings FATAL => 'io';" involving
'require warnings;' (and perhaps also 'import') ? I couldn't produce an
incantation that worked.

> Alternatively one could try to use 4-arg select().
>


Yes - otherwise this looks a possibility. Unfortunately, I find the 1-arg
select() confusing enough. I'm not going to specifically request any help
with potential code, as I haven't yet made a serious attempt to come to
terms with the 4-arg version - but if someone likes to provide further
hints/suggestions along those lines, then that's fine by me

Thanks for your insight, Anno.

Now ... back to 'perldoc -f select' ....

Cheers,
Rob


 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      03-16-2006
Sisyphus <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
> "Anno Siegel" wrote
> .
> .
> >
> > I don't know of a built-in method. You can attempt to write an empty
> > string to the filehandle and catch the warning if it fails:
> >
> > sub can_write {
> > my $fh = shift;
> > eval {
> > use warnings FATAL => 'io';
> > print $fh '';
> > };
> > not $@;
> > }
> >

>
> Any script containing that subroutine that was run on pre-5.6 perl would, I
> believe, fail to compile - even if can_write() was never going to be called
> unless $] >= 5.008. (I know this shouldn't be a consideration, but I was
> hoping to do it in a way that wouldn't break pre-5.6 perls, if possible.) Is
> there perhaps a workaround to "use warnings FATAL => 'io';" involving
> 'require warnings;' (and perhaps also 'import') ? I couldn't produce an
> incantation that worked.


Use $SIG{ __WARN__} instead, that's been around forever.

sub can_write {
my $fh = shift;
my $warned;
local $SIG{ __WARN__} = sub { $warned ++ };
print $fh '';
return ! $warned;
}

A more specific test if the warning was indeed the text "Filehandle
%s opened only for input" would be wise in both variants.

> > Alternatively one could try to use 4-arg select().
> >

>
> Yes - otherwise this looks a possibility. Unfortunately, I find the 1-arg
> select() confusing enough. I'm not going to specifically request any help
> with potential code, as I haven't yet made a serious attempt to come to
> terms with the 4-arg version - but if someone likes to provide further
> hints/suggestions along those lines, then that's fine by me


Can't be done with select(). You'd need a file number for the handle
you want to test, but fileno( $f) is -1 (invalid) if $f is a "stringy"
filehandle.

Anno

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
Sisyphus
Guest
Posts: n/a
 
      03-17-2006

"Anno Siegel" <(E-Mail Removed)-berlin.de> wrote in message n

>
> Use $SIG{ __WARN__} instead, that's been around forever.
>
> sub can_write {
> my $fh = shift;
> my $warned;
> local $SIG{ __WARN__} = sub { $warned ++ };
> print $fh '';
> return ! $warned;
> }
>
> A more specific test if the warning was indeed the text "Filehandle
> %s opened only for input" would be wise in both variants.
>
> > > Alternatively one could try to use 4-arg select().

..
..
>
> Can't be done with select(). You'd need a file number for the handle
> you want to test, but fileno( $f) is -1 (invalid) if $f is a "stringy"
> filehandle.
>


Ok ... thanks again, Anno.

Cheers,
Rob


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 5.9 How can I make a filehandle local to a subroutine? How do I pass filehandles between subroutines? How do I make an array of filehandles? PerlFAQ Server Perl Misc 0 01-12-2011 11:00 PM
Perl 5.8.x, Unicode and In-memory Filehandles Bernard Chan Perl Misc 3 03-01-2006 08:00 AM
connected but not connected =?Utf-8?B?R2xlbm4=?= Wireless Networking 1 12-04-2005 12:35 AM
problem: connected, but not connected to a wireless network Andrew Smith Wireless Networking 0 08-20-2004 07:51 PM
Connected Icon Showing, But In Reality Not Connected. Thoughts On ? Robert11 Computer Support 2 03-02-2004 04:02 PM



Advertisments