Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

filehandler stored in hash-ref

by jeanluca (Deacon)
on Dec 10, 2009 at 15:16 UTC ( #812215=perlquestion: print w/ replies, xml ) Need Help??
jeanluca has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

I want to store a filehandler in a hash-ref and use it like:
my $s = {} ; open( $s->{out}, ">out" ) or die "hmmmm\n" ; print $s->{out} "test" ; close $s->{out} ;
Looks ok (to me), but doesn't work. If I change this code to
my $s ; open( $s, ">out" ) or die "hmmmm\n" ; print $s "test" ; close $s ;
it works fine.
For some reason 'print' doesn't recognize $s->{out} as a filehandler. Any suggestions ?

thnx
LuCa

Comment on filehandler stored in hash-ref
Select or Download Code
Re: filehandle stored in hash-ref
by moritz (Cardinal) on Dec 10, 2009 at 15:19 UTC
    print { $s->{out} } "test";

    That's mentioned at the end of the print documentation.

Re: filehandler stored in hash-ref
by keszler (Priest) on Dec 10, 2009 at 15:20 UTC
    print {$s->{out}} "test";
    Update: I was too slow on posting the print, so here's the read:
    # because @in = <$s->{in}> won't work my @in = readline($s->{in});
Re: filehandler stored in hash-ref
by kyle (Abbot) on Dec 10, 2009 at 15:25 UTC

    You can get the print to work if you wrap the filehandle in a block.

    my $s = {} ; open $s->{out}, '>', 'out' or die "open: $!\n"; print { $s->{out} } "test"; close $s->{out} or die "close: $!\n";

    Note that if you're reading from a filehandle in an array, you have to store it in a scalar for the diamond operator to work.

    my $fh = $s->{in}; my $line = <$fh>;

    If that's too much trouble, you can use readline.

Re: filehandler stored in hash-ref
by ikegami (Pope) on Dec 10, 2009 at 15:29 UTC
    By the way, it's "file handle". A handle is a means of holding on to resource. A "file handler" would be a piece of code that that applies a process to a file in a situation where you also appply the process to something other than files.
Re: filehandler stored in hash-ref
by jeanluca (Deacon) on Dec 10, 2009 at 16:04 UTC
    thnx alot! also for explaining that its a handle not a handler :)

    One last (almost related) thing. I'm using Tie::Hash, so if the programs dies (because it cannot open the file), the DESTROY sub is called:
    sub DESTROY { my $self = shift ; if ( $self->{out} && $self->{out}->opened() ) { ... do stuff .... close $self->{out} ; } }
    $self->{out} is the filehandle mentioned in my first post. Unfortunatly this generates the following
    (in cleanup) Can't locate object method "opened" via package "IO::Hand +le" at modules/MyTH.pm line 29.
    So the question is how do I test if a filehandle is open ?
      Perhaps you forgot to load IO::Handle...
        thats it!

        But, if I see this
        (in cleanup) Can't locate object method "opened" via package "IO::Hand +le"
        I would say IO::Handle is already loaded, because its already using this package !

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2015-07-04 01:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls