Web Hosting Forums

Results 1 to 11 of 11

This is a discussion on Weird prob w/Perl in the Hosting Talk & Chit-chat forum
I can't figure this one out. In a Perl program, here's what I got: $test = "Two Words"; print "<input type=\"text\" name=\"foo\" value=$test>\n"; When I ...

  1. #1
    Loyal Client
    Join Date
    Aug 2002
    Posts
    266

    Weird prob w/Perl

    I can't figure this one out. In a Perl program, here's what I got:

    $test = "Two Words";

    print "<input type=\"text\" name=\"foo\" value=$test>\n";


    When I run the script, instead of getting "Two Words" in the form box, I get just the first word "Two".

    I can't figure this out. I changed the line to this:

    print "<input type=\"text\" name=\"foo\" value=$test>$test\n";

    And I get:

    Two Two Words

    (the first Two is in the text box)

    Can anybody help me with this?

  2. #2
    Just Walking...
    Join Date
    Oct 2002
    Location
    England
    Posts
    431
    You need quotes around $test its an HTML issue not perl.

    i.e.
    print "<input type=\"text\" name=\"foo\" value=\"$test\">\n";

  3. #3
    Loyal Client
    Join Date
    Aug 2002
    Posts
    266
    Oh boy I feel stupid!

    Thanks

  4. #4
    Loyal Client
    Join Date
    May 2003
    Location
    Canada
    Posts
    46
    I hate the extra slashes. I prefer to use single quotes wherever possible:

    print '<input type="text" name="foo" value="' . $test . '">' . "\n";

  5. #5
    || $name ne 'R.Stiltskin'
    Join Date
    Jun 2003
    Location
    Tejas
    Posts
    2,414
    Another useful "print" method:

    for HTML:
    print qq[<input type="text" name="foo" value=$test>\n];

    for XHTML:
    print qq[<input type="text" name="foo" value=$test />\n];

    Closing the markup tag differently is not a Perl issue either; it is an HTML issue. Besides avoiding some of the single quote/double quote pitfalls, your variable is evaluated and the quotes will be printed as typed too, perfect for XHTML compliance.
    Todd Sherman

    HistoSoft Corporation
    "Biology in a new form..."
    Home: www.histosoft.com
    Member Services: www.myhistosoft.com

  6. #6
    Loyal Client
    Join Date
    Sep 2001
    Location
    Wichita, KS
    Posts
    1,523
    I like
    print qq~<stuff blah="blrh" />~;


  7. #7
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    5,884
    Originally posted by Spathiphyllum
    for HTML:
    print qq[<input type="text" name="foo" value=$test>\n];

    for XHTML:
    print qq[<input type="text" name="foo" value=$test />\n];
    You forgot to quote the value argument's value in this example. In HTML you'll still get the originally reported problem. In XHTML this omission makes your file non-well-formed, since XML requires that you quote (either single of double quotes are acceptable) all attribute values.

    Its just an oversight, but I thought I'd point it out...

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

  8. #8
    || $name ne 'R.Stiltskin'
    Join Date
    Jun 2003
    Location
    Tejas
    Posts
    2,414
    Oops, copy and paste error. Thanks, Jason, for mentioning the typo... I'd hate to lead a fellow scripter down the wrong path.

    Todd
    Todd Sherman

    HistoSoft Corporation
    "Biology in a new form..."
    Home: www.histosoft.com
    Member Services: www.myhistosoft.com

  9. #9
    Loyal Client
    Join Date
    Aug 2002
    Posts
    266
    Well thanks for the lesson here guys. I'm really liking the qq quoting thing. Those backslashes were driving me insane.

    I'm sure I could figure this one out, but I just folded up shop for the evening. I ran a line like this:

    print qq(<p>Admission is $12.00 for adults);

    Do I need to escape that $ with a backslash (it prints as ".00")? Is there anything else I would need to escape using qq?

  10. #10
    || $name ne 'R.Stiltskin'
    Join Date
    Jun 2003
    Location
    Tejas
    Posts
    2,414
    Ah yes... the scalar variable name tag. This ($) needs to be escaped whenever you really want to explicitly use a text dollar sign. Incidently, the qq stands for "double quote interpolation" when used in the print context. It is the equivalent of saying "Print everything in between the content delimiters and, while you're at it, interpolate all variables and print their values."

    Off the top of my head, @ (array) and % (hash) would need to be escaped too if you wanted the literal string. There are probably others too. Fortunately, Perl's innate debugging features (see Perl's manpages for references) will let you know the error of your ways; or, as you have seen, if you get an unexpected result, just escape it to see if it fixes your problem. That's quick and dirty but it usually works.

    Todd
    Todd Sherman

    HistoSoft Corporation
    "Biology in a new form..."
    Home: www.histosoft.com
    Member Services: www.myhistosoft.com

  11. #11
    Loyal Client
    Join Date
    Sep 2001
    Location
    Wichita, KS
    Posts
    1,523
    print qq(<p>Admission is $12.00 for adults);
    should be:
    print qq(<p>Admission is \$12.00 for adults);

    But you could do this:
    print '<p>Admission is $12.00 for adults';
    or
    print q(<p>Admission is $12.00 for adults);

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •