hi , i want to write a cgi code to open a file and access its contents character by character ( what fgetc does in C) , and then simultaneously search for a pattern (say /blue/ ) and output the results into another file.

How to access the file content chracter by character , i have tried using to do with the "do" function and $/ , but the problem is that in this manner a pattern is looked for only once .

here is the url http://83.138.185.127/cgi-bin/search3.cgi iam pasting the code below .
regards , jagan

----------------------------------------------------------

#!/usr/bin/perl -T use CGI qw(:standard); use CGI qw(warningsToBrowser fatalsToBrowser); my $file1 = param("fil1"); my $key1 = param("phr1"); my $st1 = param("str1"); print header, start_html(-title=>"Searching for a pattern in a file" , + -bgcolor=>"#cccccc"); h1("Searching for a pattern in a file" ); if(param()) { my $file1 = param("fil1"); my $key1 = param("phr1"); my $st1 = param("str1"); my $i =0 ; open(FILE , "$file1") or die("could not open the file $!\n"); while(<FILE>) { my $st1 = do{local $/ , <FILE>}; if($st1=~ m/$key1/) { $i++; print p("The pattern <b> $key1</b> has been found <b> $i</b> times.\n" +); } else { print p("The pattern <b> $key1</b> has not been found.\n"); } print p "<a href=http://83.138.185.127/cgi-bin/search3.cgi> Try Again +</a> " ; } } else { print hr() , start_form(); print p("Enter the file name you wish to make a search into" , popup_m +enu("fil1" , ['file1' , 'file2' ,'file3' ,'file4'])); print p("Enter the key word you wish to search for " , textfield("phr1 +" , "")); print p(submit("go") , reset("clear")); print end_form() , hr(); } end_html();

In reply to acessing the contents of a file character by character by gvs_jagan

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.