Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Indirect Filehandles + use strict = error

by bowei_99 (Friar)
on Mar 14, 2006 at 01:59 UTC ( [id://536441]=perlquestion: print w/replies, xml ) Need Help??

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

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 $filename +- $!\n";
Of course, I have
use strict;
enabled, and would like to leave it that way. But because of that, I'm getting the following error when I run the code:

Can't use string ("DATA") as a symbol ref while "strict refs" in use a +t parse.pl line 105.
As you'd expect, line 105 is the open statement in the code above.

I'm using perl 5.8.7, so I know I don't need to have

use Symbol qw( gensym );
(according to PBP, pg. 204, it's only needed for versions less than 5.6). I'd also like to stay with the standard builtin modules, so it rules out using IO::File. Any thoughts?

-- Burvil

Replies are listed 'Best First'.
Re: Indirect Filehandles + use strict = error
by spiritway (Vicar) on Mar 14, 2006 at 02:08 UTC

    I think what you're looking for is something like this:

    use strict; use warnings; # and so on... open my $FH, "<", $filename or croak "and so on...\n";

    There was no need to assign the string DATA to $FH.

Re: Indirect Filehandles + use strict = error
by TheDamian (Vicar) on Mar 14, 2006 at 10:20 UTC
    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 $ +filename $!\n";
    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 code:
    my $FH; open $FH, "<", $filename or croak "Cannot open file $filename $!\n";
    Damian
Re: Indirect Filehandles + use strict = error
by Tanktalus (Canon) on Mar 14, 2006 at 03:13 UTC

    IO::File? Not builtin? Yes it is ... I've been using it as part of core at least since 5.6.0, possibly even 5.005053 (or something like that).

    That said, open my $fh, '<', $filename ... works just fine. Note also that I recommend against upper-case lexicals. It gets confusing.

Re: Indirect Filehandles + use strict = error
by zer (Deacon) on Mar 14, 2006 at 02:29 UTC
    another option would be with using typeglobs to pass filehandles... probably not what you are looking for. its a way to send filehandles to and from subroutines

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://536441]
Approved by spiritway
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-03-29 00:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found