4 Replies - 3569 Views - Last Post: 21 December 2007 - 07:38 AM

#1 sandman85048   User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 20-October 07

Perl does not write to file

Posted 18 December 2007 - 06:51 AM

I have written a script that basically makes plaintext look better (in Perl). It takes a parameter for the filename of the file that will be "prettied". It backs that file up first, "prettifies" the file, and writes the new text to the file.

The problem is that no matter what I input, it outputs a blank file. I'm not sure what I'm doing wrong here.

The file is basically a big hunk of regular expressions. You will need Unicode support on your computer for this code to display correctly:
#!/usr/bin/perl

use warnings;
use utf8;

use File::Copy;

binmode STDOUT, ":utf8";

copy($ARGV[0], "$ARGV[0]~");					
print "Backed up input file as $ARGV[0]~\n";

open (THEFILE, "+>$ARGV[0]");

for $line (<THEFILE>) {
	$line =~ s/"(.+?)"/$1/g;				# Smart quotes
	$line =~ s/'(.+?)'/$1/g;				# Better-looking single quotes
	$line =~ s/--//g;					# Change double-en-dashes to em-dashes
	$line =~ s/\.\.\.//g;					# Change triple-dot to ellipsis
	$line =~ s/\(c\)//gi;					# Change (c) to 
	$line =~ s/\(r\)//gi;					# Change (r) to 
	$line =~ s/\(tm\)//gi;					# Change (tm) to 
	$line =~ s/''//g;					# Change '' to 
	$line =~ s/``//g;					# Change `` to 
	$line =~ s/(?<!')'(?!')//g;				# Cooler-looking apotrophes (but make sure it's alone)
	$line =~ s/([0-9])'/$1′/g;				# Use the actual foot/meter-sign (but make sure there's a digit before it)
	$line =~ s/([0-9])"/$1″/g;				# Use the actual inch-sign (but make sure there's a digit before it)
	$line =~ s/([0-9])x([0-9])/$1$2/g;			# Use the better-looking 'by' sign instead of 'x'
	print THEFILE $line;
}
close THEFILE;
Does anyone know how this can be fixed?

Is This A Good Question/Topic? 0
  • +

Replies To: Perl does not write to file

#2 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Perl does not write to file

Posted 18 December 2007 - 10:43 AM

change this:

for $line (<THEFILE>) {

to:

while (my $line = <THEFILE>) {

see if that helps

Using perls inplace editor would be faster and simpler, but I have to run right now, I will check back later today and post the code if you are interested

This post has been edited by KevinADC: 18 December 2007 - 10:46 AM

Was This Post Helpful? 0
  • +
  • -

#3 sandman85048   User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 20-October 07

Re: Perl does not write to file

Posted 18 December 2007 - 03:49 PM

It still just writes an empty string.. I tried using

while (my $line = <THEFILE>) {

and

while (my($line) = <THEFILE>) {

But neither worked. Here is the code I am currently using (I added some more replacements):
#!/usr/bin/perl

use warnings;
use utf8;

use File::Copy;

binmode STDOUT, ":utf8";

copy($ARGV[0], "$ARGV[0]~");					
print "Backed up input file as $ARGV[0]~\n";

open (THEFILE, "+>$ARGV[0]");
 
while (my $line = <THEFILE>) {
	$line =~ s/"(.+?)"/$1/g;				# Smart quotes
	$line =~ s/'(.+?)'/$1/g;				# Better-looking single quotes
	$line =~ s/--//g;					# Change double-en-dashes to em-dashes
	$line =~ s/\.\.\.//g;					# Change triple-dot to ellipsis
	$line =~ s/\(c\)//gi;					# Change (c) to 
	$line =~ s/\(r\)//gi;					# Change (r) to 
	$line =~ s/\(tm\)//gi;					# Change (tm) to 
	$line =~ s/''//g;					# Change '' to 
	$line =~ s/``//g;					# Change `` to 
	$line =~ s/(?<!')'(?!')//g;				# Cooler-looking apotrophes (but make sure it's alone)
	$line =~ s/([0-9])'/$1′/g;				# Use the actual foot/meter-sign (but make sure there's a digit before it)
	$line =~ s/([0-9])"/$1″/g;				# Use the actual inch-sign (but make sure there's a digit before it)
	$line =~ s/([0-9])x([0-9])/$1$2/g;			# Use the better-looking 'by' sign instead of 'x'
	$line =~ s/([0-9])\/([0-9])/$1$2/g;			# Actual division sign
	$line =~ s/<<//g;					# Better left quote
	$line =~ s/>>//g;					# Better right quote
	print THEFILE $line;
}
close THEFILE;

Was This Post Helpful? 0
  • +
  • -

#4 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Perl does not write to file

Posted 18 December 2007 - 05:02 PM

Then maybe your system does not support open for reading and writing mode "+>". Try using perl inplace editor ($^I) I can't test this though because of the utf8 stuff:

#!/usr/bin/perl

use warnings;
use utf8;
binmode STDOUT, ":utf8";

{
  local $^I = '.bak';#makes a backup copy and goes into inplace edit mode
  while(<>) {
	s/"(.+?)"/$1/g;				# Smart quotes
	s/'(.+?)'/$1/g;				# Better-looking single quotes
	s/--//g;					# Change double-en-dashes to em-dashes
	s/\.\.\.//g;					# Change triple-dot to ellipsis
	s/\(c\)//gi;					# Change (c) to 
	s/\(r\)//gi;					# Change (r) to 
	s/\(tm\)//gi;					# Change (tm) to 
	s/''//g;					# Change '' to 
	s/``//g;					# Change `` to 
	s/(?<!')'(?!')//g;				# Cooler-looking apotrophes (but make sure it's alone)
	s/([0-9])'/$1?/g;				# Use the actual foot/meter-sign (but make sure there's a digit before it)
	s/([0-9])"/$1?/g;				# Use the actual inch-sign (but make sure there's a digit before it)
	s/([0-9])x([0-9])/$1$2/g;			# Use the better-looking 'by' sign instead of 'x'
	s/([0-9])\/([0-9])/$1$2/g;			# Actual division sign
	s/<<//g;					# Better left quote
	s/>>//g;					# Better right quote
	print; 
  }
}  


Your system maybe does not support opening files in read/write mode: +>

This is not secure though becuase you allow $ARGV to be used to open and edit a file without any vaildation, but if you are the only one that runs the script I guess you can trust yourself.
Was This Post Helpful? 0
  • +
  • -

#5 GWatt   User is offline

  • member icon

Reputation: 309
  • View blog
  • Posts: 3,106
  • Joined: 01-December 05

Re: Perl does not write to file

Posted 21 December 2007 - 07:38 AM

If you open a file with writing mode specified first, it will clear the file. Change your open to:
open THEFILE, "+<$ARGV[0]";

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1