home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / vile-src.zip / vile-8.1 / perl / glob2re.pl next >
Perl Script  |  1998-04-04  |  711b  |  39 lines

  1. #!/usr/bin/perl -w 
  2.  
  3. sub glob2re { 
  4.     my ($glob) = @_; 
  5.     my $re = '(^|\/)'; 
  6.  
  7.     while ($glob ne '') { 
  8.     if ($glob =~ /^\*(.*)$/) { 
  9.         $re .= '.*'; 
  10.         $glob = $1; 
  11.     } 
  12.     elsif ($glob =~ /^\?(.*)$/) { 
  13.         $re .= '.'; 
  14.         $glob = $1; 
  15.     } 
  16.     elsif ($glob =~ /^\[(.+?)\](.*)$/) { 
  17.         $re .= "[$1]"; 
  18.         $glob= $2; 
  19.     } 
  20.     elsif ($glob =~ /^\{(.*?,.*?)\}(.*)$/) { 
  21.         my ($alts) = $1; 
  22.         $glob = $2; 
  23.         $re .= '(' . join('|',map(quotemeta,split(/,/, $alts))) . ')'; 
  24.     } 
  25.     elsif ($glob =~ /^(.[^[{*?]*)(.*)$/) { 
  26.         $re .= quotemeta($1); 
  27.         $glob = $2; 
  28.     } 
  29.     else { 
  30.         # shouldn't get here.  If we do, give up 
  31.         $glob = ''; 
  32.     } 
  33.     } 
  34.     $re .= '$'; 
  35.     return $re; 
  36.  
  37. 1; 
  38.