A comparison of the url_handler.pl written by Browser.bat and Perl

Source: Internet
Author: User

Previously, I have also written that Windows automatically opens multiple browsers to test a URI to improve the efficiency of your browser compatibility test.

But the Browser.bat file is still the most basic, and today happens to see Cygwin installation directory in Perl written url_handler.pl, feel really small.

Total Code Appreciation: 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');#Have no useful return-value On error}

Highlights of the above:

1. Use a @try such a thought, try to open. (does not understand the meaning of Perl's @try?)

2. Instead of using a lot of if-else, use push to put an array (without checking the meaning of the Perl push?)

#!/usr/bin/perl-w# example of how to call an appropriate viewer## URLs must start with a scheme and shell Metas Shoul D be already quoted# (tin doesn ' t recognize URLs without a scheme and it quotes the Metas) UseStrict; Usewarnings; (my $pname= $0) =~ S#^.*/##; die "Usage: $pname URL" if$#ARGV! = 0;# version numbermy $version="0.1.1";my($method,$url,$match,@try);$method=$url=$ARGV[0];$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');#Have 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/$1$url/og; #expand%c if not preceded by odd number of%    $browser=~ s/(? <!%) ((?:%%)*)%c/$1:/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") ==0);}Exit 0;__end__=head1 Nameurl_handler. Pl-spawn Appropriate Viewer fora given URL=head1 SYNOPSISB<url_handler.pl> i<url>=head1 descriptionb<url_handler.pl>takes a URL as argument and spawns the first executableviewer found in either B<$BROWSER _i<SCHEME>> or b<$BROWSER>.=HEAD1 Environment=over4=item b<$BROWSER _i<SCHEME>>The user'S preferred utility to browse URLs of Tye I<scheme>. May actuallyconsist of a sequence of colon-separated browser commands to be tried inorder until one succeeds. If a command part contains%s, the URL issubstituted there, otherwise the browser command was simply called with theURL as Its last argument. Percent is replaced by a single percent sign (%), and%c are 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 "=backz<>=item b< $BROWSER >the user'S preferred utility to browse URLs forWhich there isNoSpecialviewerdefinedVia b<$BROWSER _i<scheme>>.Again It may actually consist of asequence of colon-separated browser commands to being tried in orderuntilonesucceeds.IfA command part contains%s, the URL is substituted there,otherwise the browser command is simply called and the URL as its Lastargument. Percent are replaced by a single percent sign (%), and%cis replacedby a colon (:).Examples: =over2=item$BROWSER="firefox-a firefox-remote openurl\ (%s\): opera:konqueror:links2-g:lynx:w3m"= Back=head1 securityb<url_handler.pl> was designed to work together with b<tin> (1) which onlyissues Shell escaped absolute URLs thus B<url_handler.pl>does not try hardto shell escape it input nor does it convert relative URLs into Absoluteones!IfYou UseB<url_handler.pl>From the other applications is sure to atleast shell escaped its input!=head1 authorurs Janssen E<lt>urs@tin. orge<gt>=head1 See Alsohttp://www.catb.org/~esr/browser/http://www.dwheeler.com/browse/secure_browser.HTML=cut

A comparison of the url_handler.pl written by Browser.bat and Perl

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.