Path: utzoo!attcan!uunet!aplcen!uakari.primate.wisc.edu!ames!sun-barr!newstop!sun!zweig!stef From: stef@zweig.sun.com (Stephane Payrard) Newsgroups: comp.lang.perl Subject: Re: How to zero a file Message-ID: Date: 24 Dec 89 07:43:11 GMT References: <891@pan.UUCP> Sender: news@sun.Eng.Sun.COM Organization: Sun Microsystems -- Mountain View Lines: 35 In-reply-to: jw@pan.UUCP's message of 23 Dec 89 06:46:41 GMT In article <891@pan.UUCP> jw@pan.UUCP (Jamie Watson) writes: What is the preferred way to zero a file in perl? In shell scripts I use "> filename", so in perl I can 'exec "> filename"', but it seems to me that there should be a way to do this without using exec. In a similar vein, what is the preferred way to copy a file? I could obviously just crank up 'exec "cp old new"', but again I wonder if there is some "better" way to do this entirely within perl. Copying a line at a time, with "while () print" doesn't seem to me to be an ideal solution. Here is a example program which should answer your questions (tested on a Sun 4/110 running SunOS 4.0.3 and perl 3.0 patch 8). No explicit loop: the danger is to run out of memory if copying a big file. #! /usr/bin/perl open(IN,"<$ARGV[0]") || die "can't open $ARGV[0]"; open(OUT,">$ARGV[1]") || die can't open $ARGV[1]"; # create a zero length file @in=; # read the whole file in the array @in (one entry perl line) print OUT @in; -- -- Stephane Payrard -- stef@sun.com -- (415) 336 3726 Sun Microsystems -- 2550 Garcia Avenue -- M/S 16-40 -- Mountain View CA 94043