a problem with variable interpolation..

Discussion in 'Perl Misc' started by Kevin, Oct 11, 2004.

  1. Kevin

    Kevin Guest

    #!/opt/perl_5.8.5/bin

    # heres a peice of script which is troublin me since a while. Might as
    well be # a newbee question!! The problem is,
    # upon single quoting the $sentence ( see below) , and forcing
    evaluation
    # by putting it in double quotes while printing, the string simply
    does
    # not get evaluated as no evaluation is specified during its
    definition

    # upon forcing evaluation by defining $sentence in double quotes, the
    # compiler evaluates it as a system path, and does not interpolate the
    variables.

    # Is there a workaround which would enable evaluation of variables,
    without
    # evaluating it as a path?
    # and thus hopefully print '\dhdu\foo\sdjadf\bar\edjd\huh' ?


    $var1 = 'foo';
    $var2 = 'bar';
    $var3 = 'huh';

    $sentence = "\dhdu\$var1\sdjadf\$var2\edjd\$var3";

    print "$sentence";
     
    Kevin, Oct 11, 2004
    #1
    1. Advertising

  2. Kevin

    Tore Aursand Guest

    On Sun, 10 Oct 2004 23:13:35 -0700, Kevin wrote:
    > #!/opt/perl_5.8.5/bin


    Remember these:

    use strict;
    use warnings;

    > $var1 = 'foo';
    > $var2 = 'bar';
    > $var3 = 'huh';
    >
    > $sentence = "\dhdu\$var1\sdjadf\$var2\edjd\$var3";


    The '\' will escape anything that comes right after it. You probably want
    to escape the '\' character itself, or use the '/' character instead
    (which works just fine on multiple platforms).


    --
    Tore Aursand <>
    "When you love someone, all your saved-up wishes start coming out."
    (Elizabeth Bowen)
     
    Tore Aursand, Oct 11, 2004
    #2
    1. Advertising

  3. Kevin wrote:
    > $var1 = 'foo';
    > $var2 = 'bar';
    > $var3 = 'huh';
    >
    > $sentence = "\dhdu\$var1\sdjadf\$var2\edjd\$var3";
    > print "$sentence";


    See "perldoc -q DOS":
    Why can't I use "C:\temp\foo" in DOS paths? What doesn't
    `C:\temp\foo.exe` work?

    On top of that you are escaping the dollar signs, thereby explicitely
    telling perl to _not expand the variables $var1, ...

    jue
     
    Jürgen Exner, Oct 11, 2004
    #3
    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. \Rob\
    Replies:
    8
    Views:
    684
    Daniel Kirsch
    Feb 24, 2006
  2. Andrew Fabbro

    Variable interpolation question

    Andrew Fabbro, Nov 17, 2003, in forum: Python
    Replies:
    3
    Views:
    300
    anton muhin
    Nov 17, 2003
  3. Oliver Heins
    Replies:
    2
    Views:
    139
    Oliver Heins
    Sep 11, 2006
  4. phrankster

    Variable Interpolation with %%variable

    phrankster, Aug 9, 2005, in forum: Perl Misc
    Replies:
    9
    Views:
    153
    James Taylor
    Aug 14, 2005
  5. Justin C
    Replies:
    3
    Views:
    172
    Paul Lalli
    Jul 7, 2007
Loading...

Share This Page