|
Posted by Wg on April 3, 2008, 1:22 pm
Please log in for more thread options >
>
>
> > 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.
>
> =A0 =A0 sub create_tmp_file1 {
>
> > {
>
> > =A0 =A0 =A0 =A0# 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 =A0 #between reading from each source file..
>
> > =A0 =A0my $inputFile =3D $_[0];
> > =A0 =A0my $getLine;
>
> It's generally bad practice to declare variables earlier than you need
> them.
>
> > =A0 =A0open (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.
>
> =A0 =A0 open(my $TMP_FH, ">>", "tmp_file1.txt")
> =A0 =A0 =A0 =A0 or die "...";
>
> > =A0 =A0print $inputFile."\n";
>
> > =A0 =A0&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:
>
> =A0 =A0 addNewLine($TMP_FH);
>
> > =A0 =A0while(<$inputFile>){
>
> > =A0 =A0 =A0 =A0 =A0 =A0$getLine =3D $_;
>
> It's clearer to assign directly into the right variable. As a bonus, you
> don't stomp on the global $_.
>
> =A0 =A0 while (my $getLine =3D <$inputFile>) {
>
> > =A0 =A0 =A0 =A0 =A0 =A0print TMP_FH $getLine;
> > =A0 =A0}
>
> > =A0 =A0$getLine =3D "\x0C";
> > =A0 =A0print TMP_FH $getLine;
>
> ...of course, then $getLine is no longer in scope here. I would just go
> back to printing it directly.
>
> =A0 =A0 print $TMP_FH "\x0C";
>
> > # =A0print TMP_FH "\x0c";
>
> > =A0 =A0close (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(){
>
> > =A0 =A0my $fileName =3D $_[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).
>
> > =A0 =A0my $count =3D 0;
>
> > # =A0open TEMP_FH, ">>".$fileName or die "Failed opening $outputFile
> > file ...";
>
> > =A0 =A0while($count le 30){
>
> It's simpler to write this as a for loop (foreach if you prefer):
>
> =A0 =A0 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 =3D $_[0];
my $getLine;
while(<$inputFile>){
$getLine =3D $_;
print $TMP_FH $getLine;
}
print $TMP_FH "\x0C";
close ($TMP_FH);
}
sub addNewLine{
my $fileHandle =3D $_[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
|