Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

open $self->{FILE}

by b888 (Beadle)
on Nov 05, 2004 at 15:32 UTC ( [id://405522]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings.

I'm writing object that have to write into file. How can i store filehandle in $self?

I tried to use

open($self->{FILE}, ">>$self->{file_path}$self->{file_name}"); print $self->{FILE} "some text here\n"; close($self->{FILE});

But got error "String found where operator expected"

Replies are listed 'Best First'.
Re: open $self->{FILE}
by trammell (Priest) on Nov 05, 2004 at 15:44 UTC
    From perldoc -f print:
    Note that if you're storing FILEHANDLES in an array or other expression, you will have to use a block returning its value instead: print { $files[$i] } "stuff\n"; print { $OK ? STDOUT : STDERR } "stuff\n";

      Thanks to all for help

      print { $files[$i] } "stuff\n";
      Definitly helps
Re: open $self->{FILE}
by Prior Nacre V (Hermit) on Nov 05, 2004 at 15:49 UTC

    Change

    print $self->{FILE} "some text here\n";

    to

    print { $self->{FILE} } "some text here\n";

    Regards,

    PN5

Re: open $self->{FILE}
by JediWizard (Deacon) on Nov 05, 2004 at 15:40 UTC

    First, I would advise you check the result of the open i.e.:

    open($self->{FILE}, ">>$self->{file_path}$self->{file_name}") || die " +Could not open file $!\n";

    That, however, does not solve your problem. I think that perl is getting confused by the ->. This works:

    my $self = {}; open($self->{FILE}, ">> text.txt") || die "Could not open file : $!\n" +; my $fh = $self->{FILE}; print $fh "some text here\n"; close($self->{FILE});
    May the Force be with you
      perl is not confused by the ->.
Re: open $self->{FILE}
by radiantmatrix (Parson) on Nov 05, 2004 at 15:42 UTC

    I can't imagine that's the best way to approach your problem. The package that $self is an instance of should implement its own accessor and keep a private filehandle, so that your code might look like:

    $self->open(">>$self->{file_path}$self->{file_name}"); $self->print "some text here\n"; $self->close;

    Perhaps if you gave us a little more background about what you're trying to accomplish, we might be able to help you find a different approach.

    radiantmatrix
    require General::Disclaimer;
    "Users are evil. All users are evil. Do not trust them. Perl specifically offers the -T switch because it knows users are evil." - japhy

      I think his question is how to implement something like the methods you've shown. He's asking how to do it in from inside the object, thus he's trying to access the object's data members. At least, that's the impression I got...

        If you're correct...

        package Something; .... #package stuff sub print { my $self = shift; my $FH = $self->{FILE}; #deref the FILE handle return print $FH join('',@_); } #and likewise with others.

        The above would be easier to read and maintain than the whole print { $self->{FILE} } "text" notation, IMO.

        radiantmatrix
        require General::Disclaimer;
        "Users are evil. All users are evil. Do not trust them. Perl specifically offers the -T switch because it knows users are evil." - japhy

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-26 08:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found