Accepting a pattern as an argument 
Author Message
 Accepting a pattern as an argument

We're not using 5.5 so I don't know quite what 'qr' does, but I need to
have a program accept a pattern on its command line [like 'grep'].  Here's

Quote:

} use vars qw($delim) ;
} for ($delim = 0 ; $delim < 256 ; $delim += 1)
} {
}     my $ch = chr($delim) ;
}     next if $ch =~ /[\w\s({<[]/ ;
}     last unless $pattern =~ /$ch/ ;
} }
}
} die "Can't find a pattern delimter!!\n" if $delim == 256 ;
} $delim = chr($delim) ;

And then I build a little anonymous subroutine [it is a bit more
complicated than this, but roughly:

  $matchsub = eval "sub { return \$_[0] =~ ".$delim.$pattern.$delim." ; }";

and then I just use $matchsub->(<string>) in my loop elsewhere...

This works just fine, so this is not a plea for help, but just for
education: I was just curious if I was overlooking some more elegant/clever
way to handle the pattern [and doing the matches].  speed isn't too much of
an issue [the loop with the [up to] 256 'chr' and such is essentially not
an issue in the larger scheme of things, but the _pattern_match_ is, so if
there's a faster/better way to do up the pattern match...

Thanks!

  /Bernie\
--
Bernie Cosell                     Fantasy Farm Fibers

    -->  Too many people, too few sheep  <--          



Mon, 27 Jan 2003 03:00:00 GMT  
 Accepting a pattern as an argument

Quote:

> We're not using 5.5 so I don't know quite what 'qr' does, but I need to
> have a program accept a pattern on its command line [like 'grep'].  

> } use vars qw($delim) ;
> } for ($delim = 0 ; $delim < 256 ; $delim += 1)
> } {
> }     my $ch = chr($delim) ;
> }     next if $ch =~ /[\w\s({<[]/ ;
> }     last unless $pattern =~ /$ch/ ;
> } }
> }
> } die "Can't find a pattern delimter!!\n" if $delim == 256 ;
> } $delim = chr($delim) ;

% perl -wle '$b="/x"; print "yes" if "x/xx" =~ m/$b/'
yes

Embedding a delimiter in a scalar var., which is interpolated as the pattern in
a match, is not a problem. Is that what you were worried about, or did I miss
the point?

--




Mon, 27 Jan 2003 03:00:00 GMT  
 Accepting a pattern as an argument

Quote:


} >
} > We're not using 5.5 so I don't know quite what 'qr' does, but I need to
} > have a program accept a pattern on its command line [like 'grep'].  

} >
} > } use vars qw($delim) ;
} > } for ($delim = 0 ; $delim < 256 ; $delim += 1)
  [...]
}
} % perl -wle '$b="/x"; print "yes" if "x/xx" =~ m/$b/'
} yes
}
} Embedding a delimiter in a scalar var., which is interpolated as the pattern in
} a match, is not a problem. Is that what you were worried about, or did I miss
} the point?

Yes, exactly the point.  My problem is that I got all worried about
handling the eval to make the anon subroutine.  I don't *think* it'll work
to do:
   eval "sub { return $_[0] =~ m/".$pattern."/ ; }" ;

if $pattern happens to have a '/' in it.... BUT: you've given me the key
hint for doing what I need just right:

   eval "sub { return $_[0] =~ m/\$pattern/o ; }" ;

the 'o' will get the interpolation and compilation of the RE to happen at
'eval' time, and then at actual 'run time' the subroutine will be just as
fast as the version that I worked so foolishly hard at to get the RE
'inline'...

Thanks!!
  /Bernie\
--
Bernie Cosell                     Fantasy Farm Fibers

    -->  Too many people, too few sheep  <--          



Mon, 27 Jan 2003 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Hang up when using D1 + ODBC + MS SQL Server 6.5

2. keys() doesn't accept referrenced hash as argument

3. PERLFUNC: accept - accept an incoming socket connect

4. PERLFUNC: accept - accept an incoming socket connect

5. socket accept() vs. CGI.pm accept()

6. Pattern Match Arguments in Variable

7. Auto Increment...

8. Associative arguments as arguments in Perl 5.002

9. Can "undef" accept mutliple arguments?

10. how to get Getopt::Long to accept "+integer" argument, eg "-foo +1"?

11. Better way to accept an env var as command line argument?

12. Rownumber in a DBGRid?

 

 
Powered by phpBB® Forum Software