<?xml version="1.0" encoding="windows-1252"?>
<node id="536507" title="Re: Indirect Filehandles + use strict = error" created="2006-03-14 05:20:08" updated="2006-03-14 00:20:08">
<type id="11">
note</type>
<author id="107600">
TheDamian</author>
<data>
<field name="doctext">
&lt;blockquote&gt;&lt;i&gt;
Hi all, I'm to use indirect filehandles, per Damian Conway's PBP, like so:
&lt;code&gt;
my $FH = "DATA"; open $FH, "&lt;", $filename or croak "Cannot open file $filename $!\n";
&lt;/code&gt; 
&lt;/i&gt;&lt;/blockquote&gt; 

Well, that's not quite how I said to do it in PBP. You missed one important caveat. From the book:

&lt;blockquote&gt;
Whenever you call &lt;code&gt;open&lt;/code&gt; with an undefined scalar variable as its first argument, &lt;code&gt;open&lt;/code&gt; creates an anonymous filehandle (i.e. one that isn’t stored in any symbol table), opens it, and puts a reference to it in the scalar variable you passed.
&lt;p&gt;
So you can open a file and store the resulting filehandle in a lexical variable, all in one statement, like so:

&lt;code&gt;
open my $FILE, '&lt;', $filename
    or croak "Can't open '$filename': $OS_ERROR";
&lt;/code&gt;
&lt;p&gt;
The &lt;code&gt;my $FILE&lt;/code&gt; embedded in the &lt;code&gt;open&lt;/code&gt; statement first declares a new lexical variable in the current scope. That variable is created in an undefined state, so the &lt;code&gt;open&lt;/code&gt; fills it with a reference to the filehandle it’s just created, as described above.
&lt;/blockquote&gt;

The key point being: 

&lt;blockquote&gt;
Whenever you call &lt;code&gt;open&lt;/code&gt; with an &lt;b&gt;undefined&lt;/b&gt; scalar variable as its first argument...
&lt;/blockquote&gt; 

So you were nearly right. You just needed a little bit &lt;i&gt;less&lt;/i&gt; code:

&lt;code&gt;
    my $FH;
    open $FH, "&lt;", $filename     
        or croak "Cannot open file $filename $!\n";
&lt;/code&gt;

Damian</field>
<field name="root_node">
536441</field>
<field name="parent_node">
536441</field>
</data>
</node>
