"cat"-ing three files into three files. 
Author Message
 "cat"-ing three files into three files.

I'm trying to concatenate three files into three identical output
files.  

This is what I have so far:


        open (INFILE,"+<$fname") || die "Can't open $fname: $!";
        flock(INFILE,$LOCK_EX ) || die "Can't lock $fname: $!";
        $file.=<INFILE>;
        truncate(INFILE,0);
        close (INFILE);
    }
     # A crash here will cause data loss :(


        open (OUTFILE,">$fname") || die "Can't open $fname: $!";
        flock(OUTFILE,$LOCK_EX ) || die "Can't lock $fname: $!";
        print OUTFILE $file;
        close (OUTFILE);
    }

The code above has several inperfections. For one it is prone to data
loss. For another if the data are large, it will take up a large
amount of memory.

Any ideas on how I should improve this code?



Sat, 03 Nov 2001 03:00:00 GMT  
 "cat"-ing three files into three files.

Quote:

>I'm trying to concatenate three files into three identical output
>files.  

>This is what I have so far:


>        open (INFILE,"+<$fname") || die "Can't open $fname: $!";
>        flock(INFILE,$LOCK_EX ) || die "Can't lock $fname: $!";
>        $file.=<INFILE>;
>        truncate(INFILE,0);
>        close (INFILE);
>    }
>     # A crash here will cause data loss :(


>        open (OUTFILE,">$fname") || die "Can't open $fname: $!";
>        flock(OUTFILE,$LOCK_EX ) || die "Can't lock $fname: $!";
>        print OUTFILE $file;
>        close (OUTFILE);
>    }

>The code above has several inperfections. For one it is prone to data
>loss. For another if the data are large, it will take up a large
>amount of memory.

>Any ideas on how I should improve this code?

Not sure if a 4th temporary file would save any memory or just use cache
instead:

    open (TMP,">/tmp/temp.$$") || die "Can't open temp.$$: $!";
    flock(TMP,$LOCK_EX ) || die "Can't lock temp.$$: $!";

        open (INFILE,"<$fname") || die "Can't open $fname: $!";
        flock(INFILE,$LOCK_EX ) || die "Can't lock $fname: $!";
        print TMP <INFILE>;       # copy INFILE to TMP
        close INFILE;
    }
    close TMP;

     # No data loss if you crash :)

    open (TMP,"</tmp/temp.$$") || die "Can't open temp.$$: $!";
    flock(TMP,$LOCK_EX ) || die "Can't lock temp.$$: $!";

        open (OUTFILE,">$fname") || die "Can't open $fname: $!";
        flock(OUTFILE,$LOCK_EX ) || die "Can't lock $fname: $!";
        print OUTFILE <TMP>;
        close OUTFILE;
        seek TMP,0,0;   # return file pointer to beginning.
    }
    close TMP;
    unlink '/tmp/temp.$$';      # delete tmp file

--

http://www.xnet.com/~efflandt/



Sun, 11 Nov 2001 03:00:00 GMT  
 "cat"-ing three files into three files.


Quote:

> I'm trying to concatenate three files into three identical output
> files.

> This is what I have so far:


>         open (INFILE,"+<$fname") || die "Can't open $fname: $!";
>         flock(INFILE,$LOCK_EX ) || die "Can't lock $fname: $!";
>         $file.=<INFILE>;
>         truncate(INFILE,0);
>         close (INFILE);
>     }
>      # A crash here will cause data loss :(


>         open (OUTFILE,">$fname") || die "Can't open $fname: $!";
>         flock(OUTFILE,$LOCK_EX ) || die "Can't lock $fname: $!";
>         print OUTFILE $file;
>         close (OUTFILE);
>     }

> The code above has several inperfections. For one it is prone to data
> loss. For another if the data are large, it will take up a large
> amount of memory.

> Any ideas on how I should improve this code?

I've modified the above code to look like this


        $fh{$fname}=new FileHandle("+<$fname");
        unless ($fh{$fname}) {warn "Can't open $fname: $!";next}
        flock($fh{$fname},$LOCK_EX ) || die "Can't lock $fname: $!";
        $file.=<$fh{$fname}>;

    }


        truncate($fh{$fname},0);

        print $fh{$fname} $file;
        close ($fh{$fname});
    }

this new version will dump the data back into the original files. Which
is a nice feature. However, it still has some problems.  There is still
potential for data loss. And it will still have problems with large
files. The only real improvement is the potential for data loss is less.
I would really appreciate some help on this.

(I'm really starting to miss Gnus)

--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---



Sun, 11 Nov 2001 03:00:00 GMT  
 "cat"-ing three files into three files.


<snip>
Quote:
>         flock(INFILE,$LOCK_EX ) || die "Can't lock $fname: $!";

                    ---^

Also note that your code (and the code of the two other people who
responded to your post) does not actually lock your file.  You are using
the variable $LOCK_EX which, unless you've defined yourself someplace
and didn't post that part of the code, will be undefined.  What you
probably meant to do was say:

use Fcntl ':flock';
...
flock(INFILE, LOCK_EX) || die "Can't lock $fname: $!";

'LOCK_EX' is a function, not a scalar variable.

Garth

--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---



Sun, 11 Nov 2001 03:00:00 GMT  
 "cat"-ing three files into three files.

nilrame] Date: Wed, 26 May 1999 17:43:26 GMT


nilrame] Subject: Re: "cat"-ing three files into three files.

nilrame] Organization: Deja.com - Share what you know. Learn what you don't.
nilrame]


nilrame] >
nilrame] >
nilrame]
nilrame] [....deleted...]
nilrame] I've modified the above code to look like this
nilrame]
nilrame]         close ($fh{$fname});

Can't remember where I read this, but it said something like,
"if you're going to use other than simple scalar as filehandle
variable, you have to put it in curly brace.."

   close { $fh{$fname} } or die "Can't close: $!";

-hasan-

+================================================================+

   TRABAS Technical Dept.          PT Meitraco Bahana Sejahtera  
   IT Solution Provider            http://www.trabas.co.id/        

   Surya Sumantri Blok B1 No. 35   Phone: +62-22-216660
   Setrasari Mall Bandung 40146    Fax  : +62-22-2007633
+================================================================+
     SORRY: I ignore most of attachments but plain text type
+================================================================+

"Mangga, tong era-era... tapi tong rea-rea teuing."
   --Iman, ja'i



Tue, 20 Nov 2001 03:00:00 GMT  
 "cat"-ing three files into three files.

Quote:

> Can't remember where I read this, but it said something like,
> "if you're going to use other than simple scalar as filehandle
> variable, you have to put it in curly brace.."

>    close { $fh{$fname} } or die "Can't close: $!";

Oh, that was very helpful.

#!/usr/local/bin/perl
close { $fh{$fname} } or die "Can't close: $!";
__END__
Not a GLOB reference at - line 1.

You may be referring to the ambiguity between <> as the input operator
and <> as the globbing operator, but that has absolutely nothing to do
with close().

--
 _ / '  _      /       - aka -

    /                                http://www.tiac.net/users/chipmunk/
        "It's funny because it's true ... and vice versa."



Tue, 20 Nov 2001 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. regex for one "two three" four

2. "cat" ing text in Perl

3. missing file while "make"ing modules

4. newbie trying to parse one file into three

5. using a regex for three different file names

6. 'cat'ing a GIF file?

7. 'cat'ing binary files together

8. Unix "cat"

9. Perl script as "cat" - but slower

10. Help: "cat" command and variable assignment

11. how to parse a "this", "that", "and the ", "other" file

 

 
Powered by phpBB® Forum Software