自己写的browse.bat与perl写的url_handler.pl的比较

以前自己也写过Windows下自动打开多个浏览器测试某个URI,提高浏览器兼容性测试效率。

但是写的browse.bat文件还是最基础简陋的

@echo off
if '%1'=='-c' (
start /d "C:\Program Files\Google\Chrome\Application\" chrome.exe -new-tab %2
exit
)
if '%1'=='-f' (
start /d "C:\Program Files\Mozilla Firefox" firefox.exe -new-tab %2
exit
)
if '%1'=='-i' (
start /d "C:\Program Files\Internet Explorer" iexplore.exe %2
exit
)
if '%1'=='-d' (
rundll32 url.dll,FileProtocolHandler %2
exit
) if '%1'=='-a' (
call browse -c %2
call browse -f %2
call browse -i %2
) else (
explorer %1

今天恰巧看到cygwin安装目录下用perl写的url_handler.pl,感觉作者的思路挺有趣。

代码共赏析:C:\cygwin\bin\url_handler.pl

    if ($ENV{BROWSER}) {
push(@try, split(/:/, $ENV{BROWSER}));
} else { # set some defaults
push(@try, 'firefox -a firefox -remote openURL\(%s\)');
push(@try, 'mozilla -remote openURL\(%s\)');
push(@try, 'opera -remote openURL\(%s\)');
push(@try, 'galeon -n');
push(@try, 'lynx'); # prefer lynx over links as it can handle news:-urls
push(@try, qw('links2 -g' links w3m));
push(@try, 'kfmclient newTab'); # has no useful return-value on error
}

上面的亮点:

1.使用了@try这样一个思路,尝试打开.(没有理解perl的@try的含义?)

2.没有用很多的if-else而是用push放入一个数组(没有查阅过perl的push含义?)

#! /usr/bin/perl -w
# example of how to call an appropriate viewer
#
# URLs must start with a scheme and shell metas should be already quoted
# (tin doesn't recognize URLs without a scheme and it quotes the metas) use strict;
use warnings; (my $pname = $) =~ s#^.*/##;
die "Usage: $pname URL" if $#ARGV != 0; # version Number
my $version = "0.1.1"; my ($method, $url, $match, @try);
$method = $url = $ARGV[];
$method =~ s#^([^:]+):.*#$1#io; # shell escape
$url =~ s#([\&\;\`\'\\\"\|\*\?\~\<\>\^\(\)\[\]\{\}\$\010\013\020\011])#\\$1#g; if ($ENV{"BROWSER_".uc($method)}) {
push(@try, split(/:/, $ENV{"BROWSER_".uc($method)}));
} else {
if ($ENV{BROWSER}) {
push(@try, split(/:/, $ENV{BROWSER}));
} else { # set some defaults
push(@try, 'firefox -a firefox -remote openURL\(%s\)');
push(@try, 'mozilla -remote openURL\(%s\)');
push(@try, 'opera -remote openURL\(%s\)');
push(@try, 'galeon -n');
push(@try, 'lynx'); # prefer lynx over links as it can handle news:-urls
push(@try, qw('links2 -g' links w3m));
push(@try, 'kfmclient newTab'); # has no useful return-value on error
}
} for my $browser (@try) {
# ignore empty parts
next if ($browser =~ m/^$/o);
# expand %s if not preceded by odd number of %
$match = $browser =~ s/(?<!%)((?:%%)*)%s/$$url/og;
# expand %c if not preceded by odd number of %
$browser =~ s/(?<!%)((?:%%)*)%c/$:/og;
# reduce dubble %
$browser =~ s/%%/%/og;
# append URL if no %s expansion took place
$browser .= " ".$url if (!$match);
# leave loop if $browser was started successful
last if (system("$browser 2>/dev/null") == );
}
exit ; __END__ =head1 NAME url_handler.pl - Spawn appropriate viewer for a given URL =head1 SYNOPSIS B<url_handler.pl> I<URL> =head1 DESCRIPTION B<url_handler.pl> takes an URL as argument and spawns the first executable
viewer found in either B<$BROWSER_I<SCHEME>> or B<$BROWSER>. =head1 ENVIRONMENT =over =item B<$BROWSER_I<SCHEME>> The user's preferred utility to browse URLs of tye I<SCHEME>. May actually
consist of a sequence of colon-separated browser commands to be tried in
order until one succeeds. If a command part contains %s, the URL is
substituted there, otherwise the browser command is simply called with the
URL as its last argument. %% is replaced by a single percent sign (%), and
%c is replaced by a colon (:).
Examples: =over 4 =item $BROWSER_FTP="wget:ncftp" =item $BROWSER_GOPHER="lynx:links" =item $BROWSER_MAILTO="mutt:pine -url" =item $BROWSER_NEWS="lynx" =item $BROWSER_NNTP="lynx" =back Z<> =item B<$BROWSER> The user's preferred utility to browse URLs for which there is no special
viewer defined via B<$BROWSER_I<SCHEME>>. Again it may actually consist of a
sequence of colon-separated browser commands to be tried in order until one
succeeds. If a command part contains %s, the URL is substituted there,
otherwise the browser command is simply called with the URL as its last
argument. %% is replaced by a single percent sign (%), and %c is replaced
by a colon (:).
Examples: =over =item $BROWSER="firefox -a firefox -remote openURL\(%s\):opera:konqueror:links2 -g:lynx:w3m" =back =head1 SECURITY B<url_handler.pl> was designed to work together with B<tin>() which only
issues shell escaped absolute URLs thus B<url_handler.pl> does not try hard
to shell escape its input nor does it convert relative URLs into absolute
ones! If you use B<url_handler.pl> from other applications be sure to at
least shell escaped its input! =head1 AUTHOR Urs Janssen E<lt>urs@tin.orgE<gt> =head1 SEE ALSO http://www.catb.org/~esr/BROWSER/
http://www.dwheeler.com/browse/secure_browser.html =cut
上一篇:ie6常见的兼容性问题


下一篇:ie6对hover兼容性问题的解决: