[27360] in Perl-Users-Digest
Perl-Users Digest, Issue: 9058 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Mar 16 06:05:49 2006
Date: Thu, 16 Mar 2006 03:05:04 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 16 Mar 2006 Volume: 10 Number: 9058
Today's topics:
Re: Determine read/write status of filehandles connecte (Anno Siegel)
Re: Determine read/write status of filehandles connecte <sisyphus1@nomail.afraid.org>
Re: Determine read/write status of filehandles connecte (Anno Siegel)
Re: Great JAPH (was Re: Creating Graphs Dynamically wit <zen13097@zen.co.uk>
Timeout value in Net::FTP seems to have no effect <news@lawshouse.org>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 16 Mar 2006 08:49:13 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Determine read/write status of filehandles connected to memory objects.
Message-Id: <dvb8q9$vs$1@mamenchi.zrz.TU-Berlin.DE>
Sisyphus <sisyphus1@nomail.afraid.org> 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.
------------------------------
Date: Thu, 16 Mar 2006 21:15:27 +1100
From: "Sisyphus" <sisyphus1@nomail.afraid.org>
Subject: Re: Determine read/write status of filehandles connected to memory objects.
Message-Id: <44193b78$0$10677$afc38c87@news.optusnet.com.au>
"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
------------------------------
Date: 16 Mar 2006 11:03:11 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Determine read/write status of filehandles connected to memory objects.
Message-Id: <dvbglf$5dr$1@mamenchi.zrz.TU-Berlin.DE>
Sisyphus <sisyphus1@nomail.afraid.org> 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.
------------------------------
Date: 16 Mar 2006 09:49:50 GMT
From: Dave Weaver <zen13097@zen.co.uk>
Subject: Re: Great JAPH (was Re: Creating Graphs Dynamically with Perl)
Message-Id: <441934be$0$5012$db0fefd9@news.zen.co.uk>
A. Sinan Unur <1usa@llenroc.ude.invalid> wrote:
> Abigail <abigail@abigail.nl> wrote in
> news:slrne1eb9n.1v0.abigail@alexandra.abigail.nl:
>
> > #!/opt/perl/bin/perl -- # No trailing newline after the last line!
> > BEGIN{$|=$SIG{__WARN__}=sub{$_=$_[0];y-_- -;print/(.)"$/;seek _,-open(_
> > ,"+<$0"),2;truncate _,tell _;close _;exec$0}}//rekcaH_lreP_rehtona_tsuJ
>
> This is an amazing JAPH. Very instructive.
>
Seconded!
For those having difficulty getting it to work (like me!), you need to
save this to a file and make that file executable. i.e.:
% perl japh.pl # won't work
Jsyntax error at japh.pl line 3, next token ???
Execution of japh.pl aborted due to compilation errors.
% chmod +x japh.pl ; ./japh.pl # will work.
Just another Perl Hacker
------------------------------
Date: Thu, 16 Mar 2006 09:19:20 +0000
From: Henry Law <news@lawshouse.org>
Subject: Timeout value in Net::FTP seems to have no effect
Message-Id: <1142500761.38875.0@doris.uk.clara.net>
I expect I'm doing something wrong, but Googling for [perl "Net::FTP"
timeout problem] shows nothing; nor does perldoc -q ftp. Can someone
point me in the right direction?
(ActiveState Perl, "v5.8.7 built for MSWin32-x86-multi-thread").
I use Net::FTP to pass files around my internal network. Occasionally I
specify a host that isn't up, at which point Net::FTP hangs for a long
time before giving up. "Set a timeout," I thought, but using the
Timeout=>nnn option on the login operation seems to have no effect -
though the documented default of 120 seconds isn't in operation either.
Here's a sample program which should work with any server that
supports anonymous FTP.
------------------------- sample -----------------------------
#! /usr/bin/perl
use strict; use warnings;
use Net::FTP;
my ($host,$timeout) = @ARGV;
exit 0 unless defined $host;
$timeout = 120 unless defined $timeout;
my $start_time = time;
print "Logging in to $host with timeout $timeout seconds\n";
my $ftp = Net::FTP->new( $host, Timeout=>$timeout );
unless (defined $ftp) {
print "Timed out in ",(time - $start_time)," seconds\n";
print "Session to '$host' failed\n";
exit 0;
}
$ftp->login( 'anonymous', 'news@lawshouse.org' );
my $cwd = $ftp->pwd();
print "Current directory at $host is '$cwd'\n";
$ftp->quit();
print "Session finished\n";
-------------------------- sample output follows -----------------
F:\User\PC\NFBack>tryit.pl neptune 5
Logging in to neptune with timeout 5 seconds
Current directory at neptune is '/'
Session finished
F:\User\PC\NFBack>tryit.pl uranus 5
Logging in to uranus with timeout 5 seconds
Timed out in 21 seconds
Session to 'uranus' failed
F:\User\PC\NFBack>tryit.pl uranus 1000
Logging in to uranus with timeout 1000 seconds
Timed out in 21 seconds
Session to 'uranus' failed
----------------------------- end ---------------------------------
--
Henry Law <>< Manchester, England
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 9058
***************************************