in reply to losing html in print calling function.
Here are a few more pointers:
- LesleyB++ ; I second that templating advice.
- You are using the search string directly as a regular expression, which is probably not what you mean to do. For example, if I search for "a..le", I get the hits for "apple", "battle", and more. As I demonstrate below, the quotemeta function can be used to escape special characters in the string, forcing it to just be a simple test search.
- Since your HTML form is (appropriately) using GET instead of POST, you can easily split your testing into two pieces from the commandline:
- curl -o test_results.html 'http://www.librum.us/cgi-bin/satoci.cgi?sitem=apple'
- open test_results.html (or just `test_results.html`, or double-click on the file; however you open a document on your system)
- You are using lots of repeated code, such as many calls to open and fn1(). The DRY principle will guide you to separate the parts that differ in each copy from the parts that are identical across all the copies. The Perl docs on Array of Arrays can help you understand how to manipulate the data that is extracted from each of the copies, such that you have a single call to open and and a single call to fn1(), but within a loop that walks over a list of the books. This will also clean up many inconsistencies in your output, such as "Youngs.htm Missing" vs "YCMI file not found" vs "has no ATOCI".
To explore your original problem, I refactored your code into the style that I would write (except that I would go further to use some template module). I include it below, in case anyone finds it useful.
#!/usr/bin/env perl use strict; use warnings; use Carp; my $prestring = '???'; $prestring = '.'; # Overridden for PerlMonks testing my $wanted_string = 'apple'; # really take from param 'sitem'; my $index_dir = $prestring; my @book_indexes_titles = ( [ '1800.txt' , '1800: Mechanical Movements, Powers and Devices, +1911' ], [ '507.txt' , '507: Five Hundred and Seven Mechanical Movements +. 1893' ], [ '970.txt' , '970: Mechanical Appliances, Mechanical Movements + and Novelties of Construction. 1904' ], [ 'ACAM.txt' , 'ACAM: Appletons Cyclopedia of Applied Mechanics, + 1880 ' ], [ '???.txt' , 'Audels Carpenters and Builders Guide I' ], [ '???.txt' , 'Audels Carpenters and Builders Guide II' ], [ '???.txt' , 'Audels Carpenters and Builders Guide III' ], [ '???.txt' , 'Audels Carpenters and Builders Guide IV' ], [ 'AMOM.txt' , 'AMOM: A Manual of Mending and Repairing. 1907' ] +, [ '???.txt' , 'Farm Blacksmithing (1901)' ], [ 'FCOA.txt' , 'FCOA: Farmers Cyclopedia of Agriculture. 1911' ] +, [ 'FCOLS.txt' , 'FCOLS: Farmers Cyclopedia of Live Stock. 1908' ] +, [ 'FDAD.txt' , 'FDAD: Furniture Design and Draughting. 1900' ], [ '???.txt' , 'ICS Reference 14' ], [ '???.txt' , 'Pottery for /artists, Craftsmen, and Teachers (1 +930)' ], [ '???.txt' , 'Practical Poultry Production (1910)' ], [ 'TJCB.txt' , 'TJCB: Thomas Jeffersons Cook Book. 1937' ], [ 'TMOL.txt' , 'TMOL: The Making of Leather. 1914' ], [ 'TPSD.txt' , 'TPSD: The Practical Stock Doctor. 1912' ], [ 'TSC.txt' , 'TSC: The Settlement Cookbook. 1903' ], [ 'TSI.txt' , 'TSI: The Shoe Industry. 1916' ], [ '???.txt' , 'Univeral Household Assistant (1884)' ], [ 'VIRG.txt' , 'VIRG: Virginia Housewife or Methodical Cook. 189 +9' ], [ 'WBH.txt' , 'WBH: Window Blinds. 1907. (Hasluck)' ], [ '???.txt' , 'Wood Finishing, 1903' ], [ 'WHCB.txt' , 'WHCB: White House Cook Book. 1887' ], [ 'WWM.txt' , 'Windmills and Wind Motors, 1910' ], [ 'YCMI.htm' , 'YCMI: You Can Make It. 1929-1931' ], [ 'YOUNGS.htm' , 'YOUNGS: Youngs Demonstrative Translation of Scie +ntific Secrets. 1861' ], ); sub search_the_file { croak("Wrong number of arguments") if @_ != 2; my ( $fh, $search_string ) = @_; my $search_qm = quotemeta $search_string; my $search_re = qr{$search_qm}i; # i for insensitive-to-case my @found; while (<$fh>) { chomp; push @found, $_ if uc($_) =~ /$search_re/; } return @found; } my $separator_line = '<center><img border="0" src="/article_separator.png"></center>< +br>' . "\n"; $separator_line = "---\n"; # Overridden for PerlMonks testing my $count_total = 0; BOOK: for my $book_aref (@book_indexes_titles) { my ( $index_file, $book_title ) = @{$book_aref}; print $separator_line; open my $fh, '<', "$index_dir/$index_file" or do { print "<center>ATOCI not found for book <b>$book_title</b></ce +nter><br>\n"; next BOOK; }; my @lines = search_the_file( $fh, $wanted_string ); close $fh; $count_total += scalar @lines; print "<center><b>$book_title</b></center><br>\n"; for my $line (@lines) { print "<center>$line</center><br>\n"; } } print $separator_line; print "$count_total records found.<br>\n"; print $separator_line;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: losing html in print calling function.
by Librum (Initiate) on Dec 03, 2011 at 03:53 UTC |
In Section
Seekers of Perl Wisdom