Evals, quotes and backslashes problem

Discussion in 'Perl Misc' started by Paul Burton, Sep 26, 2003.

  1. Paul Burton

    Paul Burton Guest

    I've got some code similar to this:
    $a='$b="a_string"';
    eval($a);
    print $b;

    For the above, this simply prints out "a_string".

    Of course, I actually want something a little more interesting than
    a_string. The output I actually want is the following string:
    \$a_var

    I thought I could achieve this by:
    $a='$b="\\\$a_var"';
    but this only produces the output:
    \

    I've managed to hack around this problem by generating my own special
    string wherever I want a "\$" in the output, and using a string
    substitution to put in the backslash:
    $a='$b="\!\$a_var"'
    eval($a);
    $b =~ s/\!\$/\\\$/g;

    which does the trick.

    Is there a way of doing this without the substitution cludge, with some
    clever combination of quotes and backslashes? I've tried a few things,
    but nothing seems to work!

    Cheers

    Paul.
     
    Paul Burton, Sep 26, 2003
    #1
    1. Advertising

  2. Paul Burton

    Stefan Guest

    Paul Burton wrote:
    > I've got some code similar to this:
    > $a='$b="a_string"';
    > eval($a);
    > print $b;
    >
    > For the above, this simply prints out "a_string".
    >
    > Of course, I actually want something a little more interesting than
    > a_string. The output I actually want is the following string:
    > \$a_var


    I think you want:

    $a = '$b="\\\\\\$a_var"';

    Remember that because you have two sets of quotes, the string is
    subjected to two levels of interpolation once it has been eval()ed.

    After the line above, $a contains:

    $b="\\\$a_var"

    Then, when this value is eval()ed, $b becomes

    \$a_var

    See?


    Stefan
     
    Stefan, Sep 26, 2003
    #2
    1. Advertising

  3. Paul Burton

    Bill Guest

    Stefan <> wrote in message news:<bl1400$j3r$>...
    > Paul Burton wrote:


    > > $a='$b="a_string"';
    > > eval($a);
    > > print $b;


    > I think you want:
    >
    > $a = '$b="\\\\\\$a_var"';
    >
    > After the line above, $a contains:
    > $b="\\\$a_var"
    >
    > Then, when this value is eval()ed, $b becomes
    > \$a_var


    This is an excellent example of why doublequotes " " are often
    inferior to single quotes ' ' unless variable substitution is really
    needed.
     
    Bill, Sep 26, 2003
    #3
  4. Paul Burton

    Eric Amick Guest

    On Fri, 26 Sep 2003 11:21:51 +0100, Stefan <> wrote:

    >Paul Burton wrote:
    >> I've got some code similar to this:
    >> $a='$b="a_string"';
    >> eval($a);
    >> print $b;
    >>
    >> For the above, this simply prints out "a_string".
    >>
    >> Of course, I actually want something a little more interesting than
    >> a_string. The output I actually want is the following string:
    >> \$a_var

    >
    >I think you want:
    >
    >$a = '$b="\\\\\\$a_var"';


    Ugh. You'll find something like

    $a = '$b = q(\$a_var)';

    is less painful--and even readable.

    --
    Eric Amick
    Columbia, MD
     
    Eric Amick, Sep 27, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris White

    Quotes/Double Quotes in Image Control

    Chris White, Sep 22, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    4,913
    Hermit Dave
    Sep 22, 2004
  2. Chris
    Replies:
    1
    Views:
    13,758
    Oisin
    Mar 24, 2006
  3. Phil Rhoades
    Replies:
    3
    Views:
    182
    Logan Capaldo
    Dec 20, 2005
  4. kj
    Replies:
    0
    Views:
    103
  5. CM
    Replies:
    11
    Views:
    112
    Terry Reedy
    Jan 31, 2014
Loading...

Share This Page