Hi monks,
I am showing a sample part of my coding where I am finding a problem.
I used three argument open for opening files. I wrote some contents in a file test.txt as shown below.
After that I copied the same file with another name test1.txt and I did some replacements. But when I printed the output I didn't got any output in my console.
But when I added close function it was working fine.
AFAIK close function is not mandatory in Perl (that too in three arguments it ll close the handle automatically). Also for safer purpose I used three arguments open function as well.
What is going on here or am I missing something? While writing some data in file, won't it write in file? or will it have the output only in buffer in this scenario?
If it is a silly question or mistake, I apologise for that, because I tested the below code many times but not getting output.

use strict; use warnings; use File::Copy; open (my $dbout_h, '>', "D:\\Projects\\test.txt") || die ("Unable to o +pen/File not exists: $dboutfile\n"); #open file to write print $dbout_h "<html><b>output - Table:</b><hr>\n<table border=2>\n"; my @arr =( 1, 2, 3); { local $" = '</td><td>'; for (@arr) { next if (!@arr); print $dbout_h "<tr><td>@arr</td></tr>\n"; #print the resul +ts in database output file } } print $dbout_h "<\/table>\n<\/html>"; close ($dbout_h); copy ( "D:\\Projects\\test.txt", "D:\\Projects\\test1.txt"); open (my $dbout1_h, '<', "D:\\Projects\\test1.txt") || die ("Unable to + open/File not exists: test1.txt\n"); #open file to write my $file = do {local $/, <$dbout1_h>}; $file =~ s!<table!<tab!g; $file =~ s!</table!</tab!g; print $file;

Thanks in advance

Prasad


In reply to Question on Filehandle: close function by prasadbabu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.