Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's a better way to write this?

Status
Not open for further replies.

mtorbin

Technical User
Nov 5, 2002
369
US
Here's the full code:

Code:
#!/usr/bin/perl -wT

print "Content-type: text/html\n\n";

my $newDirectory = substr $ENV{"QUERY_STRING"};

if ($newDirectory) {
    opendir (FTPDIR, "$newDirectory") || Error ('open', 'directory');
}

else {
    opendir (FTPDIR, "/var/[URL unfurl="true"]www/html/effTeePee")[/URL] || Error ('open', 'directory');
}


my @folderContents = readdir (FTPDIR);
closedir (FTPDIR);

print <<"HTML code";
<html>
<head>
<title>Untitled Document</title>
<link href=../ftpStyle.css rel=stylesheet type=text/css>
<script src=../jsLib.js></script>
</head>
<body>
<table width=80% border=0 align=center cellpadding=0 cellspacing=0>
<tr align=center valign=top>
<td width=19 height=20><img src=../images/box_gray_corner_tl.gif width=19 height=20></td>
<td background=../images/box_gray_side_t.gif><img src=../images/box_gray_side_t.gif width=2 height=20></td>
<td width=19 height=20><img src=../images/box_gray_corner_tr.gif width=19 height=20></td>
</tr>
<tr align=center valign=top>
<td background=../images/box_gray_side_l.gif><img src=../images/box_gray_side_l.gif width=19 height=2></td>
<td bgcolor=#f2f2f2>
<table width=100%  border=0 cellspacing=5 cellpadding=0>
<tr valign=top>
<td width=3% align=center>&nbsp;</td>
<td width=80% class=tableHeader>Name</td>
<td width=7% class=tableHeader>Date</td>
<td width=10% class=tableHeader>Size</td>
</tr>
<tr valign=top>
<td align=center><img src=../images/icon_folderOpen.gif width=27 height=22></td>
<td>/ Folder Name </td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
HTML code

if (@folderContents) {
   foreach my $indivFile (@folderContents) {
       next if $indivFile=~m/^\.+$/;
       my $fullUnit = $indivFile;
       # escape the following "."
       my ($name, $extension) = split (/\./ , $indivFile);
       
       if ($extension) {
           print "<tr valign=top>\n";
           print "<td align=center><img src=../images/icon_" . $extension . ".gif width=18 height=22></td>\n";
           print "<td><a href=fileDownload.cgi?fileName=" . $fullUnit . ">$fullUnit<a></td>\n";
           print "<td>[DATE]</td>\n";
           print "<td>[SIZE]</td>\n";
           print "</tr>";
       }
       
       else {
           print "<tr valign=top>\n";
           print "<td align=center><img src=../images/icon_.gif width=20 height=22></td>\n";
           print "<td><a href=listFiles.cgi?newDirectory=" . $fullUnit . ">$fullUnit<a></td>\n";
           print "<td>&nbsp;</td>\n";
           print "<td>&nbsp;</td>\n";
           print "</tr>";
       }
   }
 }

print <<"HTML code";
</table>
<br>
<br>
</td>
<td background=../images/box_gray_side_r.gif><img src=../images/box_gray_side_r.gif width=19 height=2></td>
</tr>
<tr align=center valign=top>
<td width=19 height=20><img src=../images/box_gray_corner_bl.gif width=19 height=20></td>
<td background=../images/box_gray_side_b.gif><img src=../images/box_gray_side_b.gif width=2 height=20></td>
<td width=19 height=20><img src=../images/box_gray_corner_br.gif width=19 height=20></td>
</tr>
</table>
</body>
</html>
HTML code

The area that I'm having a problem with is here:

Code:
my $newDirectory = substr $ENV{"QUERY_STRING"};

if ($newDirectory) {
    opendir (FTPDIR, "$newDirectory") || Error ('open', 'directory');
}

else {
    opendir (FTPDIR, "/var/[URL unfurl="true"]www/html/effTeePee")[/URL] || Error ('open', 'directory');
}

First question, if this is the first time running this script or this script is run from the host directory, then $newDirectory will be null. How do I avoid crashing this script?

Second, if this script is run from a later position, it WILL have a value. Should this run in its current state?

- MT
 
Hi

Take a look at the [tt]substr[/tt] syntax in the man :
[tt]
substr EXPR,OFFSET,LENGTH,REPLACEMENT
substr EXPR,OFFSET,LENGTH
substr EXPR,OFFSET
[/tt]

You called it with just one argument.

Feherke.
 
However, this works in my other page.
 
Hi

Strange. I have [tt]perl[/tt] version 5.8.0 for Linux, and for your [tt]substr[/tt] gives a fatal error :
[tt]
Not enough arguments for substr at ./mtorbin.pl line 5, near "};"
Execution of ./mtorbin.pl aborted due to compilation errors.
[/tt]

Feherke.
 
Ok, let me take a step back. Here's the goal:

IF) This script isn't passed any variables, then it should use the directory effTeePee.

ELSE) it is passed variables and it should then use the directory passed to it as in the ELSE:

else {
print "<tr valign=top>\n";
print "<td align=center><img src=../images/icon_.gif width=20 height=22></td>\n";
print "<td><a href=listFiles.cgi?newDirectory=" . $fullUnit . ">$fullUnit<a></td>\n";
print "<td>&nbsp;</td>\n";
print "<td>&nbsp;</td>\n";
print "</tr>";
}

So where did I go wrong? Do I even need substr?

- MT
 
I have to agree with Feherke - mine gives the same fatal error


Kind Regards
Duncan
 
So how do I go about re-writing it? I'm not sure I'm doing it the right way.

- MT
 
use CGI.pm to get the query string value:

at the beginning of the script:

use CGI qw/:standard/;
print header;
my $newDirectory = param('directory') || undef;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top