Ananda has asked for the wisdom of the Perl Monks concerning the following question:

Here is code that that will fetch the parent directory. I am encountering parsing errors (on Windows Environment). Also i am unclear about how exactly the regular expression is processed /interpreted. Kindly do Explain this code..
$path = "\\default\\main\\Anand\\toipcs\\Tutorials\\internet"; $curPath = $path; $curPath =~ !\default\main\(.*)\Tutorials\internet!$1!; $parentdir = $curPath; print "parent Directory : $curPath\n"; ($Pdir,$Sdir) =~ m!(.*)\(.*)$!$1,$2; print "parent branch : $Pdir \t sub branch $Sdir";
Please do post your comments and also please suggest alternate ways to acheive this functionality. Thanks in advanse., Anandatirtha

Edit: Added <code> tags. larsen

Replies are listed 'Best First'.
Re: Get Parent Directory
by gjb (Vicar) on Oct 22, 2002 at 10:11 UTC

    You might want to have a look at File::Basename, a module that is part of the standrad distribution.

    It allows to extract the file name, file extension and directory name of a given path. This is what you want to achieve, I think.

    Hope this helps, -gjb-

Re: Get Parent Directory
by alfie (Pilgrim) on Oct 22, 2002 at 10:38 UTC
    Your parsing errors result due to different things:
    • You have to escape the \ in the regexp. \d has a special meaning (a digit) within a regexp, \t is a tab and so on, so you have to double the \ in your substitute, too.
    • You should not use .* at all. Please read Death to Dot Star! for more information on this. In here you like to replace it with [^\\]* to match everything except an \.
    • You were missing the s infront of the substitute you liked to do.
    • In your assignment to $Pdir and $Sdir you did forgot that you want to match $parentdir, and the $1,$2 at the end is, uhm, simply wrong.
    Your code might look like this:
    # I dislike having to escape everything when I can use '' instead $path = '\default\main\Anand\toipcs\Tutorials\internet'; $curPath = $path; $curPath =~ s!\\default\\main\\([^\\]*)\\Tutorials\\internet!$1!; $parentdir = $curPath; print "parent Directory : $curPath\n"; ($Pdir,$Sdir) = $parentdir =~ m!([^\\]*)\([^\\]*)$!; print "parent branch : $Pdir \t sub branch $Sdir";
    HTH & HAND,
    --
    use signature; signature(" So long\nAlfie");
Re: Get Parent Directory
by BrowserUk (Patriarch) on Oct 22, 2002 at 10:29 UTC

    This may help you understand what the code trying to do.

    # Set the path variable. $path = "\\default\\main\\Anand\\toipcs\\Tutorials\\internet"; # Make a copy of it $curPath = $path; # Extract (what I assume to be) your user/home directory ("Anand"). # However this line won't work because the substitution operator is m +issing the 's' $curPath =~ !\default\main\(.*)\Tutorials\internet!$1!; # ^ There should be an 's' here # Make a copy of the extracted directory name ("Anand") and print it +out $parentdir = $curPath; print "parent Directory : $curPath\n"; # This line doesn't doesn't make any sense at all??. # It's obviously trying to extract 2 different parts of the path fro +om something # But # a) The is no variable to operate on! It would need to look somethi +ng like: # ($Pdir, $Sdir) = $path =~ m!(.*)\\(.*)!; !!!NOTE the double +d backslash (\\)! # b) Quite what the intent of the '$1,$2' at the end was meant to d +o. ($Pdir,$Sdir) =~ m!(.*)\(.*)$!$1,$2; # Nothing will be printed as the line above did nothing! print "parent branch : $Pdir \t sub branch $Sdir";

    However, I think you need to try and explain what it is that you want the code to do, then it may be possible to help you further.


    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
      Hello, My requirement is to split the path into two strings from a given string. given string pattern : \default\main\(.*)\Tutorials\internet. Here the split delimiter will be "(.*)" and this will not be a part of any of the split strings. The first split string will contain all characters before the delimiter while the second string needs to hold all characters appearing after the delimitor viz., $string1="\default\main\"; $string2="\Tutorials\internet"; Hope the above info will clarify as to what i am tryiing. Thanks in advance. Anandatirtha

        If I understand you, this may be what you are wanting.

        #! perl use warnings; # Always use this, it will help you enormously. use strict; # And this also. use diagnostics; # This gives some very useful hints when you are +developing. #I assume that this will come from somewhere else. my $path = "\\default\\main\\Anand\\toipcs\\Tutorials\\internet"; # And that you know this or know how to find it out (hint: use Cwd; is + one way). my $CurDir = 'Anand'; # And that what you want its find the parts before and after the curre +nt directory in the path my ($Pdir, $Sdir) = $path =~ m!(.+)Anand\\toipcs(.+)!; print "Pdir = '$Pdir' and Sdir = '$Sdir'\n";

        When the above program is run, it produces the following output.

        c:\test>207011 Pdir = '\default\main\' and Sdir = '\Tutorials\internet' c:\test>

        I hope this helps, if not or if I can clarify anything for you, ask away :).


        Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!