solomon243 has asked for the wisdom of the Perl Monks concerning the following question:

How to return a file descriptor from function? I am newbie. :-)

Replies are listed 'Best First'.
Re: return file descriptor from function
by johngg (Canon) on Aug 10, 2013 at 19:07 UTC

    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.

    Cheers,

    JohnGG

      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.

      Ok. This is what I need. Thanks for answers.
Re: return file descriptor from function
by 2teez (Vicar) on Aug 10, 2013 at 20:14 UTC
      Now I will conform to the rules
Re: return file descriptor from function
by davido (Cardinal) on Aug 10, 2013 at 18:58 UTC

    Could you give a little more of the "bigger picture"? ...some context will lead to more useful answers. We need to know more about what you're trying to do.


    Dave