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

Hi there Monks!
I am getting warnings in my code as this:
Name "CGI::LIST_CONTEXT_WARN" used only once: possible typo at

This is how I start the code, am I using it wrong?
#!/usr/bin/perl use strict; use warnings; use CGI; use DBI; use DBD::ODBC; use Data::Dumper; use JSON; use HTML::Template; use List::Util qw(sum); $CGI::LIST_CONTEXT_WARN = 0; my $q = CGI->new; ....

Thanks for looking!

Replies are listed 'Best First'.
Re: List Context Warn
by ikegami (Patriarch) on Sep 02, 2015 at 18:43 UTC
    { # We might be using a version of CGI from # before the feature was added. no warnings qw( once ); # We realize that $cgi->param can return # more than one value and our code handles # that correctly. $CGI::LIST_CONTEXT_WARN = 0; }
Re: List Context Warn
by toolic (Bishop) on Sep 02, 2015 at 18:21 UTC
    I get this warning too, but I'm using an old version of CGI (3.49). The POD for my version does not mention the $CGI::LIST_CONTEXT_WARN variable. However, the latest version on CPAN (4.21) does mention this variable. If you want to use the variable and you have an old version, you need to update. Note that, according to perl5220delta, CGI was removed from Core.
Re: List Context Warn
by stevieb (Canon) on Sep 02, 2015 at 18:36 UTC

    You can disable that warning by using that variable at least once (eg: print $CGI::LIST_CONTEXT_WARN;), or by using no warnings 'once';, but I'd *highly* recommend against the latter (unless it is used within a limited scope, as pointed out by ikegami below).

    There was a vulnerability found in CGI retrieving a list from the param() method, and a new multi_param() method was created to get around it. From perldoc CGI:

    "Warning - calling param() in list context can lead to vulnerabilities if you do not sanitise user input as it is possible to inject other param keys and values into your code. This is why the multi_param() method exists, to make it clear that a list is being returned, note that param() can still be called in list context and will return a list for back compatibility."

    ...and...

    "If you call param() in list context with an argument a warning will be raised by CGI.pm, you can disable this warning by setting $CGI::LIST_CONTEXT_WARN to 0 or by using the multi_param() method instead"

    So instead of dealing with the warnings of "only once" and having to understand how to protect against the vulnerabilities, get rid of the assignment line ($CGI::LIST_CONTEXT_WARN = 0;) that's causing the warning, use the recommended multi_param() for data you know is a list, and let CGI list warning output tell you when you've got a call in list context with the param() method so you can replace it with the newer multi_param() one.

    -stevieb

      I strongly disagree. I highly recommend no warnings 'once'; rather than misleading the reader by using print to do the same thing. Just be sure to limit the scope in which you disable the warning.

Re: List Context Warn
by Maresia (Beadle) on Sep 02, 2015 at 19:12 UTC
    Hi,
    In my situation this fixed my problem:
    ... my %stuff = ( myresults => sub { names( first => scalar $q->param('name'), second => scalar $q->param('zip'), ), ); ...