user2674514
user2674514

Reputation: 141

Substitution function to parse path string

I have a string:

$path='/home/usr/project/name'

I am trying to use the substitution function to parse the string and get just the name. My desired outcome is:

$path=name

I tried using with no success:

$path =~ s/^.+\///;

I'm not sure if the syntax is correct. Any help would be appreciated.

Upvotes: 1

Views: 83

Answers (1)

toolic
toolic

Reputation: 62037

File::Basename is a more portable solution than using s///:

use warnings;
use strict;
use File::Basename qw(basename);

my $path = '/home/usr/project/name';
$path = basename($path);
print $path, "\n";

Output:

name

Upvotes: 6

Related Questions