Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: return file descriptor from function

by johngg (Abbot)
on Aug 10, 2013 at 19:07 UTC ( #1048930=note: print w/ replies, xml ) Need Help??

in reply to return file descriptor from function

Use a lexical filehandle and pass it back, it behaves just as a scalar.

$ cat > rubbish sdfhi rtrthr dgdf $ perl -Mstrict -Mwarnings -e ' > my $file = q{rubbish}; > my $fh = openFunc( $file ); > print while <$fh>; > > sub openFunc > { > my $fileToOpen = shift; > open my $fileHandle, q{<}, $fileToOpen > or die qq{open: $fileToOpen: $!\n}; > return $fileHandle; > }' sdfhi rtrthr dgdf $

I hope this was what you meant from your rather terse question and that I have answered your question.



Comment on Re: return file descriptor from function
Download Code
Replies are listed 'Best First'.
Re^2: return file descriptor from function
by Laurent_R (Monsignor) on Aug 10, 2013 at 22:48 UTC

    Although the question was terse, I think that is what the OP most probably meant. And yes, using a lexical filehandle enables you to use it almost as any scalar variable: to pass it to a function, to return it from a function, to take a reference to it, to store it in an array of filehandles or in a hash, to make it a local persistent object in a closure, etc., even if you don't know or don't want to know anything on typeglobs.

Re^2: return file descriptor from function
by solomon243 (Novice) on Aug 12, 2013 at 08:34 UTC
    Ok. This is what I need. Thanks for answers.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1048930]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2015-10-10 06:30 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (255 votes), past polls