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

Dear Monks, if you could be so kind to share your wisdom:

#!/usr/bin/perl use 5.010; use strict; use warnings; { open(MYFILE, ">>c:/temp/test.txt")or die "Can't open file: $!\n"; ; #open for write, append } printf MYFILE "\n"; printf MYFILE "[ARTEC]\n"; printf MYFILE 'IniFile=f:\vaa\basis\vaa01.ini'; printf MYFILE "\n"; close(MYFILE);
It works so far but i know there is a "Label-Function" but i dont know how to use this with correct syntax.
print MYFILE <<"Label"; f:\vaa\basic Label
Is not working how do i escape the path?
If i wanted to save a "copy" of myfile e.g. to
filename.orig (e.g. test.text.orig) how would you do this?
Update: I wanted to use "say" instead of printf, maybe ;-)
Thanks in Advance
MH

Replies are listed 'Best First'.
Re: Better way to print text appended to a file "syntax problem"?
by keszler (Priest) on Nov 23, 2009 at 15:31 UTC
    The "Label-Function" is known as a here-document. See <<EOF
Re: Better way to print text appended to a file "syntax problem"?
by VinsWorldcom (Prior) on Nov 23, 2009 at 15:23 UTC

    To escape the filename you're trying to write, use double backslash:

    print MYFILE "IniFile=f:\\vaa\\basis\\vaa01.ini";

    To copy the file, use:

    use File::Copy copy('c:/temp/test.txt, 'c:/temp/test.txt.orig');
Re: Better way to print text appended to a file "syntax problem"?
by cdarke (Prior) on Nov 23, 2009 at 16:29 UTC
    From printf documentation:

    Don't fall into the trap of using a printf when a simple print would do. The print is more efficient and less error prone.
    #!/usr/bin/perl use 5.010; use strict; use warnings; # open for write, append open(my $fh,'>>','c:/temp/test.txt')or die "Can't open file: $!\n"; + print $fh "\n[ARTEC]\n". "IniFile=f:\\vaa\\basis\\vaa01.ini\n" close($fh);
Re: Better way to print text appended to a file "syntax problem"?
by kennethk (Abbot) on Nov 23, 2009 at 15:38 UTC
    To expand a little on what VinsWorldcom said, the structural element you have used in your sample is a here-doc. They are discussed in perlop in the Quote and Quote like Operators section. It is just another way of entering a string. If you want to get the contents of the file, you need to interact with the file system, either through open or by shelling out (e.g. using backticks `, see Quote Like Operators). The File::Copy module suggested uses open. Remember that while perl behaves like a shell in some respects, it is not a shell.
Re: Better way to print text appended to a file "syntax problem"?
by JadeNB (Chaplain) on Nov 23, 2009 at 16:38 UTC
    VinsWorldCom has already mentioned that you need to escape backslashes in a double-quoted string, but you may not realise that you are using a double-quoted string in your here-doc your here-doc is being treated as a double-quoted string. Specifically,
    my $qqstring = <<"HERE"; This text is subject to double-quoted interpolation. It can include e +scapes like \t, and $variables. HERE my $string = <<HERE; This text is also subject to double-quoted interpolation. HERE my $qstring = <<'HERE'; This text is treated like a single-quoted string, except that it can c +ontain embedded single quotes. \t is just a backslash followed by a +'t', and $variables is just a dollar sign followed by the string 'var +iables'. HERE
Re: Better way to print text appended to a file "syntax problem"?
by planetscape (Chancellor) on Nov 24, 2009 at 02:54 UTC

    Re: here-docs, you may also wish to review quotes in Perl.

    HTH,

    planetscape