Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Parsing Pathnames

by carlriz (Beadle)
on Apr 03, 2014 at 20:14 UTC ( #1081001=perlquestion: print w/ replies, xml ) Need Help??
carlriz has asked for the wisdom of the Perl Monks concerning the following question:

I have been having trouble parsing out these pathnames. What I want to do is to extract the name of the last file in the pathname. What is the best way to do this?

my @file_name_array = ( "/proc/cpuinfo", "/proc/meminfo", "/etc/fstab", "/proc/net/pf_ring/info", "/var/log/boot.log", "/etc/sysconfig/network-scripts/ifcfg-eth0", "/etc/sysconfig/network-scripts/ifcfg-eth1", "/etc/sysconfig/network-scripts/ifcfg-eth2", "/etc/sysconfig/network-scripts/ifcfg-eth3", "/etc/sysconfig/network-scripts/ifcfg-eth4", "/etc/udev/rules.d/70-persistent-net.rules" );

Comment on Parsing Pathnames
Download Code
Re: Parsing Pathnames
by toolic (Bishop) on Apr 03, 2014 at 20:24 UTC
    File::Basename is one way...
    use warnings; use strict; use File::Basename qw(basename); my @file_name_array = ( "/proc/cpuinfo", "/proc/meminfo", "/etc/fstab", "/proc/net/pf_ring/info", "/var/log/boot.log", "/etc/sysconfig/network-scripts/ifcfg-eth0", "/etc/sysconfig/network-scripts/ifcfg-eth1", "/etc/sysconfig/network-scripts/ifcfg-eth2", "/etc/sysconfig/network-scripts/ifcfg-eth3", "/etc/sysconfig/network-scripts/ifcfg-eth4", "/etc/udev/rules.d/70-persistent-net.rules" ); print basename($_), "\n" for @file_name_array; __END__ cpuinfo meminfo fstab info boot.log ifcfg-eth0 ifcfg-eth1 ifcfg-eth2 ifcfg-eth3 ifcfg-eth4
Re: Parsing Pathnames ( Path::Tiny )
by Anonymous Monk on Apr 03, 2014 at 23:04 UTC
    use Path::Tiny qw/ path /; print path( qw[ /ro/sham/bo ] )->basename; __END__ bo

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1081001]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2015-07-06 06:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (70 votes), past polls