File Coverage

blib/lib/Desktop/Open.pm
Criterion Covered Total %
statement 12 37 32.4
branch 0 16 0.0
condition 0 11 0.0
subroutine 4 5 80.0
pod 1 1 100.0
total 17 70 24.2


line stmt bran cond sub pod time code
1             package Desktop::Open;
2              
3 1     1   261857 use strict;
  1         1  
  1         40  
4 1     1   5 use warnings;
  1         2  
  1         98  
5 1     1   1478 use Log::ger;
  1         41  
  1         4  
6              
7 1     1   175 use Exporter qw(import);
  1         2  
  1         513  
8              
9             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10             our $DATE = '2023-10-21'; # DATE
11             our $DIST = 'Desktop-Open'; # DIST
12             our $VERSION = '0.004'; # VERSION
13              
14             our @EXPORT_OK = qw(open_desktop);
15              
16             sub open_desktop {
17 0     0 1   my $path_or_url = shift;
18              
19 0           my $res;
20             OPEN: {
21 0 0         if (defined $ENV{PERL_DESKTOP_OPEN_PROGRAM}) {
  0            
22 0           system $ENV{PERL_DESKTOP_OPEN_PROGRAM}, $path_or_url;
23 0           $res = $?;
24 0           last;
25             }
26              
27 0 0 0       goto BROWSER if ($ENV{PERL_DESKTOP_OPEN_USE_BROWSER} || 0) == 1;
28              
29 0 0         if ($^O eq 'MSWin32') {
30 0           system "start", $path_or_url;
31 0           $res = $?;
32 0 0         last if $res == 0;
33             } else {
34 0           require File::Which;
35 0 0         if (File::Which::which("xdg-open")) {
36 0           system "xdg-open", $path_or_url;
37 0           $res = $?;
38 0 0         my $exit_code = $? < 0 ? $? : $? >> 8;
39 0 0 0       last if $exit_code == 0 || $exit_code == 2; # 2 = file not found
40             # my xdg-open returns 4 instead of 2 when file is not found, so
41             # we test ourselves
42 0 0 0       if ($exit_code == 4 && $path_or_url !~ m!\A\w+://! && !(-e $path_or_url)) {
      0        
43 0           log_trace "We changed xdg-open's exit code 4 to 2 since path $path_or_url does not exist";
44 0           $res = 2 << 8;
45 0           last;
46             }
47             }
48             }
49              
50 0           BROWSER:
51             require Browser::Open;
52 0           $res = Browser::Open::open_browser($path_or_url);
53             }
54 0           $res;
55             }
56              
57             1;
58             # ABSTRACT: Open a file or URL in the user's preferred application
59              
60             __END__