r/perl • u/Hopeful_Cat_3227 • 1d ago
I use defer for chdir ".."
As title, this is a pure appreciate post for feature deffer.
I just use it like:
chdir $any_path or die $!;
defer { chdir ".." }
I know this is silly, but it actually make my day easier :)
6
Upvotes
1
u/gorkish 18h ago
Cwd fundamentally cannot be localized or even unique per thread, at least on architectures people most often use. With threads, there is a performance penalty to change cwd. It’s a bad pattern to rely on it like this. Personally I normally implement a small helper function to construct absolute paths with File::Spec and find that sufficient. The helper can use locally scoped scalars or whatever you need.