MooseX...h::Tiny(3pm)

πŸ“¦
version 0.012 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– MooseX::Types::Path::Tiny πŸ’–

okay bestie, gather round, because this is the MooseX::Types::Path::Tiny manual page (that's the 3pm one, section 3 for perl modules, iykyk) and it is genuinely so useful. no cap. let's get into it ✨


✨ VERSION ✨

version 0.012 β€” that's the one we're talking about, periodt.


🌸 SYNOPSIS 🌸

here's the whole vibe in code, bestie:

  ### specification of type constraint with coercion

  package Foo;

  use Moose;
  use MooseX::Types::Path::Tiny qw/Path Paths AbsPath/;

  has filename => (
    is => 'ro',
    isa => Path,
    coerce => 1,
  );

  has directory => (
    is => 'ro',
    isa => AbsPath,
    coerce => 1,
  );

  has filenames => (
    is => 'ro',
    isa => Paths,
    coerce => 1,
  );

  ### usage in code

  Foo->new( filename => 'foo.txt' ); # coerced to Path::Tiny
  Foo->new( directory => '.' ); # coerced to path('.')->absolute
  Foo->new( filenames => [qw/bar.txt baz.txt/] ); # coerced to ArrayRef[Path::Tiny]

like… the attribute declaration is one thing, and then you just use it and the coercion does its little magic behind the scenes. main character behaviour tbh πŸ‘‘


πŸ¦‹ DESCRIPTION πŸ¦‹

so this module hands you Path::Tiny types for Moose. it's giving "types, but make them path-aware" πŸ’…

it handles two really important flavours of coercion:

  • coercing objects that have overloaded stringification (so anything that knows how to stringify itself can become a path)
  • coercing things into absolute paths

and it can also check that files or directories actually exist, because sometimes you truly do need that 🫢


πŸŽ€ SUBTYPES πŸŽ€

this module uses MooseX::Types to define the following subtypes, and here they all are, no skips:

Path

Path makes sure an attribute is a Path::Tiny object. strings and objects with overloaded stringification are allowed to be coerced. slay ✨

AbsPath

AbsPath is a subtype of Path (the one right above, keep up bestie 😭), except it coerces to an absolute path.

File, AbsFile

these are literally just like Path and AbsPath, except they run -f to make sure the file actually exists on the filesystem. vibe check, but for files πŸ“

Dir, AbsDir

same deal as Path and AbsPath, except these check -d so the directory genuinely exists on the filesystem. bestie said "prove it" 🧚

Paths, AbsPaths

these are arrayrefs of Path and AbsPath, and yes they include coercions from arrayrefs of strings. so you can just yeet an arrayref of plain strings at them and they'll sort themselves out πŸ’œ


πŸ’€ CAVEATS πŸ’€

listen, we need to talk. these are the parts where things get real.

Path vs File vs Dir

Path only guarantees you have a Path::Tiny object. that's it. it's not checking anything.

File and Dir do check the filesystem. so like… don't reach for them unless that is really and truly what you want, because they're going to hit the disk and they mean it.

Usage with File::Temp

⚠️ be careful if you pass in a File::Temp object. this one matters.

because the argument gets stringified during coercion into a Path::Tiny object, no reference to the original File::Temp argument is held. so you need to hold an external reference to it yourself, otherwise the temporary file or directory gets cleaned up immediately at the end of the enclosing scope. and then you're sad 😭

a better approach is to use Path::Tiny's own tempfile or tempdir constructors, because those hold the reference for you. iconic of them, honestly πŸ’…

  Foo->new( filename => Path::Tiny->tempfile );

⭐ SEE ALSO ⭐

go read these too, they're part of the lore:

  • Path::Tiny
  • Moose::Manual::Types
  • Types::Path::Tiny

🫢 SUPPORT 🫢

got bugs? bestie, submit them through the RT bug tracker: https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Types-Path-Tiny (or email bug-MooseX-Types-Path-Tiny@rt.cpan.org). they'll take care of you πŸ’–

there's also a mailing list for users of this distribution, right here: http://lists.perl.org/list/moose.html

and there's an irc channel for users of this distribution too: #moose on irc.perl.org (that's irc://irc.perl.org/#moose) 🌈

oh and the author is also usually hanging around on irc as 'ether' at irc.perl.org πŸ‘‘


πŸ‘‘ AUTHOR πŸ‘‘

David Golden <dagolden@cpan.org>


πŸ’– CONTRIBUTORS πŸ’–

look at this lineup, absolute legends:

  • Karen Etheridge <ether@cpan.org>
  • Toby Inkster <mail@tobyinkster.co.uk>
  • Demian Riccardi <dde@ornl.gov>
  • Gregory Oschwald <goschwald@maxmind.com>

this software is Copyright (c) 2013 by David Golden.

it's free software, licensed under:

  The Apache License, Version 2.0, January 2004

and that's the whole page, bestie πŸ’… this was generated by perl v5.34.0, dated 2022-06-16, for the MooseX::Types::Path::Tiny page in section 3pm. go forth and coerce your paths, you absolute icon πŸŽ€βœ¨πŸ¦‹