[27847] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 9211 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Apr 29 11:05:52 2006

Date: Sat, 29 Apr 2006 08:05:04 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sat, 29 Apr 2006     Volume: 10 Number: 9211

Today's topics:
    Re: Detecting filehandles <someone@example.com>
    Re: Detecting filehandles <matthew.garrish@sympatico.ca>
    Re: Detecting filehandles <bik.mido@tiscalinet.it>
    Re: How to display image from RAM with some text around <bik.mido@tiscalinet.it>
        Module for a curses/Turbovision style interface <amajorel@teezer.fr>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sat, 29 Apr 2006 12:05:16 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: Detecting filehandles
Message-Id: <0EI4g.704$zn1.341@edtnps90>

Subotai wrote:
> Is there a way to detect if an argument you are passed in a subroutine is a
> filehandle? For instance:
> 
> open(FILE, '/file');
> foo(\*FILE);
> foo($someotherarg);
> 
> sub foo {
> $arg = shift;
>   #### do some check on $arg to see if it is a filehandle or a normal scalar
> }

Use fileno() for that, if it is defined then it is a valid filehandle.

> Also if you have an open filehandle, is there a way to see what file it is
> referencing? For instance if you open the file "/foo", is there anyway to
> get "/foo" from the filehandle? I know with the fileno() call you can get
> the file descriptor,  is there anyway you could use that to lookup that
> information? I don't think there is but I want to be 100% sure.

There is if you have a /proc file system that supports it:

$ perl -le'
open my $fh, q[test.txt] or die $!;
my $fn = fileno $fh;
print readlink "/proc/self/fd/$fn";
'
/home/john/test/test.txt

Or use the PID:

$ perl -le'
open my $fh, q[test.txt] or die $!;
my $fn = fileno $fh;
print readlink "/proc/$$/fd/$fn";
'
/home/john/test/test.txt



John
-- 
use Perl;
program
fulfillment


------------------------------

Date: Sat, 29 Apr 2006 08:04:59 -0400
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: Detecting filehandles
Message-Id: <KDI4g.4763$fx.438717@news20.bellglobal.com>


"Kevin Michael Vail" <kevin@vaildc.net> wrote in message 
news:kevin-A5F7F3.21594228042006@news.verizon.net...
> In article <48mdnTde3IAuLM_ZnZ2dnUVZ_s6dnZ2d@giganews.com>,
> "Subotai" <subotai@aol.com> wrote:
>
>> Is there a way to detect if an argument you are passed in a subroutine is 
>> a
>> filehandle? For instance:
>>
>> open(FILE, '/file');
>> foo(\*FILE);
>> foo($someotherarg);
>>
>> sub foo {
>> $arg = shift;
>>   #### do some check on $arg to see if it is a filehandle or a normal 
>> scalar
>> }
>
> Look at the 'openhandle' function in Scalar::Util.
>
>> Also if you have an open filehandle, is there a way to see what file it 
>> is
>> referencing? For instance if you open the file "/foo", is there anyway to
>> get "/foo" from the filehandle? I know with the fileno() call you can get
>> the file descriptor,  is there anyway you could use that to lookup that
>> information? I don't think there is but I want to be 100% sure.
>
> I don't believe there is, either.  It would be difficult to impossible
> to implement under Unix, because a file is one thing and its name is
> something else; in fact a file can have multiple names (directory
> entries), so how would you choose which one is "the" file?

No, but no reason you can't just use a hash to store all the information 
about the file and then pass that into the subroutine:

[untested]

my %fileinfo = (filepath => '/somefile.dat');
open ($fileinfo{'filehandle'}, '<', '$fileinfo{'filepath'}) or die $!;
mySub(\%fileinfo);

sub mySub {

   my ($arg1) = @_;

   if (ref($arg1) eq 'HASH' and defined($arg1->{'filehandle'}) {
      # have a filehandle
   }
   else {
      # do not have a filehandle
   }

}


Matt 




------------------------------

Date: Sat, 29 Apr 2006 15:30:51 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Detecting filehandles
Message-Id: <t9q652h4br8d9jqan2s17hrm1qmt7lg0lq@4ax.com>

On Fri, 28 Apr 2006 18:22:07 -0600, "Subotai" <subotai@aol.com> wrote:

>Also if you have an open filehandle, is there a way to see what file it is
>referencing? For instance if you open the file "/foo", is there anyway to

Not reliably and not portably. Under Linux you can inspect /proc. Once
I did it, but that was to analize the file descriptors of *another
process*. If you really want to do it for your script itself, then
you'll need fileno(), but personally I can't see why you should want
to do so. Thus I recommend you change the logic of your code...


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


------------------------------

Date: Sat, 29 Apr 2006 15:04:16 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: How to display image from RAM with some text around
Message-Id: <unt452topfn3nn3jjrhorokr0cuspt3pde@4ax.com>

On 28 Apr 2006 18:25:16 GMT, xhoster@gmail.com wrote:

>You could use the data uri, but it doesn't work with IE.

I have also seen using JavaScript for this, but I don't have the
slightest idea how portable it could be, and one has to have JS
enabled in any case, which... may not be the case!


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


------------------------------

Date: Sat, 29 Apr 2006 14:54:59 +0000 (UTC)
From: Andre Majorel <amajorel@teezer.fr>
Subject: Module for a curses/Turbovision style interface
Message-Id: <slrne56vi4.vc0.amajorel@atc5.vermine.org>

I'm looking into writing an interactive Perl program that runs
in a TTY. The interface will be modelled after the old NetWare
text mode utilities. I imagine that entails curses or slang and
a widget layer on top of that.

What module would you recommend for that ? Curses::UI ?
Curses::Widgets ? Something else ?

-- 
André Majorel <URL:http://www.teaser.fr/~amajorel/>
"The best justice that money can buy."


------------------------------

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 9211
***************************************


home help back first fref pref prev next nref lref last post