simple folder search

Discussion in 'Perl Misc' started by Ken Saunders, Nov 19, 2004.

  1. Ken Saunders

    Ken Saunders Guest

    Below is the script I've been working on. I know theres something
    wrong with it but can't find it for the life of me. I've made some
    changes to tighen up the logic. Can someone help me get this running.
    Its supposed to search my all of my files and return a page with the
    matchs. I had this working as an html file and a cgi but when I tried
    to merge the two it go broken. Thanks everyone

    #!/usr/bin/perl
    # Ken Saunders
    #11/16/2004
    #itc 216
    #assignment 3
    #this perl script displays a search form, and searchs for matchs on
    html files

    use strict;
    use File::Find;
    use CGI;

    #variable - query
    my $query = param("query");

    # There's nothing in the query string
    if ($query eq '')
    {
    print header();
    print start_html();

    # Displays the search form
    print q(<form action="./assign_3.cgi" method="post">);
    print q(<input type="text" name="query" size="50" />);
    print q(<input type="submit" />);
    print q(</form>);

    print end_html();

    # Bail out early if there are no results to print
    exit;

    #$filePath = $File::Find::name;
    #$filePath =~
    s/(home\/classes\/ksaund01\/public_html)/~ksaund01/ig
    }

    #prints the header wiht the query name in an ordered list
    print header();
    print start_html();
    print "\n<p>For the query $query, these results were
    found:</p>\n<ol>\n";
    undef $/;

    #search
    find( sub
    {
    return if($_ =~ /^\./);
    return unless($_ =~ /\.html/i);
    stat $File::Find::name;
    return if -d;
    return unless -r;

    open(FILE, "< $File::Find::name") or return;
    my $string = <FILE>;
    close (FILE);

    return unless ($string =~ /\Q$query\E/i);
    my $page_title = $_;
    if ($string =~ /<title>(.*?)<\/title>/is)
    {
    $page_title = $1;
    }
    print "<li><a href=\"$File::Find::name\">$page_title</a></li>\n";
    },
    '/~ksaund01/page1.html');

    print "</ol>\n";
    print end_html();
    End
     
    Ken Saunders, Nov 19, 2004
    #1
    1. Advertising

  2. Sherm Pendley, Nov 19, 2004
    #2
    1. Advertising

  3. Ken Saunders

    Ken Saunders Guest

    On Thu, 18 Nov 2004 22:43:05 -0500, Sherm Pendley
    <> wrote:

    >Ken Saunders wrote:
    >
    >> #itc 216
    >> #assignment 3

    >
    >Do your own homework.
    >
    >sherm--


    I am doing my own homework. If you look carefully you'll see the
    code. Its called asking for help when you get stuck, besides reading,
    doing and asking questions are how you learn. I happen to be stuck.
     
    Ken Saunders, Nov 19, 2004
    #3
  4. Also sprach Ken Saunders:

    > Below is the script I've been working on. I know theres something
    > wrong with it but can't find it for the life of me. I've made some
    > changes to tighen up the logic. Can someone help me get this running.
    > Its supposed to search my all of my files and return a page with the
    > matchs. I had this working as an html file and a cgi but when I tried
    > to merge the two it go broken. Thanks everyone


    It would help if you said in which way it went broken.

    > #!/usr/bin/perl
    > # Ken Saunders
    > #11/16/2004
    > #itc 216
    > #assignment 3
    > #this perl script displays a search form, and searchs for matchs on
    > #html files
    >
    > use strict;
    > use File::Find;
    > use CGI;
    >
    > #variable - query
    > my $query = param("query");
    >
    > # There's nothing in the query string
    > if ($query eq '')
    > {
    > print header();
    > print start_html();
    >
    > # Displays the search form
    > print q(<form action="./assign_3.cgi" method="post">);
    > print q(<input type="text" name="query" size="50" />);
    > print q(<input type="submit" />);
    > print q(</form>);
    >
    > print end_html();
    >
    > # Bail out early if there are no results to print
    > exit;
    >
    > #$filePath = $File::Find::name;
    > #$filePath =~
    > s/(home\/classes\/ksaund01\/public_html)/~ksaund01/ig
    > }
    >
    > #prints the header wiht the query name in an ordered list
    > print header();
    > print start_html();
    > print "\n<p>For the query $query, these results were
    > found:</p>\n<ol>\n";
    > undef $/;
    >
    > #search
    > find( sub
    > {
    > return if($_ =~ /^\./);
    > return unless($_ =~ /\.html/i);
    > stat $File::Find::name;
    > return if -d;
    > return unless -r;


    This is wrong, although by accident it works. A file test operator works
    on $_ when you ommit its parameter. In the File::Find callback, $_ is
    just the filename, $File::Find::name is the absolute path.

    The above 'stat' will set the current stat buffer to the special token
    '_' (same is true for any file test operator). File test operators can
    work on those, but yours wont. You have three separate stat() calls: One
    on $File::Find::name, and two on $_. File::Find::find() changes into
    each directory by default, so those two are equivalent in this context.
    So you may just write:

    return if -d or ! -r _;

    Also note that you never need stat() when all you want is executing some
    file test operators.

    > open(FILE, "< $File::Find::name") or return;
    > my $string = <FILE>;
    > close (FILE);
    >
    > return unless ($string =~ /\Q$query\E/i);
    > my $page_title = $_;
    > if ($string =~ /<title>(.*?)<\/title>/is)
    > {
    > $page_title = $1;
    > }
    > print "<li><a href=\"$File::Find::name\">$page_title</a></li>\n";
    > },
    > '/~ksaund01/page1.html');


    Here's the problem: find() expects a directory (or a list thereof) as
    second argument. You're passing it a file (unless of course you have a
    directory named 'page1.html'). Pass it a proper directory and I would
    expect your script to produce the desired results.

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
     
    Tassilo v. Parseval, Nov 19, 2004
    #4
  5. Sherm Pendley <> wrote in message news:<>...
    > Ken Saunders wrote:
    >
    > > #itc 216
    > > #assignment 3

    >
    > Do your own homework.


    Well, he was honest enough to point out it is homework. And he didn't
    ask for us to do it for him. He did it, instead, and only asks for
    some help, which is legitimate, IMHO.


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Nov 19, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. THY
    Replies:
    3
    Views:
    2,936
    Alvin Bruney
    Oct 16, 2003
  2. William LaMartin

    _vti_cnf folder in the bin folder-- a problem?

    William LaMartin, Nov 10, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,700
    Oliver
    Nov 10, 2003
  3. Dan
    Replies:
    7
    Views:
    18,567
    catherine sea
    Oct 28, 2010
  4. Jon Maz
    Replies:
    12
    Views:
    4,780
    Vadim Chekan
    Jul 1, 2004
  5. Abby Lee
    Replies:
    5
    Views:
    426
    Abby Lee
    Aug 2, 2004
Loading...

Share This Page