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

I want to read in a file that has a line like:
I $a want $b to $c substitute $d these $e in $f later.
I have the reading in part figured out, :) but how do I substitute the variables for their values (which will change as I run the program)? Thank you for your help.

Replies are listed 'Best First'.
Re: variable expansion
by blakem (Monsignor) on Jul 06, 2002 at 05:31 UTC
Re: variable expansion
by jjohn (Beadle) on Jul 06, 2002 at 13:34 UTC

    I have the reading in part figured out, :) but how do I substitute the variables for their values (which will change as I run the program)?

    I confess that I'm uncertain of the exact question you are asking, so I'll try to answer the easiest question I can make of your post. :-)

    Are you asking:

    How can I print out the values of scalar variables?

    If so, you're in luck. Perl makes this much easier than other C derived languages. The one way to print out values it like this:

    print "I like " . $x . "\n";

    This code uses the dot concatentation operator to join strings together. There is a little magic going on here, in that the value of $x is treated like a string. Consider this code:

    $x = '0001'; print "I like " . $x; print "but I like " . ($x + 0)

    The first print statement will yield "I like 0001", but the second prints "but I like 1". The expression with the + operator in the second print statement yeilds a number, rather than a string. Therefore, the leading zeros are removed before the concatentation occurs ($x remains '0001').

    Perl provides an even simpler way to print out scalars -- interpolation. Perl will attempt to substitute the value for scalars and arrays in double quoted strings.

    $x = '0001'; print "I like $x";

    This code prints "I like 0001". Remember that single quoted strings do not interpolate variables.

    $x = '0001'; print 'I like $x';

    This prints "I like $x". You can prevent variable interpolation in double quoted strings by escaping the sigil (the punctuation mark that precedes variable names).

    $x = 'timmah!'; print "\$x is $x";

    This prints "$x is timmah!".

    For more tips on string interpolation, checkout the perlop manpage under the section "Gory details of parsing quoted constructs".

Re: variable expansion
by fokat (Deacon) on Jul 06, 2002 at 17:39 UTC
    Something like this will do the trick:

    my @lines = &read_all_lines_into_an_array(); # Here you set the values for the variables you want # interpolated later... eval qq{print "$_\n"} for @lines;
    (Since this looks a lot like homework, I won't elaborate on the sub read_all_lines_into_an_array().

    As perl will automatically interpolate the variables for you. However, this approach is *very* dangerous, as you would be trusting "code" coming from outside your script without proper sanity checking. Although the code would only go as far as a print, I would not consider this good programming practice. If you only care for simple scalar interpolation, you can probably do something along these lines:

    for my $line (@lines) { for my $var ('@scalars_to_interpolate) { eval qq{\$line =~ s/\\$var/$var/g}, "\n"; } print $line, "\n"; }
    Provided that you have a list of the scalars to interpolate.

    Regards.