in reply to Re^4: Manipulating directory paths
in thread Manipulating directory paths

"If $ver_name is present" is meaningless, since I'm pretty sure you don't want to check if the variable exists. I'm guessing from your code that you want to check if the file named by $ver_name exists (which -f would do), but it could mean many other things.

You're using our. Don't use globals.

Don't use the conditional operator (?:) to conditionally execute entire statements. Using that operator for anything but the simplest expression usually makes it unreadable. It really works best when the returned value is actually used.

The output you said you get is not the output the code produces.
It produces '/home/user/phase/dir1abinitio/run'
and not '/home/user/phase/dir1//abinitio/run'
You're missing a / because you removed the / before log without replacing it.
This should have been easy for you to find out by comparing the one that worked and the one that didn't.

my $replace = (-f $ver_name ? "/$ver_name" : '') . '/abinitio/run'; (my $graph_dir = $before) =~ s{ /log/ .* \z }{$replace}xs;

Replies are listed 'Best First'.
Re^6: Manipulating directory paths
by pp (Novice) on Sep 13, 2007 at 18:14 UTC
    $ver_name is a parameter that I am passing to this script. And I want the script to execute in one way if the $ver_name is passed and if it is not passed in an another way.
    And that's why I am checking to see if $ver_name exists or not.

    Also I did try other manipulations with the if-then-else statements to check if $ver_name exists but I was having many problems and that's why I resorted to using (?:)

    In Case 2:
    I am expecting graph_dir to evaluate to = /home/user/phase/dir1/abinitio/run

    but instead I am getting /home/user/phase/dir1//abinitio/run

      $ver_name is a parameter that I am passing to this script. And I want the script to execute in one way if the $ver_name is passed and if it is not passed in an another way.

      You'd normally check the length of @ARGV (for arguments passed to a script) or @_ (for arguments passed to a function), but defined will do nicely here. defined tells its caller if a variable holds a value other than undef.

      my $replace = '/abinitio/run'; if (defined($ver_name)) { $replace = "/$ver_name" . $replace; } (my $graph_dir = $before) =~ s{ /log/ .* \z }{$replace}xs;

      but instead I am getting /home/user/phase/dir1//abinitio/run

      Not with the code and the inputs you provided.
      Not with the code I provided and the inputs you provided.

      Update:
      Changed
      if (defined($replace)) {
      to
      if (defined($ver_name)) {

        I tried what you suggested but still I a getting the double slashes //.