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

Hi, Monks: Did a search and didn't come up with anything so I am asking you perl monks here. How can I put a special character into a field name? I am trying to automate a web form filling. But some of the field names contain character "$" or "@". When I try to fill the form like this: $mech->field(ab$cd => $value), the code can not do the job because perl think $cd is a variable. Please help how I can get this done. Thanks. --neverminda
  • Comment on How to put special characters($, @, #) into a field name?

Replies are listed 'Best First'.
Re: How to put special characters($, @, #) into a variable?
by Joost (Canon) on May 26, 2007 at 21:39 UTC
      Thanks, tried many times and finally got it as you said $ascalar = "myemail\@gmail.com". Would you pleas look at the update question? Thanks a lot.

        Do not completely change the node title and contents. Either post a related follow up question in reply or create a new question.

        The OP title and contents were:

        Title: How to put special characters($, @, #) into a variable?

        Content:

        Hi, Monks: Did a search and didn't come up with anything so I am asking you perl monks here. How can I put a special character into a variable? Say I want $ascalar="myemail@gmail.com". I can't get the value I want since perl thinks @gmail as an array. Please help how I can get this done. Thanks. --neverminda

        DWIM is Perl's answer to Gödel
Re: How to put special characters($, @, #) into a field name?
by ww (Archbishop) on May 26, 2007 at 21:53 UTC
    Special characters have to be escaped1 (which is probably the search term you needed) with a backslash, as is done to the "@" in the first line below. (The bars in the second line are merely to make the printout show unambiguously what's in $ascalar.

    $ascalar="myemail\@gmail.com"; print "\$ascalar is |" . $ascalar . "|\n";

    Output of the code above looks like this:

    $ascalar is |myemail@gmail.com|

    And, at the hastiest of scans of your update, neverminda, same answer. BTW, this is the kind of question that's a good argument for reading the docs.

    1 Belated Update: An excellent alternate -- use of appropriate quoting -- is well covered by others and should have been noted sooner, since my initial statement is incorrectly categorical. See the replies from Fletch & shmem below.

      I tried to use $mech->field(ab\$cd => $value) but it didn't work. How should I proceed now? Thanks.

        The left hand side of the fat comma (=>) only works its quote-like magic on a single sequence of word characters; "$" isn't a word character (see \w in perlre) so you have to quote it just like you would any other string. $mech->field( 'ab$cd' => $value )

        Try
        $mech->field( 'ab$cd' => $value);

        Use single quotes for literals.

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}