For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > November 2005 > Simple read directory script not working









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Simple read directory script not working
AMT2K5

2005-11-27, 6:58 pm

use strict;
use warnings;
use CGI::Carp qw(fatalsToBrowser set_message);
use CGI qw(:standard);
use CGI::Pretty qw(:html3);

our $cgi = new CGI;


print $cgi->header(),
$cgi->start_html(-title=>'Aaron's MP3 Converter',
-background=>'1.gif',
-text=>"#A9A9A9"),
$cgi->h1({-align=>'center'},font({face=>"Verdana"},"Aaron's MP3
Converter")),
$cgi->p({-align=>'center'},"<font
face=\"Verdana\">Directory</font>",br),

$cgi->startform({-action=>"",-method=>"POST"}),
$cgi->p({-align=>'center'},
textfield({-name=>"directory",
-maxlength=>"72",
-default=>"",
-size=>"30"}),br,br,"<font face=\"Verdana\">Search
by</font>",br,

popup_menu({-name=>'search',
-values=>['Artist','Song','Album']}),br,br,"<font
face=\"Verdana\">Name</font>",br,

textfield({-name=>'name',
-maxlength=>'72',
-default=>'',
-size=>'30'}),br,br,

submit("Submit"),reset),

$cgi->endform(),
&search(),
$cgi->end_html();

sub search{
my $path = param('directory');
opendir(DIR, $path);

while (my $name = readdir(DIR)) {
h1("found file: $name"),
}

closedir(DIR);
}

What am I missing from this script, that simply reads in a directory
name on my unix account.

When I run the script and type in a real directory, I see a 1 in the
bottom left hand cornor of the screen, nothing if I dont. How can I
display the file contents? Or specifically all with the extension .mp3

Paul Lalli

2005-11-27, 6:59 pm

AMT2K5 wrote:
> print $cgi->header(),
> $cgi->start_html(-title=>'Aaron's MP3 Converter',


Here you begin a massive print statement. Exactly one statement, with
many arguments.
<snip>

> $cgi->endform(),
> &search(),


The latest argument in print()'s argument list here is the *return
value* of the search subroutine.

> $cgi->end_html();


And here, the print statement finally ends

> sub search{
> my $path = param('directory');
> opendir(DIR, $path);
>
> while (my $name = readdir(DIR)) {
> h1("found file: $name"),
> }


I'm willing to bet that if you checked your server's error logs, you
would see several instances of "useless use of scalar in void context
here". The h1() function simply generates a text string. It does not
print it.

>
> closedir(DIR);
> }
>


Here is the last line of the search subroutine. In lieu of any
explicit return() statement, this is the return value of the
subroutine. That is, whatever closedir() returns will be what search()
returns. As you saw, on success, that value is 1.

> When I run the script and type in a real directory, I see a 1 in the
> bottom left hand cornor of the screen, nothing if I dont. How can I
> display the file contents? Or specifically all with the extension .mp3


If you want search() to return a series of h1()-ified strings, create
an array, and push each h1() string into the array in the while loop.
After you've closed the directory, return that array from the
subroutine.

Paul Lalli

Tad McClellan

2005-11-27, 6:59 pm

AMT2K5 <Aaron.Train@gmail.com> wrote:
> use strict;
> use warnings;
> use CGI::Carp qw(fatalsToBrowser set_message);
> use CGI qw(:standard);
> use CGI::Pretty qw(:html3);
>
> our $cgi = new CGI;
>
> print $cgi->header(),



> sub search{
> my $path = param('directory');



Why did you switch from the OO style to the function style?


> opendir(DIR, $path);



You should check to see if you actually got what you asked for:

opendir(DIR, $path) or die "could not open '$path' directory $!";


> while (my $name = readdir(DIR)) {
> h1("found file: $name"),

^
^ what is this comma here for?
^ is this your actual code?

Stick with a single style:

$cgi->h1("found file: $name");


> What am I missing from this script,



Taint checking.

perldoc perlsec


> When I run the script


Run it from the command line instead of in a CGI environment.


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com