Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warnings when no handler found #49

Closed
Smylers opened this issue Jun 9, 2022 · 1 comment
Closed

Warnings when no handler found #49

Smylers opened this issue Jun 9, 2022 · 1 comment

Comments

@Smylers
Copy link

Smylers commented Jun 9, 2022

Invoking mime_applications on a mime type for which no applications are found yields a bunch of warnings to the terminal:

$ perl -MFile::MimeInfo::Applications  -wE "mime_applications q[application/octet-stream]"
Use of uninitialized value in subroutine entry at /usr/share/perl5/File/BaseDir.pm line 117.
Use of uninitialized value in subroutine entry at /usr/share/perl5/File/BaseDir.pm line 117.
Use of uninitialized value in subroutine entry at /usr/share/perl5/File/BaseDir.pm line 117.
Use of uninitialized value in subroutine entry at /usr/share/perl5/File/BaseDir.pm line 117.
Use of uninitialized value in subroutine entry at /usr/share/perl5/File/BaseDir.pm line 117.
Use of uninitialized value in subroutine entry at /usr/share/perl5/File/BaseDir.pm line 117.

(If you have a handler for application/octed-stream, I think just making up a Mime type would demonstrate the issue.)

I think the cause is in _find_file(). _default() invokes it like this:

my @list = _read_list(@paths);
my $desktop_file = _find_file(reverse @list);

If no handlers are specified, _read_list() returns an empty list, so inside _find_file(), @_ is empty. But the start of _find_file() is:

sub _find_file {
    my @list = shift;
    for (@list) {

shift returns a scalar value, so when @_ is empty, @list gets set to (undef). The for loop then iterates over that undef, and warnings ensure.

That for loop is also odd when @_ does contain multiple values: the first one gets shifted into @list and is processed by the loop — but either way the loop runs precisely once, making the looping part unnecessary.

I'm wondering if it should be:

sub _find_file {
    my @list = @_;
    for (@list) {

That would avoid looping at all when @_ is empty, and would also mean that when it isn't empty would loop over all its items, not just the first.

If it's desirable only to ‘loop’ over the first then maybe putting return undef if !@_ at the very top of the sub would do that and avoid the warnings.

Thanks.

@mbeijen
Copy link
Owner

mbeijen commented Jul 13, 2022

Hi, thanks for the report! And you're right....

yesterday I merged 8519aee which does exactly as you proposed, and released it to cpan as version 0.33.

@mbeijen mbeijen closed this as completed Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants