Mark Knoop <> wrote:
: Steve Dawson wrote:
:
: : If you're retrieving data from an SQL database, you could see if it
: : supports ISNULL(). It allows you to select an alternate value that
: : will be returned if the requested field is null. Maybe you can use
: : that to avoid your undef...
:
: This was one way to approach it but it still means I have to go
: through and tend to each field in each query individually which
: I feel is not really bomb-proof as I won't know if I've missed
: one until it happens. I wondered whether something like this
: could be set globally?
Probably not.
Though I agree with Steve and would probably use SQL to solve
the problem, I can understand your reasoning. There is another
solution that has not been presented. Use a template system which
can handle undefined values. I use HTML::Template, but it is not
limited to HTML. I have used it to customize CSS files and other
templates, for example.
Your original message indicated this was your intention.
foreach my $id (sort keys %values) {
my $value = $values{$id};
print <<EOF;
some stuff
$value
some more stuff
EOF
If you were using HTML::Template, your solution might look
like this.
# Stuff.
# Stuff to get $sth.
my $template = HTML::Template->new(
filename => 'test.tmpl',
die_on_bad_params => 0,
);
$template->param( loop_name => $sth->fetchall_arrayref({}) );
# More stuff.
__END__
# And in test.tmpl
<tmpl_loop loop_name>
some stuff
<templ_var key_name>
some more stuff
</tmpl_loop>
This does two important things. It gets the printed portion of
the script into a separate file, where it can be changed without
mucking up the code. And it gets the programmer out of the "print
it as soon as you produce it" solution mode. Most computers are
amply fast enough that we do not need to print everything
immediately. There is some standard programming approach which
emphasizes this type of output. (MVC, I think.)
Here's an example I wrote yesterday. See how I am only dealing
with data in perl. If a program could care, this one wouldn't care
about how the data is presented. The template takes care of that.
$self is a CGI::Application subclass. The query_templates()
method retrieves the database fields and puts them into a form
H::T understands. The private _load_template() method loads the
template with my regular defaults.
sub register_user_form {
my $self = shift;
# Load template.
my $template = $self->_load_template( 'register.tmpl' );
my $db = Database->new();
$template->param( $db->query_templates( 'register.tmpl' ) );
# Was there an error?
$template->param( error => $self->param( 'error' ) );
# Display template.
return $template->output();
}
One note, in register.tmpl the error field looks like this. If
$self->param( 'error' ) is undefined, <tmpl_if error> will fail
and the error won't show. $self->param( 'error' ) is defined
earlier in this object. On an error in input, the error parameter is
set and the order of execution changes back to printing this form,
thus exposing the error text. A third file (css) controls the
appearance of that text.
<tmpl_if error><p class="error">All fields are required.</p></tmpl_if>
HTH,
Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs