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

I have a form that sends information using sendmail after the form is completed. I added a part where I want to make sure some variable is always submitted when calling the cgi page. This does work where it redirects if something isnt submitted. My question is $query an object instance of new CGI constructor? Trying to learn the object language.
use CGI; use strict; my $query = new CGI; unless(defined($subject)) { #redirect if someone enters www.mysite.here.cgi into the url print $query->redirect('http://www.mysite.here.htm'); exit(0); } #mail sendmail program part here...
Also is this the most efficient way to make sure a variable exists before sending mail?

Replies are listed 'Best First'.
Re: Redirect and using CGI constructor
by EvdB (Deacon) on Nov 17, 2003 at 15:56 UTC
    You need to set $subject to some value first (strict will not allow the code to run otherwise.) Try this:
    use CGI; use strict; my $query = CGI->new(); my $subject = $query->param( 'subject' ); unless( defined $subject && $subject ) { ...
    The calling convention of my $query = CGI->new(); is generally prefered, your way (my $query = new CGI;)is deprecated.

    You should test that subject is both defined and that it has some value, as $subject = '' is defined but probably not what you want. You might want a regex like $subject =~ m/\w+/;to test that there are some characters in subject at the very least.

    My question is $query an object instance of new CGI constructor? $query is now an instance of the CGI object and so it allows you access to the CGI modules methods.

    --tidiness is the memory loss of environmental mnemonics

      sorry i forgot to add I did have a place where value is declared:
      my $subject = param('subject');
      Also the
      my $query = CGI->new();
      $query is an instance of the CGI contstructor and/or is it a reference to CGI constructor?
        If you really want to know:

        $query is a reference to a hash, and this hashref is blessed into the class CGI. Try running this one liner:

        perl -e 'use CGI; $cgi = CGI->new; print $cgi, "\n\n";'

        The CGI->new() is simply a way of saying run the method 'new' of the module 'CGI'. This method returns the blessed hashref above.

        This can all get really confusing in the beginning, try perldoc perltoot for a fairly nice introduction.

        --tidiness is the memory loss of environmental mnemonics

Re: Redirect and using CGI constructor
by BUU (Prior) on Nov 17, 2003 at 19:55 UTC
    To answer your original question why don't you just do:
    my $cgi = new CGI; my $must_have_param = $cgi->param('foo'); if(not defined $must_have_param) { $must_have_param = 'Alive!'; }
    In this case your actual script just tests if the form input named 'foo' was submitted, and if not assigns's a default value. If your concerned about using the param interface I'm fairly certain you could just do $cgi->param('foo','Alive') if not defined $cgi->param('foo'); To set the param foo to a certain value inside the cgi object.