Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

5. Input and Output > Filehandles in a Scalar

Filehandles in a Scalar

Since Perl 5.6, you can create a filehandle in a scalar variable so you don’t have to use a bareword. This makes many things, such as passing a filehandle as a subroutine argument, storing them in arrays or hashes, or controlling its scope, much easier. Although, you still need to know how to use the barewords because you’ll still find them in Perl code and they are actually quite handy in short scripts where you don’t benefit that much from the filehandles in a variable.

If you use a scalar variable without a value in place of the bareword in open, your filehandle ends up in the variable. People typically do this with a lexical variable since that ensures you get a variable without a value; some like to put a _fh on the end of these variable names to remind themselves that they are using it for a filehandle:


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint