IO::Fi...lename(3pm)

πŸ’– IO::File::WithFilename β€” from the IO::File::WithFilename page in the User Contributed Perl Documentation ✨

Okay bestie, sit down, because this one is short but it's so useful. 🫢


🌸 NAME 🌸

IO::File::WithFilename β€” filehandles that know their origin πŸ’…

Like. The filehandle literally remembers where it came from. Main character energy fr fr.


✨ SYNOPSIS ✨

use IO::File::WithFilename;

my $fh = IO::File::WithFilename->new('../movies/kin-dza-dza.ogg', O_RDONLY);
print $fh->filename, "\n";
print "$fh\n"; # same as above

Notice the second print? You can just stringify the whole object and it gives you the same thing. No extra work. We love a low-effort slay. πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

Okay so here's the tea: this module does everything that IO::File does β€” every single thing, no cap β€” plus it implements the filename method, the one that File::Temp objects already have.

Why do we care? Because it lets you write code that is ignorant of what classes of objects it's working with. ✨ Your code doesn't need to know or care whether it got handed an IO::File, a File::Temp, or this little icon right here β€” it just asks for the filename and moves on. Obsessed, honestly.

If you want to check whether it's safe to call the filename method, the recommendation is to call the can method rather than to go poking around at an object's inheritance. πŸ™ˆ Do it like this:

print $obj->filename, "\n" if eval { $obj->can('filename') };

That's the vibe. can first, inheritance-snooping never. πŸŽ€

🧚 EXPORT 🧚

Same as IO::File, i. e. the O_XXX constants from the Fcntl module β€” if this module is available. (If it's not available, well, no constants for you, bestie. 😭)


πŸ‘‘ SEE ALSO πŸ‘‘

  • IO::File
  • File::Temp

Go read those too, they're the whole reason this module gets to exist. 🌈


⭐ AUTHOR ⭐

Ivan Fomichev, <ifomichev@cpan.org>


Copyright (C) 2010 by Ivan Fomichev

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.1 or, at your option, any later version of Perl 5 you may have available.


Real talk though: that license line is not a suggestion, it's the whole deal. Free software, same terms as Perl, version 5.10.1 or any later Perl 5 you've got lying around. Respect it and it respects you. 🫢

And that's a wrap!! This page came from perl v5.36.0, dated 2023-02-05, straight out of the User Contributed Perl Documentation. Go forth and know your filenames. πŸ’–πŸŒΈβœ¨

πŸ“¦ where this page comes from