How do I set two different strings equal to each other inside a variable in perl? [closed]

Solution 1:

I think this is your question. You have a path template such as dir1/$PathVariable/dir2/dir3 and you want to fill in the $PathVariable with different values.

The quick way to do that is simple double-quoted interpolation:

 my $PathVariable = 'abc';
 my $directory = "dir1/$PathVariable/dir2/dir3";
 print "Dir is <$directory>\n"; # dir1/abc/dir2/dir3

But, you want to do this for a few directories. Iterate through a list of the values you want. Each time through the foreach, $PathVariable gets the next value from @paths:

my @paths = qw(abc xyz);
foreach my $PathVariable ( @paths ) {
    my $directory = "dir1/$PathVariable/dir2/dir3";
    print "Dir is <$directory>\n"; 
    ... do whatever you need to do ...
    }

Now, having figured that part out, there are a few things to think about when creating paths. The File::Spec module that comes with Perl knows how to put together paths appropriate for the system that you are working on. It's a good habit to have so you avoid weird cases:

use File::Spec::Functions;
my $dir = catfile( 'dir1', $PathVariable, 'dir2', 'dir3' );

There are other CPAN modules that do the job too, but that might be a bit much until you solve this issue.