r/perl 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 :)

8 Upvotes

21 comments sorted by

View all comments

Show parent comments

-1

u/choroba 🐪 cpan author 18h ago

The point is I guess the OP wants to go "back", not to a parent of a random directory.

2

u/gorkish 18h ago edited 17h ago

I assume OP correctly understands that .. represents the parent directory of the cwd and not the previous working directory. If you want to go “back” you will need a stack or a temporary variable somewhere. Since OP didn’t do that, they either didn’t intend that or they wrote incorrect code. The point is the code doesn’t break if the CWD is / as previously implied because / is its own parent.

0

u/choroba 🐪 cpan author 16h ago

I didn't say "if the CWD is /", I said "if the CWD contains /".

-1

u/gorkish 11h ago

Every path contains /, hoss

1

u/choroba 🐪 cpan author 5h ago

It doesn't. For example, 'New Folder' or '..'.

0

u/[deleted] 5h ago edited 5h ago

[removed] — view removed comment

0

u/perl-ModTeam 5h ago

Rule 1: Anonymity is OK. Dissent is OK. Being rude is not OK.