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

Im the guy that asked the quesiton about the forum,
I know whats wrong! for some reason I can't use
$query->param('name');
is there something I have to do before I can use that?
Is there something I can use to replace that?

The reason I know I cant use it is because I made a little
test to check, this is it:

#!/usr/bin/perl

print "Content-type: text/html\n\n";

$text=$query->param('message');

print "<HTML><BODY><CENTER>",$text,"</CENTER></BODY></HTML>";

I also tried just putting "print $query->param('name');"
but it didnt work... Can someone please help me with this?
Thanks!

Replies are listed 'Best First'.
Re: I almost got it!
by btrott (Parson) on Apr 25, 2000 at 00:25 UTC
    You have to do this:
    use CGI; my $query = new CGI;
    $query is a CGI object (perldoc CGI), so you have to load in CGI.pm and create the new object.

    And you should turn on warnings and use strict:

    #!/usr/bin/perl -w use strict;
      When using CGI.pm, you may also replace:
      
      #!/usr/bin/perl
      print "Content-type: text/html\n\n";
      
      
      With:
      
      #!/usr/bin/perl -w
      
      use strict;
      use CGI qw(-no_debug :standard);
      my $q = new CGI;
      
      print $q->header; # prints default 
                        # content-type, 
                        # which is text/html
      
      # rest of progam...
      
      __END__
      
       Notes: qw(-no_debug :standard) is
       for disabling command line input
       and only importing the standard 
       functions in CGI.pm, instead of
       ALL of them -- which you probably
       don't need.
      
       $q is the same as $query in your 
       example. You can name that variable
       anything you wish. 
      
       $q is your new CGI object, -> means 
       your actually pointing to a function
       in CGI.pm. the text after the -> is
       the function you would like to use.
      
       i.e. $q->header;
      
            This is your new CGI object ($q),
            referencing the header() 
            function in the 'standard' set
            of functions that you imported 
            from CGI.pm.
      
       Finally, test your program, as you go,
       from the command line like so, to test 
       for errors:
      
       /path/to/perl -wc <program_name>
      
       (where <program_name> is the name of
       your script.)
      
       Cheers!
      
      

      Can you explain that a bit more? I dont know what to do with the code you gave me...
      or tell me where to get that CGI.pm thing at?
      Thanks!

        CGI.pm should already be installed on your system as it comes with Perl, I believe. Do perldoc CGI to see if it's installed. It's a Perl module that gives you nice and easy access to all of the CGI parameters passed in to your script.

        You should put

        use CGI; my $query = new CGI;
        at the top of your script before you do anything like
        my $name = $query->param('name');
RE: I almost got it!
by Anonymous Monk on Apr 25, 2000 at 08:19 UTC
    It looks like you're trying to use CGI.pm to me ... this code will work: #!/usr/bin/perl use CGI; $q = new CGI; $query = $q->Vars; print "Content-type: text/html\n\n"; $text=$query->{message}; print "<HTML><BODY>
    $text
    </BODY></HTML>"; ... where you call the script as script.cgi?message=whatever
      Ik! Sorry, that was my first post ever ... I didn't realize i had to format the code myself. ________________________
      #!/usr/bin/perl
      
      use CGI;
      
      $q = new CGI;    
      $query = $q->Vars;   
      
      print "Content-type: text/html\n\n";
      $text=$query->{message};
      print "<HTML><BODY>
      $text
      </BODY></HTML>";
      _______________________ Hope that works :-)
RE: I almost got it!
by Anonymous Monk on Apr 25, 2000 at 01:14 UTC
    You are calling a function of a method. First include that class in the program: use CGI; (if you get errors for that you need to download the CGI perlmodule) Then create a variable "$query" of the CGI class: my $query = new CGI; That all you need to do. Try and dig up the documentation on the CGI permodule. Theres alot this thing cna do (like cookies).
RE: I almost got it!
by Anonymous Monk on Apr 25, 2000 at 08:08 UTC
    Hum .. FYI - he is using CGI.pm ... He said that
    
    $text=$query->param('message'); 
    
    print  "..." , $text, "..." ;
    
    dose work...
    One thing I wonder is why print $query->param('message'); didn't work? It should. Now ... print "$query->param('message')"; dosen't work (note the quotes ") ..

    I don't know the 'exact' reason for it, but basically what's happening is that Perl is not seeing that '->' is part of the variable itself. It has to do with how perl does it quoting. It having trouble doing full interpretation of the variable in that context.
    Using: print "...", $query->param('message'), "..."; should work just fine .. I say should cause I normally use concatenation (put a period '.' instead of the commas ',') instead. Concatenation is slow if you keep doing it .. since it's just in a print, I don't think it shows anything down (at least nothing noticeable :) Any other questions?

RE: I almost got it!
by jen (Novice) on Apr 25, 2000 at 03:09 UTC
    Are you forgetting the use CGI qw(:standard); or similar statement?