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

i wanted to open a file and then add text to it using print statement. will i be able to do it using perl ? Follwing is the script i am using for the same. open HELLO, ">file.txt"; select HELLO; print "hi !"; I am getting a compilation error. Can anyone tell me where i am going wrong ?
  • Comment on Perl : Unable to use filehandle to open a file

Replies are listed 'Best First'.
Re: Perl : Unable to use filehandle to open a file
by mwah (Hermit) on Nov 17, 2007 at 19:15 UTC

    As holli said, there is nothing wrong within the lines you posted. Ist this a command line app or do you try to do this from within CGI or mod_perl program?

    Try a small modification on your code:

    open HELLO, ">file.txt" or die "$!"; select HELLO or die "$!"; print "hi !";

    and respond to this thread with the error message shown.

    Regards

    mwa

Re: Perl : Unable to use filehandle to open a file
by graff (Chancellor) on Nov 17, 2007 at 19:23 UTC
    If you want to add text to an existing file (that is, append new data at the end, rather than replacing all existing content), then the open statement should be like this -- and you should add error checking as well:
    open HELLO, ">>file.text" or die "open failed: $!";
    (note the two angle brackets instead of one)

    As for the compilation error, you'd have to show us the actual text of the error message. If the message mentions a particular line number, you'd have to show us your code in such a way that we can tell what line that is. (Use "<code>" and "</code>" around the text of the script.)

      hi,
      I am assuming that you want to open a file and print "HI" in that if that is the case
      do this way
      #!/usr/bin/perl
      open HELLO, ">file.txt";
      select HELLO;
      print HELLO "hi !";
Re: Perl : Unable to use filehandle to open a file
by holli (Abbot) on Nov 17, 2007 at 18:06 UTC
    The code you show is correct (though, the select-call is unneccessary overkill). What error do you get?


    holli, /regexed monk/
Re: Perl : Unable to use filehandle to open a file
by shmem (Chancellor) on Nov 18, 2007 at 01:00 UTC
    I am getting a compilation error. Can anyone tell me where i am going wrong ?

    Which error? See How (Not) To Ask A Question. Selecting the error and pasting it into the composition box is easy.

    Are you sure it is perl which is throwing the error? Maybe you are invoking that script as is, and so bash (or whatever shell you have) gets it? Maybe it is no error from perl, but from your shell?

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Perl : Unable to use filehandle to open a file
by bart (Canon) on Nov 18, 2007 at 09:47 UTC
    I bet you were confusing a runtime warning (likely because opening the file failed, and then next, you can't print to a filehandle that isn't open) with a compilation error.
Re: Perl : Unable to use filehandle to open a file
by nehaz (Initiate) on Nov 18, 2007 at 06:37 UTC
    Hi all, thanks for a quick response. The script is working. I am not getting that error again. dont know the reason for the same.