Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

capturing terminal directory relative path?

by Anonymous Monk
on Mar 07, 2010 at 15:45 UTC ( #827245=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:


Let's say I am traversing a directory:

/home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82468 /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82480 /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82485 /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82490 /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82767 /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82802 /home/BORK/work/test-backfills/cp_test
I need to isolate the terminal directory path (in this case, 82468, 82480, 82485, etc) because they are ids that I need to insert into xml documents. I've been looking at the various file modules in cpan, but I can't figure out what to use. Maybe File::Spec? Any examples would be much appreciated.

Replies are listed 'Best First'.
Re: capturing terminal directory relative path?
by liverpole (Monsignor) on Mar 07, 2010 at 16:09 UTC
    What you probably want is File::Basename.  It's in the core already (ie. no need to install it).


    use strict; use warnings; use File::Basename; my $base = basename("/home/BORK/work/test-backfills/cp_test/82802"); # Note that $base is now '82802'

Re: capturing name of deepest directory in a tree structure.
by Corion (Pope) on Mar 07, 2010 at 17:17 UTC

    sort your strings, and then compare them using index. Potentially split them into their parts:

    /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82468 /home/BORK/work/test-backfills/cp_test/82480
Re: capturing terminal directory relative path?
by bichonfrise74 (Vicar) on Mar 07, 2010 at 16:46 UTC
    I agree that File::Basename is the robust way to do what you need. But maybe the below code is something you might want to consider as well.
    #!/usr/bin/perl use strict; while (<DATA>) { print + (split( /\// ))[-1] if /\d+/; } __DATA__ /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82468 /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82480 /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82485 /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82490 /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82767 /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82802 /home/BORK/work/test-backfills/cp_test

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://827245]
Approved by moritz
and not a whimper to be heard...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2018-04-22 17:23 GMT
Find Nodes?
    Voting Booth?