Shlomi Fish wrote:
Hi Chris,
A few comments on your code - some of which may help you.
On Monday 12 Apr 2010 12:06:16 Chris Coggins wrote:
I'm having trouble getting a piece of data from a form input to print in
html. Here's the relevant portion of my code
sub subroutine {
I hope you didn't call your subroutine "subroutine". It should have a
descriptive name. Furthermore, next time please include the entire code or
something that can reproduce it.
I changed the terms of the actual data/subroutines/etc to prevent
exposing company data, since I was sort of cussed out for not doing so
in a previous discussion. Therefore the terminology has been "blanded"
to the fullest extent possible. Furthermore, the entire code is about 3k
bytes. I extracted the portion that is central to the problem I'm
having. The entire code goes something like this (narrative)
0-declarations [ my (\%fields) ]
1-read the form data (take all the data into the hash)
2-print some plain html
3-print some form data in html format
4-print some more plain html
5- print some more form data in html format
6-print another form that launches another script with user input.
Each of these steps is executed by calling a subroutine. Each subroutine
that needs data from the hash references the hash like this:
printformdatastep3(\%fields);
The subroutine begins:
my ($hash) = shift;
and then does whatever it needs to do with the data.
My problem is that the data is recognized fine in step 5, and I can
successfully print a whole mess of data in a series of tables. But in
step 3, using the exact same code, no data is found. All the variables
are essentially empty, including identical data that is again referenced
and printed in step 5.
my($hash) = shift;
1. Please add a space after the my so it will be clearer.
2. Generally you should do either of:
{{{
my $hash = shift;
}}}
Or:
{{{
my ($hash, $param2, $param3) = @_;
}}}
And don't call your variable "$hash" as it's not descriptive. (And at the very
least it's a hash-*reference* not a hash.).
my($data) = "$hash->{'expl'}";
Again << my $data = $hash->{'expl'}; >> or << my $data = $hash->{expl}; >>
would be fine. No usual need to stringify it and the (...) will make it be
evaluated in list context which may cause problems in the future (tough
probably not here.)
print "Content-type: text/html\n\n";
You should use << print $cgi->header(); >> or the equivalent for non-CGI.pm
code.
print <<STOPHTML;
You should always say <<"STOPHTML" <<'STOPHTML' <<`STOPHTML` etc. with
explicit quotes depending on what you say. Otherwise, you may not be sure that
it's doing the right thing (nor will your readers). See Perl Best Practices
for more information.
<div class="empdata">This employee has $data</div>
STOPHTML
}
The result is This employee has
Seems like $data is empty for some reason. You can try debugging the script
locally using perl -d and see where you've gone wrong.
That is the entire crux of the problem. The $data is not showing up when
I try to reference it in step three. It shows up fine in step 5. But it
will not appear in step 3.
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/