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 πŸŽ€

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