Hi Chris,
On Monday 12 Apr 2010 14:25:04 Chris Coggins wrote:
> 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)
The right thing to do in that case is to extract a small self-contained
testcase out of the program and ask questions about that. This will also often
help to isolate the problem and see where you've gone wrong.
If you're going to use generic and non-indicative identifiers in order to
protect the innocent, then write a short warning in English that this is the
case at the top. But don't keep us wondering about it.
> 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.
>
Then try using a debugger to see what's wrong.
> >> 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.
Then please use a debugger or print+Data::Dumper to see what's wrong. Also try
reducing the code to the bare minimum and isolating the problem.
Regards,
Shlomi Fish
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
Stop Using MSIE - http://www.shlomifish.org/no-ie/
Deletionists delete Wikipedia articles that they consider lame.
Chuck Norris deletes deletionists whom he considers lame.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/