Variable interpolation in Variables possible?

Discussion in 'Perl Misc' started by kw, Nov 17, 2005.

  1. kw

    kw Guest

    Hello Group,

    i would like to do in perl something similar to the eval command in a
    korn shell.

    First i define some scalar variables $a and $b
    my $a="123";
    my $b="abc";

    then i define a scalar $c to be the composition of $a and $b

    my $c='$a $b';

    then many line of code later and probably changes of $a and $b i want
    to use $c for instance so as to print it:

    print $c;

    I know there ist no variable interpolation after $c has been
    substituted but how can i make this happen?

    In the shell i would write

    eval print $c
     
    kw, Nov 17, 2005
    #1
    1. Advertising

  2. kw

    Paul Lalli Guest

    kw wrote:
    > i would like to do in perl something similar to the eval command in a
    > korn shell.


    You have asked a Self Answering Question

    perldoc -f eval

    > First i define some scalar variables $a and $b
    > my $a="123";
    > my $b="abc";
    >
    > then i define a scalar $c to be the composition of $a and $b
    >
    > my $c='$a $b';


    That's simply the value of $a, a space, and the value of $b. If you
    were to evaluate this, you would likely get a syntax error. I think
    you probably want to concatenate the two values:
    my $c = '$a . $b';
    #(or possibly concatenate them with a space in between)
    my $c = '$a . " " . $b';
    #or
    my $c = '"$a $b"';

    > then many line of code later and probably changes of $a and $b i want
    > to use $c for instance so as to print it:
    >
    > print $c;
    >
    > I know there ist no variable interpolation after $c has been
    > substituted but how can i make this happen?
    >
    > In the shell i would write
    >
    > eval print $c


    In perl, you want to evaluate the contents of $c as perl code, and
    prent the results:
    print eval $c;

    Putting it together:
    #!/usr/bin/perl
    use strict;
    use warnings;

    my $a = '123';
    my $b = '456';
    my $c = '"$a $b"';

    print "C: ", eval $c, "\n";

    $a = 'abc';
    $b = 'def';
    print "C: ", eval $c, "\n";
    __END__

    C: 123 456
    C: abc def

    Paul Lalli
     
    Paul Lalli, Nov 17, 2005
    #2
    1. Advertising

  3. Paul Lalli wrote:
    > kw wrote:
    > > i would like to do in perl something similar to the eval command in a
    > > korn shell.

    >
    > You have asked a Self Answering Question
    >
    > perldoc -f eval


    However you may want to consider avoiding eval.

    See FAQ: "How can I expand variables in text strings?"
     
    Brian McCauley, Nov 17, 2005
    #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:
    671
    Daniel Kirsch
    Feb 24, 2006
  2. Lord0
    Replies:
    1
    Views:
    630
    Lord0
    May 16, 2005
  3. Dietrich
    Replies:
    3
    Views:
    162
    Joe Smith
    Jul 25, 2004
  4. phrankster

    Variable Interpolation with %%variable

    phrankster, Aug 9, 2005, in forum: Perl Misc
    Replies:
    9
    Views:
    151
    James Taylor
    Aug 14, 2005
  5. Replies:
    2
    Views:
    207
    smallpond
    Apr 29, 2009
Loading...

Share This Page