Re: Modifing Text Files

2007-10-29 Thread Chas. Owens
On 10/28/07, Telemachus Odysseos [EMAIL PROTECTED] wrote:
 On 10/25/07, Chas. Owens [EMAIL PROTECTED] wrote:
  From the sound of it what you want is in-place-editing:
 
  #!/usr/bin/perl -i
 
  use strict;
  use warnings;
 
  while () {
   s/this/that/
  }
 
  The code above will read in any number of files modifying this to
  that in each one.

 Doesn't that code empty out a file?  That is, isn't there a key line
 missing:

 #!/usr/bin/perl -i
 use strict;
 use warnings;

 while () {
  s/this/that/;
  print;
 }

 I apologize if print; was so obvious that it didn't even need to be
 mentioned, but to be honest, I tried it without that line and emptied a few
 test files before I got it right.


Doh!, that was supposed to be -pi on the command line version and a
print in the full code.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Modifing Text Files

2007-10-28 Thread Telemachus Odysseos
On 10/25/07, Chas. Owens [EMAIL PROTECTED] wrote:

 From the sound of it what you want is in-place-editing:

 #!/usr/bin/perl -i

 use strict;
 use warnings;

 while () {
  s/this/that/
 }

 The code above will read in any number of files modifying this to
 that in each one.


Doesn't that code empty out a file?  That is, isn't there a key line
missing:

#!/usr/bin/perl -i
use strict;
use warnings;

while () {
 s/this/that/;
 print;
}

I apologize if print; was so obvious that it didn't even need to be
mentioned, but to be honest, I tried it without that line and emptied a few
test files before I got it right.


Re: Modifing Text Files

2007-10-27 Thread John W . Krahn
On Thursday 25 October 2007 09:30, Joseph L. Casale wrote:
 I need to make some edits on small (~30 lines) text files. From
 reading perldoc and recent posts, am I correct in understanding that
 the proper way is to read the file in through one handle and write it
 out through another? If that is the case, I suppose I need to write
 in some code then to write the file out to a temp file, then either
 move it over top or delete the old file and rename the new file?

 What is the accepted method in this scenario?

Since the file is relatively small you could slurp the entire file into 
memory, modify it, and then write it back out to the same file.  For 
example (UNTESTED):


use warnings;
use strict;
use Fcntl qw/ :flock :seek /;

my $file = 'something';

open my $fh, '+', $file   or die Cannot open '$file' $!;
flock $fh, LOCK_EX or die Cannot flock '$file' $!;
read $fh, my $data, -s $fh or die Cannot read '$file' $!;

# modify $data

seek $fh, 0, SEEK_SET or die Cannot seek on '$file' $!;
truncate $fh, 0   or die Cannot truncate '$file' $!;
print $fh $data   or die Cannot print to '$file' $!;
close $fh or die Cannot close '$file' $!;



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Modifing Text Files

2007-10-25 Thread Joseph L. Casale
I need to make some edits on small (~30 lines) text files. From reading perldoc 
and recent posts, am I correct in understanding that the proper way is to read 
the file in through one handle and write it out through another? If that is the 
case, I suppose I need to write in some code then to write the file out to a 
temp file, then either move it over top or delete the old file and rename the 
new file?

What is the accepted method in this scenario?

Thanks!
jlc


Re: Modifing Text Files

2007-10-25 Thread Chas. Owens
On 10/25/07, Joseph L. Casale [EMAIL PROTECTED] wrote:
 I need to make some edits on small (~30 lines) text files. From reading 
 perldoc and
 recent posts, am I correct in understanding that the proper way is to read 
 the file in
 through one handle and write it out through another? If that is the case, I 
 suppose
 I need to write in some code then to write the file out to a temp file, then 
 either move
 it over top or delete the old file and rename the new file?

 What is the accepted method in this scenario?
snip

From the sound of it what you want is in-place-editing:

#!/usr/bin/perl -i

use strict;
use warnings;

while () {
 s/this/that/
}

The code above will read in any number of files modifying this to
that in each one.  It isn't really editing the files in place (it
renames the input file, opens a file with to old name, and selects it
for output), but it might as well from your perspective.  If you want
a backup copy of the old file just add an extension after the -i like
this

#!/usr/bin/perl -i.bak

You can also turn on in-place-editing by fiddling with the $^I variable.

see perldoc perlrun and perldoc perlvar for more information.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/