diff --git a/lib/Path/Tiny.pm b/lib/Path/Tiny.pm index 58862e4..1371973 100644 --- a/lib/Path/Tiny.pm +++ b/lib/Path/Tiny.pm @@ -1267,6 +1267,23 @@ sub is_rootdir { return $self->[DIR] eq '/' && $self->[FILE] eq ''; } +=method is_temporary + + my $temp = Path::Tiny->tempfile; + if ($temp->is_temporary) { + ... + } + +Returns true if the C object was created by C or +C. Returns false otherwise. + +=cut + +sub is_temporary { + my ($self) = @_; + !!eval { $self->cached_temp; 1 }; +} + =method iterator $iter = path("/tmp")->iterator( \%options ); diff --git a/t/temp.t b/t/temp.t index 59c839a..01f25cb 100644 --- a/t/temp.t +++ b/t/temp.t @@ -145,5 +145,14 @@ subtest "tempfile, instance method, overridden DIR" => sub { ok( $tempfile->parent ne $bd ), "DIR is overridden"; }; +subtest "is_temporary" => sub { + my $tempdir = Path::Tiny->tempdir; + ok( $tempdir->is_temporary, "tempdir is temporary" ); + my $tempfile = Path::Tiny->tempfile; + ok( $tempfile->is_temporary, "tempfile is temporary" ); + my $path = path("abcdefg"); + ok( !$path->is_temporary, "regular path is not temporary" ); +}; + done_testing; # COPYRIGHT