data comprised of regexs (while loop weirdness)

Discussion in 'Perl Misc' started by jason@cyberpine.com, Aug 12, 2004.

  1. Guest

    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$
    , Aug 12, 2004
    #1
    1. Advertising

  2. writes:

    > 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.
    Scott W Gifford, Aug 12, 2004
    #2
    1. Advertising

  3. wrote:
    > 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;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Aug 12, 2004
    #3
  4. Gunnar Hjalmarsson wrote:
    > wrote:
    >> 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;


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

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Aug 12, 2004
    #4
  5. <> wrote:


    > 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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Aug 13, 2004
    #5
  6. gnari Guest

    "Scott W Gifford" <> wrote in message
    news:...

    > #!/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
    gnari, Aug 13, 2004
    #6
  7. Ben Morrow Guest

    Quoth bowsayge <>:
    > 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

    --
    Razors pain you / Rivers are damp
    Acids stain you / And drugs cause cramp. [Dorothy Parker]
    Guns aren't lawful / Nooses give
    Gas smells awful / You might as well live.
    Ben Morrow, Aug 13, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,206
    Tim Slattery
    Mar 30, 2005
  2. steve
    Replies:
    4
    Views:
    508
    Brian van den Broek
    Mar 13, 2005
  3. David Mathog

    Initializing an array comprised of very long strings

    David Mathog, May 4, 2007, in forum: C Programming
    Replies:
    5
    Views:
    363
    =?UTF-8?B?QmrDuHJuIEF1Z2VzdGFk?=
    May 7, 2007
  4. Andrew Crabb

    Writing a module comprised of several files?

    Andrew Crabb, May 17, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    100
    Anno Siegel
    May 18, 2004
  5. Isaac Won
    Replies:
    9
    Views:
    342
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page