Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the Path-Class CPAN distribution.

Maintainer(s)' notes

I prefer that bugs & patches are filed on GitHub rather than on RT: https://github.com/kenahoo/Path-Class/issues. Thanks.

Report information
The Basics
Id: 17312
Status: resolved
Priority: 0/
Queue: Path-Class

People
Owner: Nobody in particular
Requestors: CLKAO [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: basename method for dir objects
Attached is the patch and tests to add "basename" method to dir objects. like basename(1), we should have an easy way to get the information just like we can with file objects.
Subject: path-class.patch
Only in Path-Class-0.15.clkao: Makefile Only in Path-Class-0.15.clkao: blib diff -ru Path-Class-0.15/lib/Path/Class/Dir.pm Path-Class-0.15.clkao/lib/Path/Class/Dir.pm --- Path-Class-0.15/lib/Path/Class/Dir.pm 2005-12-16 10:12:44.000000000 +0800 +++ Path-Class-0.15.clkao/lib/Path/Class/Dir.pm 2006-01-27 23:58:28.000000000 +0800 @@ -56,6 +56,8 @@ return Path::Class::File->new(@_); } +sub basename { shift()->{dirs}[-1] } + sub dir_list { my $self = shift; my $d = $self->{dirs}; Only in Path-Class-0.15.clkao: pm_to_blib diff -ru Path-Class-0.15/t/01-basic.t Path-Class-0.15.clkao/t/01-basic.t --- Path-Class-0.15/t/01-basic.t 2005-12-16 10:12:44.000000000 +0800 +++ Path-Class-0.15.clkao/t/01-basic.t 2006-01-27 23:58:58.000000000 +0800 @@ -7,7 +7,7 @@ use Path::Class; use Cwd; -plan tests => 65; +plan tests => 67; ok(1); my $file1 = Path::Class::File->new('foo.txt'); @@ -25,6 +25,7 @@ my $dir = dir('tmp'); ok $dir, 'tmp'; ok $dir->is_absolute, ''; +ok $dir->basename, 'tmp'; my $dir2 = dir('/tmp'); ok $dir2, '/tmp'; @@ -47,6 +48,7 @@ $cat = $dir2->subdir('foo'); ok $cat, '/tmp/foo'; ok $cat->isa('Path::Class::Dir'); +ok $cat->basename, 'foo'; my $file = file('/foo//baz/./foo')->cleanup; ok $file, '/foo/baz/foo';
From: jloverso [...] mathworks.com
On Fri Jan 27 11:07:59 2006, CLKAO wrote: Show quoted text
> Attached is the patch and tests to add "basename" method to dir > objects. like basename(1), we > should have an easy way to get the information just like we can with > file objects.
I was just going to open a request for a 'basename' method, since I find myself writing '$d->dir_list(-1)' to get the basename of a directory. Ken: please release a new version with this patch.
I'd just like to add that I would also very much like to see this ->basename method added to Path::Class::Dir
On Thu Mar 05 12:00:01 2009, THEPLER wrote: Show quoted text
> I'd just like to add that I would also very much like to see this > ->basename method added to Path::Class::Dir
Here's an example where not having this sucks: my @names = map { $_->is_dir ? $_->dir_list(-1) : $_->basename } $dir->children; With this patch, this gets nicer: my @names = map { $_->basename } $dir->children; Note that this example corresponds with what you'd typically get from qx(ls), so it would be good to make it more concise. Basically, I shouldn't have to care whether something is a dir or file in order to get the basename (ie "the last part of the path").
I'd love to have this patch added to the next release...
Thanks everyone, this is now released into version 0.23. -Ken