Gossamer Forum
Home : General : Perl Programming :

Did I miss something...

Quote Reply
Did I miss something...
This one seems onsolvable.

The scripts (actually the sub process_form) should check if a file exist (based on it's input) and open the file.

If the script can't open it, it should only show the search form.

But what happens is no file exists, it prints out the searchform and "Content-type: text/plain Unable to display results." While the script should have returned and not even read the last parts.

Strange or not?

Offcourse I could removed the last part, but I'm curious what I'm doing wrong. Just to learn and understand return better. Because what would happen if return doesn't work and I don't have a second ok check.

Code:
sub main {
# --------------------------------------------------------
local (%in) = &parse_form;

# We are processing the form.
if (keys %in != 0) {
&process_form;
}
# Otherwise we are displaying the form (in site_html.pl).
else {
&site_html_search_form;
}
}

sub process_form {
# --------------------------------------------------------
my ($found, $htmlfile);

my $page = $in{'page'} || '1';
my $dir = $cathtml{$in{'cat'}} || 'all';
my $location = "$build_root_path/$dir/$page.html";

# Let's get the category html-page out of the correct dir.
if (-e "$location") {
$found = 1;
open (FILE, "<$location") or &display ("Unable to open file. Reason: $!");
$htmlfile = join ("", <FILE> );
close FILE;
}
$found or &site_html_search_form and return;


# Now let's display the record...
if ($found) {
&html_print_headers;
print $htmlfile;
} else { # just double checking
&display ("Unable to display results.");
}
}

Last edited by:

Perlboy Chris: Nov 21, 2001, 1:56 PM
Quote Reply
Re: [Perlboy Chris] Did I miss something... In reply to
try adding an exit after:

print $htmlfile;
Quote Reply
Re: [PaulW] Did I miss something... In reply to
I could but I would like to know more about return.

I changed the line

$found or &site_html_search_form and return;

into:

$found or &site_html_search_form('Unable to connect to database.') and return;

and now it works fine. Only why? Due to the ('') ? Because only () didn't help.
Quote Reply
Re: [Perlboy Chris] Did I miss something... In reply to
Hi Chris,

Take a look at [url=http://www.gossamer-threads.com/perl/gforum/gforum.cgi?post=144591;search_string=precedence;#144591]this post[/url] by jagerman.

I think your problem is in this line: $found or &site_html_search_form and return;

Try this;
Code:
if ($found) {
&html_print_headers;
print $htmlfile;
}
else {
&site_html_search_form;
}

return $found;

or something along those lines.

EDIT: You would get rid of the $found or &site_html_search_form and return; line.

Regards,
Charlie

Last edited by:

CP: Nov 21, 2001, 2:33 PM
Quote Reply
Re: [CP] Did I miss something... In reply to
Precedence, could be.

But did you read my second post before posting? because with the ('') extra it works fine. And that should not be the case if ot would be a precedence problem!

Is Jagerman around Tongue
Quote Reply
Re: [Perlboy Chris] Did I miss something... In reply to
You "return" something to another sub-routine, otherwise you "print" it Tongue

eg....
Code:
sub one {

print two();

}

sub two {

return 5;

}

Try:

$found or &site_html_search_form, return;