in reply to Re: Working with word document
in thread Working with word document

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:";

Replies are listed 'Best First'.
Re^3: Working with word document
by 2teez (Vicar) on Aug 27, 2012 at 22:41 UTC
    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.

        Hi,

        see this: open my $fh2,'>>','file2.doc' or die "can't open file:";
        With the double arrow ">>" the file file2.doc is created if it doesn't exist, but if it does, it "appends" to the file.
        Thus, the increase in the size of the file. So, to just add to the file you will use do like so:
        open my $fh2,'>','file2.doc' or die "can't open file:";
        Please check open for more detailed information.