ganeshk has asked for the wisdom of the Perl Monks concerning the following question:

Can anyone please tell me how to avoid warnings that HTML::Template::Pro prints to log file. I get a lot of warnings like the following. I understand that I can fix them if I avoid passing undefined values to template parameters. But is there an easier way?

HTML::Template::Pro:in  TMPL_IF at pos 20700: in EXPR:at pos 20779 [64]: non-initialized variable 
HTML::Template::Pro:in  TMPL_IF at pos 20700: in EXPR:at pos 20804 [89]: non-initialized variable
  • Comment on How to avoid warnings in HTML::Template::Pro?

Replies are listed 'Best First'.
Re: How to avoid warnings in HTML::Template::Pro?
by Anonymous Monk on Dec 08, 2009 at 07:32 UTC
Re: How to avoid warnings in HTML::Template::Pro?
by Marshall (Canon) on Dec 08, 2009 at 21:04 UTC
    I am not familar with the module that you are using, but some general comments on techniques to deal with undef vars may be of interest. I personally strive to write code that just doesn't produce warnings rather than restricting the logging level or ignoring them.

    Sometimes when working strings, I use Perl's ||= operator to make sure that some string variable is defined to at least "" (null string). This can also be used to set a null string to some default value! The below code demo's this special ||= operator.

    Example:
    (a) shows how to avoid undef.
    (b) shows a "gottcha" if the string is "0", you probably won't get the result intended! So you have to decide if "0" is some reasonable value or not and if so, handle that differently.
    (c) shows a case case where the value of "xyzzy" remains unchanged. Perl is returning the value of "xyzzy" without evaluating the "" part. This is not a simple "True/False" value. The ||= operator is also very fast.
    (d) shows setting the default "after the fact". Often you set a var to some default value and then override the default in a subsequent statement. However sometimes (like maybe dealing with a regex pattern match that fails), it is more convenient to have a syntax that says "hey if it didn't work, then this is the default string that I wanted" when result was undef or null string.
    (e)shows a more complex example, I took this from one of my programs (of course "var names have been changed to protect the innocent"). I did it this way to simplify the thing that outputs this structure. I could have chosen not to create a value for {'key2'} but then I'd have to do something on the output end to figure out that this value didn't exist.

    #!/usr/bin/perl -w use strict; my $a = undef; print "a=$a\n"; #warning undef var ! $a ||= ""; # $a = $a || ""; print "a=$a\n"; #no warning "" null string my $b= "0"; #gottcha with string of "0" print "b=$b\n"; $b ||= ""; print "b=$b\n"; #sets b to "" (changes "0" to "") my $c = "xyzzy"; $c ||= ""; print "c=$c\n"; #c still "xyzzy" !! my $d = ""; $d ||= "default"; print "d=$d\n"; #d is "default" my %hash; my $e = ""; $hash{'key1'}{'key2'} = $e ||= "--"; print "$hash{'key1'}{'key2'}\n"; #prints "--" __END__ Output: Use of uninitialized value $a in concatenation (.) or string at C:\TEM +P\scratch.pl line 5. a= a= b=0 b= c=xyzzy d=default --

      I am sorry for the late follow up on this. I got stuck with some other things. However let me tell you that the problem got solved for me. It was a bug in my code. When I fixed it this problem vanished! I am very much grateful for the help provided by the anonymous monk and Marshall.

      I appreciate Marshall's detailed description of defined checks. Talking about them I think it's worthy to mention the handy new defined-or operator // which got introduced in perl 5.10. It can be used for some of the cases in Marshall's examples also.

        Thanks for your thanks and notes on Perl 5.10! Have a happy holiday too!

        I will keep in mind Perl 5.10 I still work a lot with Perl 5.8 and thank goodness to a much lesser extent with 5.6!

        $a // $b means: defined $a ? $a : $b $c //= $d; means: $c = $d unless defined $c; This is a bit different than $c ||= $d in <Perl 5.10.