Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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:
    1. curl -o test_results.html 'http://www.librum.us/cgi-bin/satoci.cgi?sitem=apple'
    2. open test_results.html (or just `test_results.html`, or double-click on the file; however you open a document on your system)
    You can inspect test_results.html in a editor, to look for problems with the HTML, or make sure that what your HTTP server sent is what your `curl` client recieved, or to change the HTML by hand to see what effect such changes make to the rendered output. This is what allowed me to quickly find your CENTER tag problem.
  • 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;


In reply to Re: losing html in print calling function. by Util
in thread losing html in print calling function. by Librum

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-19 15:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found