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

Hello my monk friends,

I need to know how to open and save a file.I've looked at getSaveFile and getOpenFile but it just seems to bring up the GUI needed for those operations(and give the name of the file to save or to open). From what I understand I need to create a file and write the data to that file in order to save and to look into the file and read the content in order to open it with our software. Am I way off? Could you give me a guideline of what I would need to use please, it would me much appreciated.

Thanks for your time in advance :o)

Replies are listed 'Best First'.
Re: Open/Save
by Joost (Canon) on Mar 30, 2005 at 16:05 UTC
Re: Open/Save
by jZed (Prior) on Mar 30, 2005 at 16:22 UTC
    > getSaveFile and getOpenFile

    Are these commands in some GUI program? If so, maybe you should mention what program they are in so we'd have some idea of what you are talking about.

Re: Open/Save
by ambs (Pilgrim) on Mar 30, 2005 at 16:04 UTC
    You can open a file for output as:
    open OUT, ">output.txt" or die "Can't open file\n"; print OUT foo close OUT
    and you can read as:
    open IN, "output" or die "Can't open file\n"; while(<IN>) { # do something with $_ } close IN;
    Don't know if this is exactly what you want.

    Alberto Simões

      You forgot semicolons after some statements. Anyway, in modern Perl it's written like that:
      { open my $fh, '>', 'filename.ext'; print $fh 'foo'; close $fh; }; { open my $fh, '<', 'filename.ext'; while (<$fh>) { # $_ }; close $fh; };