File::pushd(3)
π File::pushd β the chdir that cleans up after itself π
okay bestie, so this is a page from the User Contributed Perl Documentation section, and it's about File::pushd(3) β a module that lets you change directories for a hot second and then automatically puts you right back where you were. no cap, it's iconic.
β¨ NAME β¨
File::pushd β change directory temporarily for a limited scope π¦
π₯ VERSION π₯
version 1.016
πΈ SYNOPSIS πΈ
use File::pushd;
chdir $ENV{HOME};
# change directory again for a limited scope
{
my $dir = pushd( '/tmp' );
# working directory changed to /tmp
}
# working directory has reverted to $ENV{HOME}
# tempd() is equivalent to pushd( File::Temp::tempdir )
{
my $dir = tempd();
}
# object stringifies naturally as an absolute path
{
my $dir = pushd( '/tmp' );
my $filename = File::Spec->catfile( $dir, "somefile.txt" );
# gives /tmp/somefile.txt
}
π DESCRIPTION π
So File::pushd does a temporary "chdir" that's super easy to undo β and honestly it undoes itself automatically. It's giving the same energy as "pushd" in some Unix command shells, but make it Perl.
The way it works: it makes an object that caches the original working directory. When that object gets destroyed, the destructor calls "chdir" to slide you back to the original working directory. And because you store the object in a lexical variable with a limited scope, this justβ¦ happens on its own at the end of the scope. no extra code, no cleanup lines, periodt. π«Ά
It's lowkey perfect when you're working with temporary directories for stuff like testing β and there's even a function provided to streamline grabbing a temporary directory from File::Temp. slay.
For convenience, the object stringifies as the canonical form of the absolute pathname of the directory you entered. π
β οΈ Warning, bestie, listen up: if you create multiple "pushd" objects in the same lexical scope, their destruction order is not guaranteed and you might not wind up in the directory you expect. π like, the vibes are unpredictable. keep them in separate scopes, iykyk.
π USAGE π
use File::pushd;
Using File::pushd automatically imports the "pushd" and "tempd" functions for you. no import list needed, we love a low-effort moment. β¨
π pushd π
{
my $dir = pushd( $target_directory );
}
Caches the current working directory, calls "chdir" to change to the target directory, and hands you back a File::pushd object. When that object gets destroyed, the working directory reverts to the original directory. chef's kiss. π
The target directory you pass can be a relative or an absolute path. If you call it with no arguments at all, it uses the current directory as its target and returns to the current directory when the object is destroyed.
If the target directory doesn't exist β or if the directory change fails for literally any reason β "pushd" will die with an error message. π it's not subtle about it.
You can also give it a hashref as an optional second argument. The only supported option is "untaint_pattern", which is used to untaint file paths involved. It defaults to {qr{^("" in -+@\w.+)$}}, which is reasonably restrictive (e.g. it does not even allow spaces in the path β yeah, fr fr, not even spaces). Change this to suit your circumstances and security needs if you're running under taint mode. Note: you must include the parentheses in the pattern to capture the untainted portion of the path. don't forget the parens, bestie, that's the whole point π
π§ tempd π§
{
my $dir = tempd();
}
This function is like "pushd" but it automatically creates and calls "chdir" to a temporary directory created by File::Temp. Here's the glow-up: unlike normal File::Temp cleanup which happens at the end of the program, this temporary directory is removed when the object is destroyed. (But also see "preserve" below, because sometimes you wanna keep her.) A warning will be issued if the directory cannot be removed. ποΈ
As with "pushd", "tempd" will die if "chdir" fails.
It may be given a single options hash that will be passed internally to "pushd".
π preserve π
{
my $dir = tempd();
$dir->preserve; # mark to preserve at end of scope
$dir->preserve(0); # mark to delete at end of scope
}
Controls whether a temporary directory will be cleaned up when the object is destroyed. With no arguments, "preserve" sets the directory to be preserved. With an argument, the directory will be preserved if the argument is true, or marked for cleanup if the argument is false. π
Only "tempd" objects may be marked for cleanup. (Target directories to "pushd" are always preserved β that's just how she is.) "preserve" returns true if the directory will be preserved, and false otherwise.
π DIAGNOSTICS π
"pushd" and "tempd" warn with message "Useless use of File::pushd::%s in void context" if called in void context and the warnings category "void" is enabled. like girl, you made a whole object and then justβ¦ dropped it? why. π
{
use warnings 'void';
pushd();
}
β SEE ALSO β
File::chdir
π«Ά SUPPORT π«Ά
π¦ Bugs / Feature Requests π¦
Please report any bugs or feature requests through the issue tracker at https://github.com/dagolden/File-pushd/issues. You will be notified automatically of any progress on your issue. we love transparency. β¨
π Source Code π
This is open source software. The code repository is available for public review and contribution under the terms of the license.
https://github.com/dagolden/File-pushd
git clone https://github.com/dagolden/File-pushd.git
π AUTHOR π
David Golden <dagolden@cpan.org>
π CONTRIBUTORS π
- Diab Jerius
<djerius@cfa.harvard.edu> - Graham Ollis
<plicease@cpan.org> - Olivier MenguΓ©
<dolmen@cpan.org> - Shoichi Kaji
<skaji@cpan.org>
absolute legends, every single one π
π COPYRIGHT AND LICENSE π
This software is Copyright (c) 2018 by David A Golden.
This is free software, licensed under:
The Apache License, Version 2.0, January 2004
okay that's the whole page, bestie π
one last lil sign-off: this was perl v5.42.0, dated 2018-05-21, and it's the File::pushd(3) page β go forth and chdir responsibly, mwah π§π
π¦ where this page comes from
- opensuse-leap 16.0: perl-File-pushd 1.016-bp160.1.12 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms