title: How to go 1 level back for a directory path stored in a variable in perl
body: <p>I am dynamically getting the path of a directory and it is getting stored in a variable. I need to go 1 level down and get the parent directory. Also, the path is a Windows path.
  Basically , i am running perl on windows.
  How to achieve this in perl ? Anything is fine - regular expression or any other way.
  Please help!</p>
  
  <p>For example :</p>
  
  <p>Original directory path :</p>
  
  <pre><code>        my $dir = "C:/mytest/mydata/mywork/mydir";
  </code></pre>
  
  <p>Output what is needed is :</p>
  
  <pre><code>        my $dir = "C:/mytest/mydata/mywork";
  </code></pre>
  
  <p>I need till parent directory "mywork" , not "mydir".</p>
  
  <p>Can anybody help in writing code for the same ?</p>
  
  <p>Second query :</p>
  
  <p>I am having space in the directory path:</p>
  
  <p>example :</p>
  
  <pre><code>      my $testdir = "C:/mytest own/mydata/mywork/mydir";
  </code></pre>
  
  <p>Notice, "mytest own" is one directory but with space.
  Now, when i am trying to enter to use this variable to enter to this directory, i am unable to do so.</p>
  
  <p>For query 1 , I tried the below piece of code :</p>
  
  <p>@Ingo :</p>
  
  <pre><code>          #!/usr/bin/perl
  
            my $dir = "C:/mytest/mydata/mywork/mydir";
  
            print "Input directory is : $dir\n";
  
            my $outdir = $dir . "/..";
  
            print "Output directory is :  $outdir";
  </code></pre>
  
  <p>Note : I need to accomplish this, without using any perl module. </p>
  
  <p>@TLP : I tried using your cut-short method, the code is again copied, but it doesn't give the desired output.</p>
  
  <pre><code>        #!/usr/bin/perl
  
           use strict;
           use warnings;
           use File::Spec;
  
            my $str    = "C:/mytest/mydata/mywork/mydir";
            my @dirs   = File::Spec-&gt;splitdir($str);        # parse directories                                    
            my $newdir = File::Spec-&gt;catdir(@dirs, File::Spec-&gt;updir());      # create new path
            print $newdir;
  </code></pre>
  
  <p>Output looks like : The output gets appended with /..</p>
  
  <pre><code>          C:/mytest/mydata/mywork/mydir/..
  </code></pre>