[HRPM] works but ugly

Jim Johnke jjohnke at mapmobile.com
Thu May 11 12:57:55 CDT 2000


This is an interesting program.  I've only been programming in Perl for
a few months now, and then only for my own personal use to make my life
easier.  I took your program and made a few modifications with things I
learned over the past few months.  I'm also interested in seeing some of
the other suggestions.

#!/usr/bin/perl -w

&Usage if (@ARGV < 1);

foreach (@ARGV) {
  print "Processing file: $_\n";

  open(INFILE, "<$_") or die "Could not open $_: $!";

  $outfile = $_ . '.out';
  open(OUTFILE, ">$outfile") or die "Could not open $outfile: $!";

  while ($line = <INFILE>) {
    $line =~ s/^M$//;   # ^M created by ctrl + V, still holding ctrl,
then M
    print OUTFILE $line if ($line !~ /^$/);     # print only if not a
blank line
  }

  close(OUTFILE);
  close(INFILE);

  unlink($_);                   # Delete original file
  rename ($outfile, $_);        # rename the output file to the original
name
  unlink($outfile);             # Delete out file
}

exit 0;

sub Usage {
  my ($prog) = $0 =~ m:([^/]+)/*$:;
  print STDERR "Usage: $prog <filename>\n";
  exit 1;
}


"Troy E. Webster" wrote:
> 
> Ok,
> 
> I wrote this little script to take care of large blocks of white space and
> also to strip off windoze generated carriage-returns/newlines from html
> pages.  Trust me, it is usefull to me in certain situations.
> 
> Problem is I don't know how a '^M' (or windoze carriage return) is
> represented in perl, so I just yank off two characters using chop() and
> hope for the best.  Definetly not bullet proof. If anyone knows how I
> could better this please let me know. It's my first perl program from
> scratch.
> 
> thanks
> Troy
> 
> ****************************************************************
> #!/usr/local/bin/perl -w
> ######################################################
> # code.pl
> #
> # This is a simple script which will process html files
> # and remove annoying blank lines in an html page's
> # source code which are generated by software packages
> # such as Cold Fusion. Hand-editing these pages is
> # tedious.
> #
> # ERROR: If a line contains only a single letter and
> #        a \n (or a line with only two characters)
> #        then this script will chop it out. The
> #        assumption is that no html file will exhibit
> #        this characteristic.(bad assumption)
> #
> # NOTE:  This script will work on pages generated by
> #        a UNIX platform-specific application OR a
> #        page which was created in Windows. (there
> #        are different "newlines" associated with each)
> #
> # AUTHOR: Troy Webster May 2000
> ######################################################
> die "Usage: code.pl [filename]\n" if (@ARGV < 1);
> 
> foreach (@ARGV) {
>         print "Processing file: $_\n";
> 
>         my @lines;
>         if(open(FH, "<$_"))
>         {
>                 @lines = <FH>;    #throw it into an array
>                 close(FH);
> 
>                 if(open(FH,">$_"))
>                 {
>                     foreach(@lines)
>                     {
> 
>                         if ($_ eq "\n")
>                         {
>                             print "removing blank line.\n";
>                             chop $_;
>                         }
> 
>                         elsif (length($_) == 2)  # not the answer,
>                                                  # but it works
>                         {
>                             print "Length is 2\n";
> 
>                             chop;
>                             chop;
> 
>                         }
> 
>                         print FH $_;
>                     }
>                     close(FH);
>                 }
>                 else
>                 {
>                     die "File $_ not written.\n";
>                 }
>             }
>         else
>         {
>             die "File $_ not opened.\n";
>         }
> 
>     }
> exit;
> 
> 
> ___________________________________________________________________________
>                 - improvise, adapt, overcome -
> 
>                 www.pcs.cnu.edu/~twebster/
> ---------------------------------------------------------------------------



More information about the Norfolk-pm mailing list