Simple read directory script not working

A

AMT2K5

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
 
P

Paul Lalli

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

Here you begin a massive print statement. Exactly one statement, with
many arguments.
$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
 
T

Tad McClellan

AMT2K5 said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top