regex on the command line

2004-09-08 Thread Errin Larsen
Hi Perl-Buddies,

I'm wondering how to get a regex into my code at run-time.  I mean in
a command line like this:

# my_perl_program "foo(\w|and|or)+bar" foobar.txt

and in my code I want to be able to search the foobar.txt file with
the regex found in the quotes.  So if I assign the above to a
variable:

my $regex = shift;

How do I use the regex in $regex in a m// oeration?
Do I just throw it in there?

/$regex/

That seems to work, but is it doing what I think it's doing?  I may be
over-thinking myself here, but it just seems too easy!

--Errin Larsen

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: regex on the command line

2004-09-08 Thread Bob Showalter
Errin Larsen wrote:
> Hi Perl-Buddies,
> 
> I'm wondering how to get a regex into my code at run-time.  I mean in
> a command line like this:
> 
> # my_perl_program "foo(\w|and|or)+bar" foobar.txt
> 
> and in my code I want to be able to search the foobar.txt file with
> the regex found in the quotes.  So if I assign the above to a
> variable:
> 
> my $regex = shift;
> 
> How do I use the regex in $regex in a m// oeration?
> Do I just throw it in there?
> 
> /$regex/
> 
> That seems to work, but is it doing what I think it's doing?  I may be
> over-thinking myself here, but it just seems too easy!

Yes, that's fine. Since $regex doesn't change once it's assigned, you should
use the /o option.

   /$regex/o

Do 'perldoc perlop' and search for the string "poor man's grep" for an
example.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]