Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2018-06-23 07:00 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.