in reply to How to open() something in a hash?

How are you using the file handle (specific code sample required here!)? The following works for me:

use strict; use warnings; my $str; my $self = bless {}; open $self->{fh}, '>', \$str; my $fh = $self->{fh}; # Perl gets confused unless globs are simple sca +lars print $fh "Hello world\n"; close $self->{fh}; print $str;

Prints:

Hello world

Perl's payment curve coincides with its learning curve.

Replies are listed 'Best First'.
Re^2: How to open() something in a hash?
by ikegami (Patriarch) on Jan 24, 2009 at 01:08 UTC
    Alternatives to
    my $fh = $self->{fh}; print $fh "Hello world\n";
    are
    print { $self->{fh} } "Hello world\n";
    and
    use IO::Handle qw( ); $self->{fh}->print("Hello world\n");
Re^2: How to open() something in a hash?
by jh- (Scribe) on Jan 24, 2009 at 01:21 UTC

    Strange how you find the answer yourself just when you've asked for help... =)

    You're right, there was nothing wrong with opening the file. I just had forgotten to do $self->{LOGHANDLE}->autoflush(1) which was kinda essential since my tests consisted of starting up the bot, spamming a few lines in irc to see if it logged anything and then killing it with ^C since I hadn't bothered to implement any cleaner way to shut down the bot yet. =)

    By the way, you can use  print { $self->{fh} } "Hello world\n"; to print to a file handle that isn't just a simple scalar. ;)

    Thanks for the help anyways!