in reply to Working with word document

Hi,

But when I try for .doc file, it doesn't work.

How do you mean? Any error message whatsoever or just a plain .doc file?
It works for me. Though, I would have written the open function ( using the 3 - arugment open function and using a lexical variable as filehandles instead of barewords), like so:

open my $fh,'<','file1.doc' or die "can't open file: $!"; ## OR open my $fh2,'>>','file2.doc' or die "can't open file: $!";
Then you might also want to close the file handlers in the reverse order in which they are opened.
like so:
close $fh2 or die "can't close file: $!"; close $fh or die "can't close file: $!";
You may also consider checking these modules Win32::OLE, Win32::Word::Writer from http://www.cpan.org

Replies are listed 'Best First'.
Re^2: Working with word document
by nick321 (Initiate) on Aug 27, 2012 at 22:15 UTC

    hi, There is no error message. The .doc file just goes blank. I tried the both modifications you have mentioned. But neither of them works out for me.

    here is the code i have tried

    open my $fh,'<','file1.doc' or die "can't open file:"; open my $fh2,'>>','file2.doc' or die "can't open file:"; while(<fh>) { print fh2 "$_\n"; } close $fh2 or die "can't close file:"; close $fh or die "can't close file:";
      Hi,

      ofcourse, the .doc file will be blank with this:

      ... while(<fh>) ## note this <fh> Oops { print fh2 "$_\n"; ## note this fh2 Oops } ...
      but not with this:
      ... while(<$fh>) ## this is correct $fh { print $fh2 "$_\n"; ## correct $fh2 } ...
      The filehandles are $fh and $fh2 NOT fh and fh2!!!
      Moreover, if you use warnings and strict you will pick this easily.
      Hope this helps

        Here is the corrected code

        #!/usr/bin/perl use 5.010; use strict; use warnings; open my $fh,'<','file1.doc' or die "can't open file:"; open my $fh2,'>>','file2.doc' or die "can't open file:"; while(<$fh>) { print $fh2 "$_"; } close $fh2 or die "can't close file:"; close $fh or die "can't close file:";

        but still the .doc is blank. P.S: I can see the .doc file size is expanding each time I run the program. For Example, when I run for the first time the .doc file size is 13KB. For the second time it is 26KB and it keeps increasing as I run the program.