[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl-AnyPerl] works as script, not as droplet



Ok folks,

The script below is driving me nuts. I created a droplet to add
the DOCTYPE line to an HTML file awhile back. Occasionally, I have to
adjust it when I find it does something unexpected. The latest problem was
it quitting on receiving a long input file name, as I append an extension
for output. My solution was to add a 'write name to log and jump to next
file' statement. (BTW, all work occurs on a Mac.)

I tested the script assigning a list of file names to @ARGV and then
commenting the line when I created a droplet, which gets its files from
@ARGV. What is happening is that the script is merely writing the file
names and lengths to the log file and exiting. I can easily replicate
the behavior, so I'm hoping others can also.

The input files are UNIX. I use BBEdit to edit files. Saving as Mac files
changes nothing. (Actually, that problem was fixed in an earlier edit.
Thanks to Chris.)

If anyone can enlighten me, I'd appreciate it. It's built from clippings
I've collected from discussion lists, etc. (I include credits in a
section removed here for brevity...)

Also, the script is probably going to hard-wrap in the mailer.

Thanks in advance.

Paul
----

#!/usr/bin/perl - w

use strict;

# test files assigned to @ARGV in lieu of command-line param:
# comment-out before creating droplet
@ARGV = qw(manual_Unireg.html manual_Users.html);

# declare file-level variables
my ($doctype_line,$inputfile,$tempfile,@lines,$line);
my $session_log = "session.log";
my $session_open = 0;

$doctype_line = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 
Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';

FILE: foreach (@ARGV) {

	# get command-line arg or dropped file
	$inputfile = $_;
	
	# if it's not an HTML file (shtml,shtm,html,htm), skip it.
	next if (lc($inputfile) !~ /\.s?html?$/);

	# work with an individual source file
	
	# create similar-named temp file
	#
	# Macintosh has a limit of 31 chars in file name.
	$tempfile = ""; # initialize
	if ( (length $inputfile) < 26 ) {
	  $tempfile = "$inputfile.tmp"; # set temp file, if meets test
	} else {
	  open (LOG, ">>$session_log");
	  unless($session_open) {print LOG "\n\n" . scalar localtime() . "
-- Unable to create temp file.\n"}
	  # remove path if MacOS
	  if ($^O =~ /macos/i) {$inputfile =~ s/^.+:(.+)$/$1/;}
	  print LOG "\nFile: $inputfile Length: " . length $inputfile .
"chars.\n";
	  close LOG;
	  # if Mac, make sure output file is a BBEdit file,
	  # not default MPW
	  if ($^O =~ /macos/i) {
	    MacPerl::SetFileInfo("R*ch", "TEXT", $session_log);
	  }
	  $session_open = 1; # set the session flag
	  next FILE;
	}
	
	# open input file for reading
	open (INPUT, "<$inputfile") or die "Can't open $inputfile for
reading - $!";

	# do our newline magic here [after opening INPUT file, find 
	# LF/CR type and assign to $/ PJC]
	local $/;
	binmode(INPUT);
	my $pos = tell INPUT;
	until ($/) {
        read INPUT, my($bytes), 1024 or return;
        ($/) = ($bytes =~ /(\015?\012|\015)$/);
    }
    seek INPUT, $pos, 0 or warn "No newline found for `$inputfile'?\n" && 
next FILE;

	# now we get real lines, if $/ is correct
	# put lines into array elements for ease of processing
	@lines = <INPUT>;
	close (INPUT);
	
	# just check the first three lines if DOCTYPE exists
	my @temp_array = @lines[0..2];
	while ( defined($line = shift (@temp_array)) ) {
	  # next file, we have a doctype line
	  next FILE if ($line =~ /DOCTYPE/i);
	} # end foreach
	
	# open output file for writing
	open (TEMP, ">$tempfile") || die ("Could not open $tempfile for
writing.\n");

	# prepend new line to file and print $/ for proper newline
	print TEMP $doctype_line, $/;

	# just print all the lines, they already have the
	# right newline on them
	print TEMP @lines;
	
	# close output file
	close (TEMP);
	
	# replace original file with temp file contents by renaming
	rename ($inputfile, "$inputfile.orig");
	rename ($tempfile, $inputfile);
	
	# if Mac, make sure output file is a BBEdit file,
	# not default MPW
	if ($^O =~ /macos/i) {
	  MacPerl::SetFileInfo("R*ch", "TEXT", $inputfile);
	}

} # end FILE foreach @ARGV



==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-anyperl-request@macperl.org