Chris Coggins <[email protected]> asked:
> 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 {
> my($hash) = shift;
> my($data) = "$hash->{'expl'}";
>
> print "Content-type: text/html\n\n";
> print <<STOPHTML;
> <div class="empdata">This employee has $data</div>
> STOPHTML
> }
[...]
> I either get a 500 error or the $data string is not there as in the
> first instance of code. Can someone help?
Some notes on your code:
For once, you should not be writing "my($variable)" in an assignment unless you
have understood its difference to writing "my $variable".
To wit:
$ perl -wle 'my $data = localtime; print $data'
Mon Apr 12 11:25:22 2010
$ perl -wle 'my( $data ) = localtime; print $data'
28
Or as another example:
$ perl -wle 'sub foo { print wantarray ? "List context" : "Scalar context" } ;
my( $data) = foo()'
List context
$ perl -wle 'sub foo { print wantarray ? "List context" : "Scalar context" } ;
my $data = foo()'
Scalar context
=> Using my() on the left side of an assignment creates a list context. There
are functions like localtime that return different things depending on the
context that they've been called from. Use my( $variable ) in an assignment
only if you want to grab the first item of an array and discard all of the
other data.
The other things is that you force extrapolation by saying "$hash->{'expl'}";.
Not only is is superfluous in most instances, but it can hurt you, too, if the
thing you're extrapolation isn't a string.
As for you initial problem, there's too little here to do an autopsy.
The list guidelines recommend that you post a minimal code sample that'll
reproduce your problem.
Without access to that sample, I can only recommend that you run your code with
"use CGI::Carp qw/fatalsToBrowser/;" to capture and print the error messages
you're not seeing.
HTH,
Thomas
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/