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

Hi Monks:

I wanted to ask what does it shows me : "Filehandle $ARCHIVO opened only for output at Char.pl line 9." in the following code:

#!/usr/bin/perl use warnings; use strict; sub Take_Txt{ my $archivo = shift; open my $ARCHIVO, '>', $archivo or die $!; my @lineas = <$ARCHIVO>; close $ARCHIVO; shift(@lineas); return @lineas; }

Thanks for answering.

Oh Thanks,I didnt notice,thanks for answering.

Oh,sorry to bother again, but I did what u said, and it doesnt show anything although I have gave it parameters('mon.txt') after finishing the subroutine,but it doesnt show me anything(I have checked that it does have something).Any suggestions?

Replies are listed 'Best First'.
Re: Filehandle opened only for output error
by JavaFan (Canon) on Aug 21, 2009 at 16:34 UTC
    Exactly what it says. You open the handle $ARCHIVO for writing, then you attempt to read from it.

    If you want to open it for reading, use '<' instead.

Re: Filehandle opened only for output error
by toolic (Bishop) on Aug 21, 2009 at 17:13 UTC
    Oh,sorry to bother again, but I did what u said, and it doesnt show anything although I have gave it parameters('mon.txt') after finishing the subroutine,but it doesnt show me anything(I have checked that it does have something).Any suggestions?
    I have a few suggestions.

    Be more specific with the problem you are having. Show your updated code. Show (a small) sample of your input file. If you expect the code to produce some output, show us what you get, and show us what you expect.

    Does @lineas contain what you expect it to? Prove it by printing it with Data::Dumper:

    use Data::Dumper; sub Take_Txt{ my $archivo = shift; open my $ARCHIVO, '>', $archivo or die $!; my @lineas = <$ARCHIVO>; print Data::Dumper(\@lineas); close $ARCHIVO; shift(@lineas); return @lineas; }

    This advice, and other tips, is available in the Basic debugging checklist.

    Read this: How do I compose an effective node title?. A better node title might be 'Filehandle open for output error'

Re: Filehandle opened only for output error
by AnomalousMonk (Archbishop) on Aug 21, 2009 at 16:35 UTC
    This is because you open the file only for output (i.e., the  '>' parameter of the  open statement) and then you try to read from it (the  my @lineas = <$ARCHIVO>; statement).
Re: Filehandle opened only for output error
by ack (Deacon) on Aug 21, 2009 at 16:38 UTC

    In your open statement, using the 3-argument version of open, you have specified '>' which means that you want the file opened for "output" only.

    In the next line you then proceed to try to read from that file (i.e., you try to use the file for "input"). But you didn't open the file for input...only for output.

    If you need to read (i.e., input) from the file, then you need to open the file for input. You do that, in the 3-argume open, by specifying '<' as the 2nd argument instead of the '>' that you used.

    ack Albuquerque, NM

      Oh,sorry to bother again, but I did what u said, and it doesnt show anything although I have gave it parameters('mon.txt') after finishing the subroutine,but it doesnt show me anything(I have checked that it does have something).Any suggestions?

        According to what you showed us, you never print out anything. You don't even call the function to read the file!
        well then probably something else is wrong. could you show us a wider context in which this function is used. here is an example that refers to your 'problem' but with a working procedure.
        use strict; use Data::Dumper; open(FILE, "<", "file.in") || die "Error in : $!"; my @array = <FILE>; close FILE; print Dumper(\@array); file.in here i am , where are you, i am here and you also here
        cheers

        PS

        try dumping the @lineas to check if the data is in the array. if it is then surely the problem is somewhere else.

        use Data::Dumper; sub Take_Txt{ my $archivo = shift; open my $ARCHIVO, '>', $archivo or die $!; my @lineas = <$ARCHIVO>; close $ARCHIVO; shift(@lineas); die Dumper(\@lineas); return @lineas; }