data comprised of regexs (while loop weirdness)

J

jason

Sure this is a newbie issue. And please pardon any reposting I might
have done.


My simple example doesn't work and there are few quirks I'm trying to
understand about perl.

for the below code,

1. why do I only see a yes when passing the last data entry (test3) as
an argument?
2. Why do results vary depending on whether $_ is on the left or
right of the if compare with $request?
3. why does only my last entry display if I uncomment the chomp

My real objective is to read a text file with a list of regular
expression in it that I can test against user input (that file may
have 200 expressons). If none of the expression find a match my user
input, then I would reject the input with a message. Any help or
information is appreciated.

<code snipet>

#!/usr/bin/perl -w
use strict;
my $request = shift @ARGV;
my @data = <DATA>;
for(@data)
#chomp;
{
print $_;
if ($request =~ $_) {print 'yes';}
#if ($_ =~ $request) {print 'yes';}
#if ($request =~ /$_/) {print 'yes';} #what i really want to do is
test for regex match coming in from an a flat file.

}

__DATA__
test1
test2
test3

<end code snipet>


testing:

perl m3.pl test3
test1
test2
test3yes$

perl m3.pl test2
test1
test2
test3$
 
S

Scott W Gifford

Sure this is a newbie issue. And please pardon any reposting I might
have done.
[...]

1. why do I only see a yes when passing the last data entry (test3) as
an argument?

Not sure; as written, your code didn't work for me at all.
2. Why do results vary depending on whether $_ is on the left or
right of the if compare with $request?

If it's on the left, it's the string being tested; if it's on the
right, it's the pattern being tested against.
3. why does only my last entry display if I uncomment the chomp

I'm not sure why it works at all without the chomp.

With some slight changes it worked OK for me, though. Actually I
pretty much uncommented the chomp and moved it inside the loop and it
worked:

#!/usr/bin/perl -w

use strict;

my $request = shift @ARGV;

my @data = <DATA>;
for(@data)
{
chomp;
print "'$request' ?~ /$_/: ";
if ($request =~ /$_/) {print 'yes';}
print "\n";
}
__DATA__
test1
test2
test3

Produces:

[gifford@gifford gifford]$ perl /tmp/t2 test
'test' ?~ /test1/:
'test' ?~ /test2/:
'test' ?~ /test3/:
[gifford@gifford gifford]$ perl /tmp/t2 test1
'test1' ?~ /test1/: yes
'test1' ?~ /test2/:
'test1' ?~ /test3/:
[gifford@gifford gifford]$ perl /tmp/t2 test2
'test2' ?~ /test1/:
'test2' ?~ /test2/: yes
'test2' ?~ /test3/:
[gifford@gifford gifford]$ perl /tmp/t2 test3
'test3' ?~ /test1/:
'test3' ?~ /test2/:
'test3' ?~ /test3/: yes

I assume that the contents of the file you're reading from is under
your control, BTW; regular expressions can contain code, so if
somebody can write to that file they'll have the ability to cause your
script to execute code of their choice.

----ScottG.
 
G

Gunnar Hjalmarsson

And please pardon any reposting I might have done.

It's fine that you move the discussion from the defunct group
comp.lang.perl, but it would have been suitable to state that fact.
My simple example doesn't work and there are few quirks I'm trying
to understand about perl.

for the below code,

1. why do I only see a yes when passing the last data entry (test3)
as an argument?

Because your script file ends with "test3" without a trailing newline.
2. Why do results vary depending on whether $_ is on the left or
right of the if compare with $request?

Because it searches the string to the left for the pattern to the
right. Please read about the m// operator in "perldoc perlop".
3. why does only my last entry display if I uncomment the chomp

A correctly placed chomp makes the code print 'yes' for any of
'temp1', 'temp2' or 'temp3'.
my $request = shift @ARGV;
my @data = <DATA>;
for(@data)
#chomp;
{

chomp;
 
G

Gunnar Hjalmarsson

Gunnar said:
A correctly placed chomp makes the code print 'yes' for any of
'temp1', 'temp2' or 'temp3'.


chomp;

Did you see Joe's and Jim's theories in comp.lang.perl? Can it
possibly be a DOS to Unix confusion?
 
T

Tad McClellan

My real objective is to read a text file with a list of regular ^^^^^^^
expression in it that I can test against user input (that file may ^^^^^^^^^^
have 200 expressons).

perldoc -q regular
perldoc -q expression
perldoc -q match

Did you try any of those before posting?


How do I efficiently match many regular expressions at once?

#!/usr/bin/perl -w
use strict;


Lexical warnings (use warnings;) are better than -w (if you
have a perl less than 3 years old or so).

my @data = <DATA>;
for(@data)


Don't put the whole thing in memory unless you need the
whole thing in memory:

while ( <DATA> )

can replace those 2 lines.
 
G

gnari

#!/usr/bin/perl -w

use strict;

my $request = shift @ARGV;

my @data = <DATA>;
for(@data)
{
chomp;
print "'$request' ?~ /$_/: ";
if ($request =~ /$_/) {print 'yes';}
print "\n";
}
__DATA__
test1
test2
test3
...
I assume that the contents of the file you're reading from is under
your control, BTW; regular expressions can contain code, so if
somebody can write to that file they'll have the ability to cause your
script to execute code of their choice.

please provide an example.
there is no eval() or /e in the code showed.
(?{}) is not interpolated.

what are you talking about?

gnari
 
B

Ben Morrow

Quoth bowsayge said:
Scott W Gifford said to us:

[...]
regular expressions can contain code, so if
somebody can write to that file they'll have the ability to cause your
script to execute code of their choice.

How can this feature be disabled?

It is disabled by default in regexes created from interpolated
variables, unless you use re 'eval'. That is, either of these will
execute the code:

/(?{system 'rm -rf /'})/

{
use re 'eval';
my $danger = '(?{system "rm -rf /"})';
/$danger/;
}

but this won't:

my $danger = '(?{system "rm -rf /"})';
/$danger/;

..

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top