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: 31264
Status: open
Priority: 0/
Queue: Path-Class

People
Owner: Nobody in particular
Requestors: davidgaramond [...] gmail.com
Cc:
AdminCc:

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



Subject: $dir->chdir() perhaps?
I find myself a couple of times writing this: dir('/path/to/somewhere')->mkpath; chdir('/path/to/somewhere'); How do you think about adding chdir()/cd() method, so we can write this: $dir = dir('/path/to/somewhere'); $dir->mkpath; $dir->chdir; Or even: $dir->mkpath2_or_whatever->chdir; Is it good? Ugly? :-)
From: KWILLIAMS [...] cpan.org
I think there's probably not much point in a $dir->chdir() method, because chdir($dir) works fine. However, I would love to add a *scoped* chdir(), so you could chdir() into a directory and then automatically chdir() back out of it, even if an exception is thrown. This could be done fairly easily with a method that takes a subref indicating the action to perform in a directory. A true chdir() scoped to the current block would take a bit more conjuring. -Ken
On Sat Dec 08 23:57:58 2007, KWILLIAMS wrote: Show quoted text
> However, I would love to add a *scoped* chdir(), so you could chdir() > into a directory and then automatically chdir() back out of it, even > if an exception is thrown.
Currently, it can be done using use Sub::ScopeFinalizer qw( scope_finalizer ); { my $orig_dir = Path::Class::Dir->new('.')->absolute(); chdir($some_dir) or die("Can't change directory: $!\n"); my $sentry = scope_finalizer { chdir($orig_dir) or die("Can't restore directory: $!\n"); }; ... die if $cond; ... } So you could do use Sub::ScopeFinalizer qw( scope_finalizer ); sub scoped_chdir { my $self = shift; my $orig_dir = Path::Class::Dir->new('.')->absolute(); chdir($self) or return; return scope_finalizer { chdir($orig_dir) or die("Can't restore directory: $!\n"); }; } Which would be called as follows: { my $sentry = $some_dir->scoped_chdir() or die("Can't change to dir $some_dir: $!\n"); ... die if $cond; ... } Is it worth it? dunno.
FWIW $dir->chdir(sub { ... }) sounds simple and handy. +1 to that.  A truly automatical chdir() would be even better, but you either need to use unstable magic or an unwieldy sentinel.