Hi all, I'm to use indirect filehandles, per Damian Conway's PBP, like so:
my $FH = "DATA"; open $FH, "<", $filename or croak "Cannot open file $
Well, that's not quite how I said to do it in PBP. You missed one important caveat. From the book:
Whenever you call open with an undefined scalar variable as its first argument, open 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.
So you can open a file and store the resulting filehandle in a lexical variable, all in one statement, like so:
open my $FILE, '<', $filename
or croak "Can't open '$filename': $OS_ERROR";
The my $FILE embedded in the open statement first declares a new lexical variable in the current scope. That variable is created in an undefined state, so the open fills it with a reference to the filehandle itís just created, as described above.
The key point being:
Whenever you call open with an undefined scalar variable as its first argument...
So you were nearly right. You just needed a little bit less
open $FH, "<", $filename
or croak "Cannot open file $filename $!\n";
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
Outside of code tags, you may need to use entities for some characters:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||