Chap Harrison wrote:
I want to compute a file pathname based on a "template" at runtime, when I know the value of a variable that the template uses.

In other words, the template for the path name looks like this...

/foo/bar/$project/here

...and I want to evaluate this expression once I have set the value of $project.

Here's what I've got:

#!/usr/bin/perl
use strict;
use warnings;

my $project = "my-project";
my $all_projects_path = '/foo/bar/$project/here';
my $project_path;
my $temp = '$project_path  = "$all_projects_path"';

print "\$project is: $project\n";
print "\$all_projects_path is: $all_projects_path\n";
print "Evaluating: $temp\n";
eval $temp;
print "\$project_path is now: $project_path\n";

-----

Output:

$ doit
$project is: my-project
$all_projects_path is: /foo/bar/$project/here
Evaluating: $project_path  = "$all_projects_path"
$project_path is now: /foo/bar/$project/here

-----

The variable $project wasn't interpolated.
I've read perldoc on 'eval', and googled a bit, but no joy.
I know this is going to be a forehead-slapper, but what have I done wrong?

Why not use sprintf() and '/foo/bar/%s/here' as the template:

my $project = "my-project";
my $all_projects_path = '/foo/bar/%s/here';
my $project_path = sprintf $all_projects_path, $project;




John
--
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to