Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is it possible to evaluate?

Status
Not open for further replies.

ns705

Programmer
Feb 4, 2004
29
GB
Hi,

can you please help me with one problem?

I read from some file the lines looking like
/dir1/dir2/$dirX/dir3/$dirY/filename.
$dirX and $dirX are some variables, they are passed to perl program as input parms.
In which way can I set the variable $path=the above value with substitution of $dirX and $dirY?
I attempted to use eval command but failed with it.

Is it possible to eval it, as it can be done in ksh script?

Thank you in advance.

Anta
 
Following works. "$dirX" and "$dirY" are replaced in $path by the values passed in on the command line.
Code:
#!perl
use strict;
use warnings;

my ($dirX, $dirY) = (shift, shift);
die qq(dirX and dirY must be specified on command line.\n)
    unless $dirX && $dirY;

my $pathstring = <DATA>;
chomp $pathstring;

my $path;
eval '$path = "' . $pathstring . '"';

print qq(\$path = $path\n);

__DATA__
/dir1/dir2/$dirX/dir3/$dirY/filename
I'm not great when it somes to eval, though. Perhaps someone else can suggest a better solution.
 
Thank you, mikevh!

Yes, it works, and it does what I need and what I have expected.
Thank you for your help.

Kind regards,
Anta
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top