Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions lib/File/stat.pm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package File::stat 1.14;
package File::stat 1.15;
use v5.38;

use warnings::register;
Expand Down Expand Up @@ -200,14 +200,16 @@ sub stat :prototype($) {
my $arg = shift;
my $st = populate(CORE::stat $arg);
return $st if defined $st;
my $fh;
return if ref $arg;
# ... maybe $arg is the name of a bareword handle?
my $fh;
{
local $!;
no strict 'refs';
require Symbol;
$fh = \*{ Symbol::qualify( $arg, caller() )};
return unless defined fileno $fh;
}
local $!;
no strict 'refs';
require Symbol;
$fh = \*{ Symbol::qualify( $arg, caller() )};
return unless defined fileno $fh;
}
return populate(CORE::stat $fh);
}

Expand Down
34 changes: 34 additions & 0 deletions lib/File/stat.t
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,40 @@ SKIP:
ok(-p($pstat), "check -p detects a pipe");
}

{
# GH #23507
{
package PathObj;
use overload
'""' => sub { $_[0]->to_string },
fallback => 1;

sub new {
my $class = shift;
bless { path => $_[0] }, $class
}

sub to_string {
my $self = shift;
$self->{path}
}
}

my $good_path = PathObj->new($file);
my $bad_path = PathObj->new('/ no such file');

# explicit stringification
isa_ok stat("$good_path"), 'File::stat', 'stat("$good_path")';
is stat("$bad_path"), undef, 'stat("$bad_path") fails by returning undef';
# implicit stringification
isa_ok stat($good_path), 'File::stat', 'stat($good_path)';
is stat($bad_path), undef, 'stat($bad_path) fails by returning undef';
# and for good measure, unblessed references
is stat(\42), undef, 'stat(\42) fails by returning undef';
is stat([]), undef, 'stat([]) fails by returning undef';
is stat({}), undef, 'stat({}) fails by returning undef';
}

# Testing pretty much anything else is unportable.

done_testing;
Expand Down
Loading