Monks,
I've been trying to "upgrade" one of my scripts from the now-outdated (so I am told) CGI.pm to a more modern implementation that I had hoped would be mostly a "drop-in" replacement for it: CGI::Tiny. However, my script needs to function on several sets of pages, not all of them having the same form parameters. CGI.pm did not have any trouble with something like:
my $bookparam = $cgi->param('book') || '';
But CGI::Tiny is throwing a hissy fit over this if the form on that particular page does not happen to include the 'book' field, i.e. code execution is totally halted. Surely there is a way to avoid this, but I cannot find it. I tried this:
defined(my $bookparam = $cgi->param("book")) or my $bookparam = "";
...and I tried this:
my $bookparam;
if (defined $cgi->param("book")) {$bookparam = $cgi->param("book")};
...but both of these generate show-stopper errors, like this one (in the logfile):
[Sun Dec 11 22:03:35 2022] [error] [client ::1] Can't call method "param" on an undefined value at /Users/me/PERL/ScriptDir/dev.pl line 1026.
...and to the browser:
Status: 500 Internal Server Error Date: Sun, 11 Dec 2022 15:03:35 GMT Content-Type: text/plain Content-Length: 25 500 Internal Server Error
Obviously, that's not going to work. Must I return to CGI.pm? If a total code rewrite is required in order to switch, it's too big of an ask. I have tens of thousands of lines of code in this project, and hundreds of form fields.
Aside from a
defined(%hash) is deprecated error in the log, no other errors are produced--and that "defined(%hash)" issue is part of debugging code that only runs in debug mode anyhow. And yes, "strict" is in use.
How can I detect an undefined field before crashing with CGI::Tiny on it?
NOTE: Before suggesting them, please understand I am not interested in Dancer, Dancer2, Mojolicious, Catalyst, Moose, etc. or other heavy-duty frameworks. I am not using PSGI nor FastCGI, and am not looking for something with large overhead. This is why I have tried CGI::Tiny.
Note 2: Online searches have not helped--results are mostly focused on folk with faulty package installations.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.