http://qs321.pair.com?node_id=936162

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

hi, I am new to CGI an perl. I wrote the following code
$Template = HTML::Template->new(filename => "user_login.html"); $Template->param(USER=>$usr); $Template->param(PASSWD=>$pwd); $final = $Template->output(); $q=CGI->new(); print $q->header(); print $q->start_html('user input'); print $q->h1($final); print $q->end_html();
When i execute the script from my shell it displays the following

Welcome akilesh you have logged in with password root<br>

But from the browser i get only
Welcome you have logged in with password
The variables $usr and $pwd are not getting printed to the browser. What could cause this issue Please help me in this

Replies are listed 'Best First'.
Re: CGI problem
by graff (Chancellor) on Nov 05, 2011 at 17:07 UTC
    You haven't shown us how $usr and $pwd are being set. Presumably, whatever you're doing to assign values to those variables, it behaves one way when it runs in your shell environment, and behaves some other way when it runs via the web server.
Re: CGI problem
by liverpole (Monsignor) on Nov 06, 2011 at 14:36 UTC
        "...the '==' has to be used for numeric and 'eq' in case of strings."

    Yes, '==' for numeric, 'eq' for strings, and '=' for assignments.

    Years ago, I learned a good trick for avoiding this kind of error:

    if ($query_str='') { ... }
    when, in fact, you meant to say:
    if ($query_str == '') { ... }

    If you get in the habit of always putting the constant on the left side of the '==' sign:

    if ('' == $query_str) { ... }
    then, if you accidentally drop one of the '=', you'll get an error:
    if ('' = $query_str) { ... } # Produces: # Can't modify constant item in scalar assignment at example.pl line + 3, # near "$query_str) " # Execution of example.pl aborted due to compilation errors.
    I don't usually make the mistake of writing '=' where I meant '==', but whenever I do, thanks to putting the constant on the left side, the compiler now let's me know about it in no uncertain terms!

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
        Hey Thank you for the timely help. But now i have another problem. My script seems to run fine from command line. but if it gets invoked by the apache server it throws internal server error. When i look into the error log this is what i get.
        [Mon Nov 07 14:59:57 2011] [error] [client 127.0.0.1] Premature end of + script headers: user_login.pl, referer: http://127.0.0.1/login.htm [Mon Nov 07 14:59:57 2011] [error] [client 127.0.0.1] install_driver(m +ysql) failed: Can't load 'C:/perl/perl/site/lib/auto/DBD/mysql/mysql. +dll' for module DBD::mysql: load_file:The specified module could not +be found at C:/perl/perl/lib/DynaLoader.pm line 200., referer: http:/ +/127.0.0.1/login.htm [Mon Nov 07 14:59:57 2011] [error] [client 127.0.0.1] at (eval 6) lin +e 3, referer: http://127.0.0.1/login.htm [Mon Nov 07 14:59:57 2011] [error] [client 127.0.0.1] Compilation fail +ed in require at (eval 6) line 3., referer: http://127.0.0.1/login.ht +m [Mon Nov 07 14:59:57 2011] [error] [client 127.0.0.1] Perhaps a requir +ed shared library or dll isn't installed where expected, referer: htt +p://127.0.0.1/login.htm [Mon Nov 07 14:59:57 2011] [error] [client 127.0.0.1] at C:/Program F +iles/Apache Software Foundation/Apache2.2/cgi-bin/user_login.pl line +57, referer: http://127.0.0.1/login.htm
        Any idea why could this happen What is Dynaloader.pm
Re: CGI problem
by Anonymous Monk on Nov 06, 2011 at 04:54 UTC
    Here this is the entire script
    use DBI; use CGI; use HTML::Template; use strict; my ($usr,$pwd,$final,@par,$query_str,$Template,$q); $query_str = $ENV{QUERY_STRING}; if ($query_str='') { $query_str = $ARGV[0]; } @par = split (/['&','=']/,$query_str); $usr = $par[1]; $pwd = $par[3]; $Template = HTML::Template->new(filename => "user_login.html"); $Template->param(USER=>$usr); $Template->param(PASSWD=>$pwd); $final = $Template->output(); $q=CGI->new(); print $q->header(); print $q->start_html('user input'); print $q->h1($final); print $q->end_html();
    Another important thing is that when i do not use any mudules I get what i expect that is when i dont use CGI,HTML Template and DBI and simply print the variables to the browser. I am using apache httpd server
      if ($query_str='')
      Your destroying the query string right there. use == or 'eq'
Re: CGI problem
by Anonymous Monk on Nov 06, 2011 at 05:17 UTC
    you are correct. using the 'eq' operator solved it. Correct me if i am wrong. the '==' has to be used for numeric and 'eq' in case of strings.