Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

CGI.pm, combining two strings

by stuffy (Monk)
on May 07, 2001 at 22:42 UTC ( [id://78600]=perlquestion: print w/replies, xml ) Need Help??

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

I am using CGI.pm now. I have a form where the user inputs a couple different strings. here is the html.

print $query->textfield('foo'); print $query->textfield('bar');

the operator inputs "this" for foo and "that" for bar then hits submit.
my question is two-fold. What is the easiest way to have the output as:
"this that" with the space.
"thisthat" without the space.


Stuffy

Replies are listed 'Best First'.
Re: CGI.pm, combining two strings
by Masem (Monsignor) on May 07, 2001 at 22:44 UTC
    #no space print $query->textfield('foo').$query->textfield('bar'); #space print $query->textfield('foo').' '.$query->textfield('bar');

    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
      Here's the fishing rod: the dot operator concatenates strings. In the second example, Masem is concatenating the first variable with a space with the second variable.
Re: CGI.pm, combining two strings
by Sifmole (Chaplain) on May 07, 2001 at 23:01 UTC
    I think that maybe you are looking for the other side of the equation -- getting back the information from the form.

    # Get rid of the ' ', if you do not want the space. print $query->param('foo), ' ', $query->param('bar');
    Update
    If you are simply printing the result you do not need the '.' concatenation operator; A print with the comma will do just as well and ...

    according to some quick and dirty Benchmarking, which is always suspicious, is just a teensy bit quicker. Apparently the difference in speed grows as the length of the strings grows as well as the number of strings being utilized grows. As well at some point the join routine becomes more efficient than a string of '.' operators.

Re: CGI.pm, combining two strings
by TGI (Parson) on May 07, 2001 at 23:45 UTC

    You seem to be a bit confused about how all the parts fit together. Your code is really CGI.pm perl pidgin HTML.

    The plain old HTML would look something more like:

    <FORM METHOD="POST" ACTION="foo.cgi"> <INPUT TYPE="text" NAME="foo"><P> <INPUT TYPE="text" NAME="bar"> <INPUT TYPE="submit"> </FORM>

    The HTML could either be generated by a script or reside in a static file somwhere.

    Here's some code that prints a form and any results of previous submits. I didn't list a form action, becuase it defaults to the page it reside in.

    #!perl use CGI; use strict; my $q = new CGI; print $q->header(), $q->start_html(), $q->h1('Print some input.'), $q->startform(), $q->textfield('foo'), $q->p(), $q->textfield('bar'), $q->submit(), $q->endform(); # Print w/ space print $q->param('foo').' '.$q->param('bar'); # You can't use "" to in +terpolate param(), it's a function call, not a variable. print $q->p(); # Print w/o space print $q->param('foo').$q->param('bar'); print $q->end_html;


    TGI says moo

      correct, I posted the part of the cgi that created the html that made the form. it also appears that I can use a comma or a period according to the posts that I have received from others

      Stuffy

        The comma operator (,) acts as a list separator. When you feed print and bunch of stuff with commas in-between you are saying print this list of things. In a scalar context the comma evaluates the left arg and dumps the value, then it evaluates the right arg and returns the result.print scalar('foo','bar','this'); gets you this

        The period (.) is the string concatenation operator. It combines strings. It also wants its operands in scalar context.

        $a = 'abc'; $d = 'def'; $A = $a.$b; #ie, $A='abcdef' print $A.$a.$b;

        This code prints abcdefabcdef. Here I say print this one thing that you can make by putting these things together.

        Though they seem to do much the same thing when you're printing, they are actually quite different. Watch out for the scalars only aspect of .!


        TGI says moo

Re: CGI.pm, combining two strings
by ckohl1 (Hermit) on May 07, 2001 at 23:01 UTC
    use Strict; use CGI; my $myCGI = new CGI; my $withSpace = $myCGI->param('foo') . ' ' . $myCGI->param('bar'); my $withoutSpace = $myCGI->param('foo') . $myCGI->param('bar'); print "With Space \t $withSpace \n"; print "Without Space \t $withoutSpace";
    Chris
    'You can't get there from here.'
Re: CGI.pm, combining two strings
by JP Sama (Hermit) on May 08, 2001 at 00:02 UTC
    Or you could use the following script:

    #!/usr/bin/perl use CGI; use CGI::Carp 'fatalsToBrowser'; CGI::ReadParse(*in); $foo = $in{'name_of_first_field'}; $bar = $in{'name_of_second_field'}; $withspace = "${foo}${bar}"; $withoutspace = "$foo $bar";
    And do what they told you already:
    print "$withspace\n"; print "$withoutspace\n";

    Good Luck!

    #!/jpsama/bin/perl -w
    $tks = `mount`;
    $jpsama = $! if $!;
    print $jpsama;
    

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2024-04-18 17:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found