Re: Problem adding new line !



On Apr 3, 7:36 pm, Ben Morrow <b...@xxxxxxxxxxxx> wrote:
Quoth Wg <techreposit...@xxxxxxxxx>:



I'm trying to add a new line to a file say "tmp_file1.txt" before
reading into that file. Here tmp_file1.txt is the destination file
into which I'm trying to copy.. Pasting code snippet. Is there
anything that I'm missing out here ..

What's going wrong? It's not clear exactly what you're trying to
achieve, and you haven't told us what the code below is doing wrong. It
appears to be appending the contents of $inputFile, followed by 31 lines
with two spaces, followed by a form-feed character (with no following
newline, so the file is no longer a text file) to tmp_file1.txt.

Some general comments on the code follow, but I doubt they'll solve your
problem.

sub create_tmp_file1()

You don't want those parens: they don't do what you thing they do. Perl
isn't shell.

    sub create_tmp_file1 {

{

       # Pls note: the function recieve's the file handle of the
source file and passess on the file handle of the #destination file to
the sub routine that is called for inserting new line.. Attempt is to
give sufficient spacing   #between reading from each source file..

   my $inputFile = $_[0];
   my $getLine;

It's generally bad practice to declare variables earlier than you need
them.

   open (TMP_FH, ">>tmp_file1.txt") or die "Unable to open file
tmp_file1 for writing ..\n";

You will find dealing with filehandles much easier if you use the
lexical filehandles introduced in perl 5.6. For a start, you won't need
to mess around with globrefs.

You should use three-arg open: while it doesn't matter in this case,
it's a good habit to get into.

    open(my $TMP_FH, ">>", "tmp_file1.txt")
        or die "...";

   print $inputFile."\n";

   &addNewLine(\*TMP_FH);

Don't call subs with & unless you know what it does. This may explain
why you got away with the () on your sub declarations...

With lexical filehandles (opened as above) you don't need to use globref
syntax. Just pass the filehandle as it is:

    addNewLine($TMP_FH);

   while(<$inputFile>){

           $getLine = $_;

It's clearer to assign directly into the right variable. As a bonus, you
don't stomp on the global $_.

    while (my $getLine = <$inputFile>) {

           print TMP_FH $getLine;
   }

   $getLine = "\x0C";
   print TMP_FH $getLine;

...of course, then $getLine is no longer in scope here. I would just go
back to printing it directly.

    print $TMP_FH "\x0C";

#  print TMP_FH "\x0c";

   close (TMP_FH);

Another benefit of lexical filehandles is that they close for you when
the variable goes out of scope. When you're writing to a file, though,
you should check for errors on close, as any error while writing
(because of a full disk, for instance) will show up as an error on close.
(Of course, if you want to know exactly *what* failed to write, as
opposed to simply dieing with an error, you'll need to check the return
value of print as well.)

}

# Function that adds new line character..

sub addNewLine(){

   my $fileName = $_[0];

This is not a file name, but a filehandle. I presume you know this, but
leaving bad variable names in your code is a sure way to confuse
yourself when you come back to it later (not to mention people on Usenet
trying to read your code).

   my $count = 0;

#  open TEMP_FH, ">>".$fileName or die "Failed opening $outputFile
file ...";

   while($count le 30){

It's simpler to write this as a for loop (foreach if you prefer):

    for my $count (0..30) {

Ben

Thanks Ben !

I was trying to insert new line to the destination file between
reading each source file. Follow'd Ben's guidance on this.. Pasting
below the code snippet that worked for me..

sub create_tmp_file1{

open (my $TMP_FH,">>","tmp_file1.txt") or die "Unable to open file
tmp_file1 for writing ..\n";

addNewLine($TMP_FH);

my $inputFile = $_[0];
my $getLine;

while(<$inputFile>){

$getLine = $_;
print $TMP_FH $getLine;
}

print $TMP_FH "\x0C";

close ($TMP_FH);

}

sub addNewLine{

my $fileHandle = $_[0];

# open ($fileHandle, ">>", "tmp_file1.txt") or die "Failed opening
file ...";

for my $count(0 .. 10){

print $fileHandle " \n";

}

# close($fileHandle);

}

Thanks Ben for your time and patience..

-Wg
.



Relevant Pages

  • Problem adding new line !
    ... reading into that file. ... Here tmp_file1.txt is the destination file ... the sub routine that is called for inserting new line.. ... print TMP_FH $getLine; ...
    (comp.lang.perl.misc)
  • Re: Problem adding new line !
    ... reading into that file. ... Here tmp_file1.txt is the destination file ... the sub routine that is called for inserting new line.. ... With lexical filehandles you don't need to use globref ...
    (comp.lang.perl.misc)
  • Re: Cant use global FILEHANDLEs?
    ... Subject: Can't use global FILEHANDLEs? ... :> It's related open sub is: ... Now, if you're sure you need to close $hndFIFO before re-opening it, ...
    (perl.beginners)
  • Re: Reading and from more than one filehandle / memory question
    ... put the filehandles inside another filehandles while loop. ... use strict; ... sub read_the_files{ ... "\nOpen successful"} ...
    (perl.beginners)
  • Re: Reading from multiple pipes
    ... But there is no guarantee that the ... that provides line-by-line reading from available filehandles. ... Ed Avis ...
    (perl.beginners)