Gossamer Forum
Home : General : Perl Programming :

Help Wanted with this little code

Quote Reply
Help Wanted with this little code
Hi all.

Here is my code:
#!/usr/local/bin/perl

$var1 = "http://$ENV{'SERVER_NAME'}$ENV{'DOCUMENT_URI'}";

if ($ENV{'QUERY_STRING'} =~ /jump/) { &jump; }
else { &menu; }

###############################################################
# This is the section that reads the text file and puts #
# it in the option of pull down menu #
###############################################################
sub menu {
unless (open(MYFILE, "menu2.txt")) {
die ("cannot open input file file1\n");
}

# if the program gets this far, the file was
# opened successfully
print "Content-type: text/html", "\n\n";
$line = <MYFILE>;

($link_dest, $link_name) = split ("\@", $line);
print "<FORM METHOD=POST ACTION=\"http://www.test.ca/\~me/cgi-bin/menujump.cgi?jump\">";
print "<select name=\"url\" size=\"1\">";
while ($line ne ""){
print "<option value=\"$link_dest\"\> $link_name";
$line = <MYFILE>;
($link_dest, $link_name) = split ("\@", $line);
}
print "</select><input type=\"image\" src=\"../images/submit.gif\" border=\"0\"></form>";

}

sub jump {
# Read in all the variables set by the form
&parse_form;
$gotourl = $FORM{'url'};
print "Location: $gotourl\n\n";
sub parse_form {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
}
}

...and here it is what i like to know:
how do i tell the program NOT to display $var1 in the pull down menu???
What i like to do is if line in text:

##########TEXT FILE LOOKS LIKE THIS#####

http://www.test.com@Test Page
http:///www.thispage.com@This Page

#########END OF TEXT FILE###############

is equal to $var1 i like the program to skip it and not put in pull down menu....any sugestions....
I try using seek function and chomp...but i think i didn't get it right..
P.S.
I'm just starting with PERL, so please go easy on me....
Thank you
Jarek


[This message has been edited by jarekg28 (edited February 16, 1999).]
Quote Reply
Re: Help Wanted with this little code In reply to
This is just a matter of knowing what specific variables the program uses to build its end results. In order to exclude the variable var1:

$var1 = "http://$ENV{'SERVER_NAME'}$ENV{'DOCUMENT_URI'}";

Which is in essence the server name and document info from the page which requested the CGI itself. The text file actually contains URL's and descriptions for the drop down list, and is separated by @ symbols. This is confusing I think, because some users might confuse this with an email @ symbol as well. To verify and exclude your link from the list, I would simply do the following. Change this segment of code which includes the subroutine sub menu to the following (this person's code is a mess if you ask me):
Code:
sub menu {
# First we print out the HTML header and some info for setting up the jump select.
print "Content-type: text/html", "\n\n";
print "<FORM METHOD=POST ACTION=\"http://www.test.ca/\~me/cgi-bin/menujump.cgi?jump\">";
print "<select name=\"url\" size=\"1\">";

open (MYFILE, "menu2.txt") &#0124; &#0124; die ("cannot open input file file1\n");
$line = <MYFILE>;
while ($line) {
chomp $line;
($link_dest, $link_name) = split /\@/, $line;
if ($link_dest eq $var) {
# Do not include this item in the list
$line = <MYFILE>;
next;
} else {
print "<option value=\"$link_dest\"\>$link_name</option>" if $line;
$line = <MYFILE>;
} #endif
} #endwhile
close MYFILE;
print "</select><input type=\"image\" src=\"../images/submit.gif\" border=\"0\"></form>";
} #endsub
Since I didn't look at the jump, I will assume you're not having any problems with it. PLEASE read and understand this code, and make sure you know exactly what it does. If you're learning, examples are an excellent teaching tool as long as you understand them.

Hope this helps,


------------------
Fred Hirsch
Web Consultant & Programmer
Quote Reply
Re: Help Wanted with this little code In reply to
Hi...

Thanks Fred for this advice, i'll try as soon as i get a moment on front of my computer..

Yes i know this is mess, but this is my first CGI script.....just learning....and trying on examples....
The first part was (some parts) taken from book about PERL and the jump section was taken from newsgroup that i found by accident...
This is why i'm here, i see that i can learn lots of good stuff Wink

Thanks again
Jarek
Quote Reply
Re: Help Wanted with this little code In reply to
Hi Fred.
I did try this code and didn't work....I was not able to display the pulldown menu....

Did you get this working on your server?

Thanks
Jarek