Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Passing scalars to CGI module

by filmo (Scribe)
on Apr 14, 2001 at 01:34 UTC ( [id://72491]=perlquestion: print w/replies, xml ) Need Help??

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

Normally, one would pass params to the CGI mod like this.
$q->textfield('lastname');
However, I want to pass a scalar to the module but keep getting an error. What I've tried is as follows.
$a = qq|textfield('lastname')|; $q->$a;
Error...
$q->\$a;
Error... I'm sure this must be possible. Any help is appreciated.
--
Filmo the Klown

Replies are listed 'Best First'.
Re: Passing scalars to CGI module
by Trimbach (Curate) on Apr 14, 2001 at 02:49 UTC
    First, let me idly suggest that you re-consider your data structure to use a hash of hashes instead of a hash of arrays. Named lists are LOTS easier to use in situations like yours: your code will become more self-documenting, easier to modify (adding elements is bunches easier), and easier to understand 6 months from now.

    But back to your problem: suppose you did this:

    %sample = ( 'login' =>["Login", 75, 'textfield', 'login'], 'last' =>["Last Name", 75, 'textfield', 'last'], 'first' =>["First Name", 70, 'textfield', 'first'] );
    ...where $sample{'login'}[2] is your CGI method and  $sample{'login'}[3] is the fieldname. Or better yet:
    %sample= ( 'login' =>{name=>"Login", width=>75, method=> 'textfield', field=>'log +in'}, 'last' =>{name=>"Last Name", width=> 75, method=>'textfield', field=>' +last'], 'first' =>{name=>"First Name", width=>70, method=>'textfield', field=> +'first'} );
    That way if you wanted to access the default param of 'last' all you'd have to do is:
    print $q->$sample{'last'}{'method'} ($sample{'last'}{'field'});
    or somesuch.

    Gary Blackburn
    Trained Killer

      Okay, I understand your point about hash of hashes. So,...
      %sample= ( 'login' => {name=>"Login", width=>75, method=> 'textfield', field=>'login', size=>'10', max=>'8' }, etc... );
      Now, to generate the <INPUT TYPE="text" NAME="login" VALUE="" SIZE=10 MAXLENGTH=8> HTML code, I would do that by:
      $q = new CGI; print $q->$sample{'login'}{'method'}->($sample{'login'}{'field'}, $sample{'login'}{'size'}, $sample{'login'}{'max'}); or do I need to do the following: print $q->$sample{'login'}{'method'}->(-name=>$sample{'login'}{'field' +}, -size=>$sample{'login'}{'size'}, -max=>$sample{'login'}{'max'});
      Ultimately, I want to be able to do something like the following.
      @report = ('last','first','phone','payment'); foreach (@report) { print qq|<tr><td width="$sample{$_}{'width'}">$sample{$_}{'name'}|; print $q->$sample{$_}{'method'}->(-name=>$sample{$_}{'field'}, -size=>$sample{$_}{'size'}, -max=>$sample{$_}{'max'}); print qq|</td></tr>|; }

      --
      Filmo the Klown
Re: Passing scalars to CGI module
by MrNobo1024 (Hermit) on Apr 14, 2001 at 02:13 UTC
    If you want to use $a as a method, you only store the method name in it, not the parameters.
    $a = 'textfield'; $q->$a('lastname');
      is it possible to do the following then:
      @a = ('textfield','lastname','Bob',20,15); $q->$a[0[(-name=>$a[1],-default=>$a[2],-size=>$a[3],-max=>$a[4]);
      or even better
      @a = ('textfield',qq|-name=>'lastname'|, qq|-default=>'Bob'|, etc.); $q->$a[0]($a[1],$a[2],$a[3], etc...)
      --
      Filmo the Klown
Re: Passing scalars to CGI module
by filmo (Scribe) on Apr 14, 2001 at 05:39 UTC
    Okay, just wanted to thank everybody for their help. This is truely the great thing about the Perl community. Below is my final code, which now works as I had hoped. I changed the data structure to be hash of hash as suggested. It's a lot clearer.
    %sample = { 'short' =>{name=>"Job ID", width=>40, cgi=>{method=>'textfield',field=>'short',size=>15,max=>10}, div=>"center"}, 'name' =>{name=>"Job Name", width=>150, cgi=>{method=>'textfield',field=>'name',size=>35}} }; foreach (keys %sample) { $method = $sample{$_}{'cgi'}{'method'}; # if method doesn't exist, you get undef sub error in CGI pm if ($method) { print $q->$method(-name=>$job{$_}{'cgi'}{'field'}, -default=>$job{$_}{'cgi'}{'default'}, -size=>$job{$_}{'cgi'}{'size'}, -maxlength=>$job{$_}{'cgi'}{'max'} ); } print "\n"; }
    --
    Filmo the Klown
Re: Passing scalars to CGI module
by converter (Priest) on Apr 14, 2001 at 01:57 UTC

    Assuming that $q is a blessed CGI reference, $q->textfield() (with the proper arguments) would return an HTML text input field, which you would normally print to stdout as part of a web page.

    If you're trying to retrieve query paramters, you want to use something like:

    $var = $q->param('lastname');

    Update:

    Ok, I think I misunderstood what you were aiming for. MrNobo1024 hit the nail on the head.

Re: Passing scalars to CGI module
by thabenksta (Pilgrim) on Apr 14, 2001 at 01:37 UTC

    May I ask why you are trying to do this?

    There might be a better way to do what your trying to accomplish.

    my $name = 'Ben Kittrell'; $name=~s/^(.+)\s(.).+$/\L$1$2/g; my $nick = 'tha' . $name . 'sta';
      I've set up a hash which contains information about individual column in a mySQL file. The hash contains the mySQL column name, the "plain text" column name, default column widths for tables, and the default param. Since there are hundreds of different mySQl columns, I don't want to create the CGI param unless that particular column is needed. When generating a report, the columns desired are entered into an array, that array is then stepped through, with each $_ acting as the key to the %hash, pulling all the relevant information as needed about any particular column. example
      %sample = ( 'login' =>["Login", 75, qq|textfield('login')|], 'last' =>["Last Name", 75, qq|textfield('last')|], 'first' =>["First Name", 70, qq|textfield('first')|], 'address' =>["Address", 140, qq|textarea('address','',2,40)|] );
      Thus to create a table of just 'last' and 'first', I could reference the correct element of the hash and retreive the correct column name, the default width, and the cgi param to pass in order to create the correct form element. Additionally, if I'm generating a report, all I would need to pull is the column name and size, etc.

      I could replace the qq|textfield('xxxx')| with $q->textfield('xxx'), which does work, however, I don't want to generate all those cgi params unless they are needed.
      --
      Filmo the Klown

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://72491]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-16 08:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found