File Coverage

blib/lib/Browser/Open.pm
Criterion Covered Total %
statement 33 39 84.6
branch 11 22 50.0
condition 9 15 60.0
subroutine 9 10 90.0
pod 3 3 100.0
total 65 89 73.0


line stmt bran cond sub pod time code
1             package Browser::Open;
2             our $VERSION = '0.04';
3              
4              
5              
6 2     2   50474 use strict;
  2         6  
  2         71  
7 2     2   10 use warnings;
  2         5  
  2         56  
8 2     2   11 use Carp;
  2         7  
  2         191  
9 2     2   1908 use File::Spec::Functions qw( catfile );
  2         1836  
  2         139  
10              
11 2     2   2021 use parent 'Exporter';
  2         769  
  2         13  
12              
13             @Browser::Open::EXPORT_OK = qw(
14             open_browser
15             open_browser_cmd
16             open_browser_cmd_all
17             );
18              
19             my @known_commands = (
20               ['', $ENV{BROWSER}],
21               ['darwin', '/usr/bin/open', 1],
22               ['cygwin', 'start'],
23               ['MSWin32', 'start', undef, 1],
24               ['solaris', 'xdg-open'],
25               ['solaris', 'firefox'],
26               ['linux', 'sensible-browser'],
27               ['linux', 'xdg-open'],
28               ['linux', 'x-www-browser'],
29               ['linux', 'www-browser'],
30               ['linux', 'htmlview'],
31               ['linux', 'gnome-open'],
32               ['linux', 'gnome-moz-remote'],
33               ['linux', 'kfmclient'],
34               ['linux', 'exo-open'],
35               ['linux', 'firefox'],
36               ['linux', 'seamonkey'],
37               ['linux', 'opera'],
38               ['linux', 'mozilla'],
39               ['linux', 'iceweasel'],
40               ['linux', 'netscape'],
41               ['linux', 'galeon'],
42               ['linux', 'opera'],
43               ['linux', 'w3m'],
44               ['linux', 'lynx'],
45               ['freebsd', 'xdg-open'],
46               ['freebsd', 'gnome-open'],
47               ['freebsd', 'gnome-moz-remote'],
48               ['freebsd', 'kfmclient'],
49               ['freebsd', 'exo-open'],
50               ['freebsd', 'firefox'],
51               ['freebsd', 'seamonkey'],
52               ['freebsd', 'opera'],
53               ['freebsd', 'mozilla'],
54               ['freebsd', 'netscape'],
55               ['freebsd', 'galeon'],
56               ['freebsd', 'opera'],
57               ['freebsd', 'w3m'],
58               ['freebsd', 'lynx'],
59               ['', 'open'],
60               ['', 'start'],
61             );
62              
63             ##################################
64              
65             sub open_browser {
66 0     0 1 0   my ($url, $all) = @_;
67 0 0       0   croak('Missing required parameter $url, ') unless $url;
68              
69 0 0       0   my $cmd = $all ? open_browser_cmd_all() : open_browser_cmd();
70 0 0       0   return unless $cmd;
71              
72 0         0   return system($cmd, $url);
73             }
74              
75             sub open_browser_cmd {
76 1     1 1 8   return _check_all_cmds($^O);
77             }
78              
79             sub open_browser_cmd_all {
80 1     1 1 740   return _check_all_cmds('');
81             }
82              
83              
84             ##################################
85              
86             sub _check_all_cmds {
87 2     2   6   my ($filter) = @_;
88              
89 2         4   foreach my $spec (@known_commands) {
90 10         19     my ($osname, $cmd, $exact, $no_search) = @$spec;
91 10 100       16     next unless $cmd;
92 8 100 66     42     next if $osname && $filter && $osname ne $filter;
      100        
93 3 0 33     7     next if $no_search && !$filter && $osname ne $^O;
      33        
94              
95 3 50 66     22     return $cmd if $exact && -x $cmd;
96 3 50       6     return $cmd if $no_search;
97 3         5     $cmd = _search_in_path($cmd);
98 3 100       13     return $cmd if $cmd;
99               }
100 0         0   return;
101             }
102              
103             sub _search_in_path {
104 3     3   3   my $cmd = shift;
105              
106 3         13   for my $path (split(/:/, $ENV{PATH})) {
107 16 50       27     next unless $path;
108 16         49     my $file = catfile($path, $cmd);
109 16 100       256     return $file if -x $file;
110               }
111 1         3   return;
112             }
113              
114              
115             1;
116             __END__
117            
118             =head1 NAME
119            
120             Browser::Open - open a browser in a given URL
121            
122            
123             =head1 VERSION
124            
125             version 0.03
126            
127             =head1 SYNOPSIS
128            
129             use Browser::Open qw( open_browser );
130            
131             ### Try commands specific to the current Operating System
132             my $ok = open_browser($url);
133             # ! defined($ok): no recognized command found
134             # $ok == 0: command found and executed
135             # $ok != 0: command found, error while executing
136            
137             ### Try all known commands
138             my $ok = open_browser($url, 1);
139            
140            
141             =head1 DESCRIPTION
142            
143             The functions optionaly exported by this module allows you to open URLs
144             in the user browser.
145            
146             A set of known commands per OS-name is tested for presence, and the
147             first one found is executed. With an optional parameter, all known
148             commands are checked.
149            
150             The L<"open_browser"> uses the C<system()> function to execute the
151             command. If you want more control, you can get the command with the
152             L<"open_browser_cmd"> or L<"open_browser_cmd_all"> functions and then
153             use whatever method you want to execute it.
154            
155            
156             =head1 API
157            
158             All functions are B<not> exported by default. You must ask for them
159             explicitly.
160            
161            
162             =head2 open_browser
163            
164             my $ok = open_browser($url, $all);
165            
166             Find an appropriate command and executes it with your C<$url>. If
167             C<$all> is false, the default, only commands that match the current OS
168             will be tested. If true, all known commands will be tested.
169            
170             If no command was found, returns C<undef>.
171            
172             If a command is found, returns the exit code of the execution attempt, 0
173             for success. See the C<system()> for more information about this
174             exit code.
175            
176             If no C<$url> is given, an exception will be thrown:
177             C<< Missing required parameter $url >>.
178            
179            
180             =head2 open_browser_cmd
181            
182             my $cmd = open_browser_cmd();
183            
184             Returns the best command found to open a URL on your system.
185            
186             If no command was found, returns C<undef>.
187            
188            
189             =head2 open_browser_cmd_all
190            
191             my $cmd = open_browser_cmd_all();
192            
193             Returns the first command found to open a URL.
194            
195             If no command was found, returns C<undef>.
196            
197            
198             =head1 AUTHOR
199            
200             Pedro Melo, C<< <melo at cpan.org> >>
201            
202            
203             =head1 COPYRIGHT & LICENSE
204            
205             Copyright 2009 Pedro Melo.
206            
207             This program is free software; you can redistribute it and/or modify it
208             under the same terms as Perl itself.
209            
210             =cut