help assessing efficiency of perl script, and testing file content size 
Author Message
 help assessing efficiency of perl script, and testing file content size

Special thanks to Hal R. Pomeranz and Michael J. Corrigan for the
pieces that helped me get this together.  This is a filter that is
piped to by mail (MH) and resulting files get put into the appropriate
gopher directory along with a .cap file.  I understand what I've done,
but only barely.  I wonder if someone can tell me if this is an efficient
way to approach this problem.  I'd also like to take this a step further.
An ideal next step would have the $contents being tested, and in the
case that it exceeds a set number of bytes, the contents would be set
off in yet another area for large files.  Can someone give me a clue
about a way to approach that?

John Price-Wilkin


$headers_done = 0;
while(<>) {
        if ( $headers_done == 0) {

                        if (/^Name: (.*)/) {
                                $fname = $1;
                                $fname =~ s/^\s*//;

                                if ( $#header_lines <  0 )  {
                                        $headers_done = 1;
                                }
                         }
                         if (/^Title: (.*)/) {
                                $capname = $1;
                                $capname =~ s/^\s*//;
                                ($subdir = (split(//, $capname))[0]);
                                if (!(-e $subdir)) {
                                    mkdir($subdir, 0755) &&  
mkdir("$subdir/.cap", 0755);
                                }

                                if ( $#header_lines <  0 )  {
                                        $headers_done = 1;
                                }
                                last;
                        }
                }
        }
        else {
                if (/^$/..eof) {
                $contents .= $_;
                $contents =~ s/^Z//g;
                $contents =~ s/\f/\n\n/g;
        }
        open (FILE, ">$subdir/$fname");
        print FILE $contents;
        close FILE;

Quote:
}

        system  "echo 'Name=$capname' >$subdir/.cap/$fname";


Tue, 12 Sep 1995 11:20:31 GMT  
 help assessing efficiency of perl script, and testing file content size

: Special thanks to Hal R. Pomeranz and Michael J. Corrigan for the
: pieces that helped me get this together.  This is a filter that is
: piped to by mail (MH) and resulting files get put into the appropriate
: gopher directory along with a .cap file.  I understand what I've done,
: but only barely.  I wonder if someone can tell me if this is an efficient
: way to approach this problem.  I'd also like to take this a step further.
: An ideal next step would have the $contents being tested, and in the
: case that it exceeds a set number of bytes, the contents would be set
: off in yet another area for large files.  Can someone give me a clue
: about a way to approach that?
:
: John Price-Wilkin

:
:

: $headers_done = 0;
: while(<>) {
:         if ( $headers_done == 0) {

:                         if (/^Name: (.*)/) {
:                                 $fname = $1;
:                                 $fname =~ s/^\s*//;

:                                 if ( $#header_lines <  0 )  {
:                                         $headers_done = 1;
:                                 }
:                          }
:                          if (/^Title: (.*)/) {
:                                 $capname = $1;
:                                 $capname =~ s/^\s*//;
:                                 ($subdir = (split(//, $capname))[0]);

That doesn't do what you want, unless the subdirectory is always one
character long, since splitting on // returns each character separately.
If you wanted that, you'd say substr($capname, 0, 1).

:                                 if (!(-e $subdir)) {
:                                     mkdir($subdir, 0755) &&  
: mkdir("$subdir/.cap", 0755);
:                                 }

:                                 if ( $#header_lines <  0 )  {
:                                         $headers_done = 1;
:                                 }
:                                 last;
:                         }
:                 }
:         }

Rather than putting a conditional inside of a loop, you could just do
two loops, the first of which processes the header, and the second of
which processes the body.  I also don't understand the necessity of

anywhere.  Why not just

while (<>) {
    last if /^$/;       # end of header
    if (/Name: (.*)/) {
        ...
    }
    elsif (/Title: (.*)/) {
        ...
    }

Quote:
}

:         else {
:                 if (/^$/..eof) {
:                 $contents .= $_;
:                 $contents =~ s/^Z//g;
:                 $contents =~ s/\f/\n\n/g;
:         }

You're translating the whole of $contents on every line.  If you use the
two loop method, it's just

open (FILE, ">$subdir/$fname") || die "Can't open $subdir/$fname: $!\n";
while (<>) {
    s/^Z//;
    s/\f/\n\n/g;
    print FILE $_;

Quote:
}

That way you don't even have to accumulate anything, just filter it through.

Note that s/^Z// doesn't translate control Z.  It deletes the first Z on
the line.  If you want to delete control Z you have to say

        s/\cZ//;

:         open (FILE, ">$subdir/$fname");
:         print FILE $contents;
:         close FILE;

Fine, though I'd check the return value of open like below.

:         system  "echo 'Name=$capname' >$subdir/.cap/$fname";

It's faster to say

        open(CAP, ">$subdir/.cap/$fname") || die "Can't open cap file: $!\n";
        print CAP "Name=$capname\n";
        close CAP;

On top of which, you don't have to worry about shell metacharacters in $capname.

Larry



Thu, 14 Sep 1995 11:34:29 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. CGI.pm Bug: file upload file size mismatch when file size is close to buffer size

2. directory content with files size

3. file size testing

4. Is there a maximum recommended file size for Perl Scripts

5. stat/test and _ and efficiency

6. help needed to find the size of a file using perl

7. help finding combined size of files in dir via perl

8. Help requested with Matt's script-modify script to test data

9. efficiency in data-processing perl script ...

10. Total size of all contents of a directory

11. Troubleshooting Perl Script That Creates Multiple Test Data Files

12. Perl Script for Reading STDF files (Teradyne Standard TEst Data Format)

 

 
Powered by phpBB® Forum Software