fourmi has asked for the wisdom of the Perl Monks concerning the following question:

I am all logic'd out. a mars bar in the mail to anyone, who can simplify this for me!
if ( ($ENV{'HTTP_REFERER'} && $ENV{'REMOTE_ADDR'} ne "129.215.67.96") && $ENV{'REQUEST_URI'} !~ /\/bulletin\// && $ENV{'REQUEST_URI'} !~ /\.c +ss/ && $ENV{'REQUEST_URI'} !~ /\.ico/ && $ENV{'REQUEST_URI'} !~ /formmail\. +/ && $ENV{'REQUEST_URI'} !~ /\/prosp\// && $ENV{'REQUEST_URI'} !~ /\/cale +ndar\// && $ENV{'REQUEST_URI'} !~ /\/edit\// && $ENV{'REQUEST_URI'} !~ /tmp\/calendar2002-3\/pgradh\/regs\/029\.html/ )
In addition I'd also like to get the following in there...
PseudoCoded
false if $ENV{'REQUEST_URI'} contains "FalseIfJustThis" true if $ENV{'REQUEST_URI'} contains "FalseIfJustThis" AND contains "ButIfAlsoThisThenTrue")
there are some instances where "ButIfAlsoThisThenTrue" should lead to a false, so merely working it on this string won't work.

My knickers are in a twist, and i am fairly sure i am going the wrong way about this!!
Cheers
ant

edited by ybiC: balanced <code> tags and format tweaks

Replies are listed 'Best First'.
Re: If Simplification
by broquaint (Abbot) on Aug 07, 2003 at 10:58 UTC
    my $req_uri_re = join '|', map quotemeta, qw( /bulletin/ .css .ico formmail /prosp/ /calendar/ /edit/ /tmp/calendar2002-3/pgradh/regs/029.html/ ); if ($ENV{'HTTP_REFERER'} and $ENV{'REMOTE_ADDR'} ne "129.215.67.96" and $ENV{REQUEST_URI} !~ $req_uri_re) { }
    And your pseudocode, in real code, might look like this
    ## false if $ENV{'REQUEST_URI'} contains "FalseIfJustThis" if($ENV{REQUEST_URI} !~ $regex) { } ## true if $ENV{'REQUEST_URI'} contains "FalseIfJustThis" ## AND contains "ButIfAlsoThisThenTrue") if($ENV{REQUEST_URI} =~ $regex and $ENV{REQUEST_URI} =~ $regex2) { }
    See. perlop and perlre for more info.
    HTH

    _________
    broquaint

      If i roll the two pseudocode if statements together then the following IS valid (isn't it?)
      if (($ENV{REQUEST_URI} !~ $regex) || ($ENV{REQUEST_URI} =~ $regex and + $ENV{REQUEST_URI} =~ $regex2)) {}
      I thought it would end up being fairly straightforward, I think my problem was trying to do the whole lot at once rather than bits at a time, and then combine, cheers for that!
      Or if you want to do the same thing in both cases, you can do with only a single evaluation:
      if( $ENV{REQUEST_URI} =~ $regex ? $ENV{REQUEST_URI} =~ $regex2 : 1 ) { # ... }
      Which reads a lot less awkwardly using $_.
      local $_ = $ENV{REQUEST_URI}; if(/$regex/ ? /$regex2/ : 1) { # ... }
      In other words, if matching $regex succeed, then it depends on whether it matches $regex2, if it failed, then it's always true.

      Makeshifts last the longest.

Re: If Simplification
by dbwiz (Curate) on Aug 07, 2003 at 10:59 UTC

    You can create a sub to check for an array of values that must not be inside a given string. This way, if you have more values, you can just add them to your array.

    sub absent { my $str = shift; for (@_) { return 0 if $str =~ $_ } return 1; } my @must_not_be_there = ( '/bulletin/', '.css', 'formmail.', '/prosp/', '/calendar/', '/edit/', '/tmp/calendar2002-3/pgradh/regs/029.html', 'FalseIfJustThis', 'ButIfAlsoThisThenTrue' ); if (($ENV{'HTTP_REFERER'} && $ENV{'REMOTE_ADDR'} ne "129.215.67.96") && absent ($ENV{'REQUEST_URI'}, @must_not_be_there) ) { print "matches\n"; }

    Or simply use grep.

    && !(grep {$ENV{'REQUEST_URI'} =~ $_} @must_not_be_there ) )
      Cool, the array idea sounds simple for my head, I'd need to have a think about how to get the pseudocode bit incorporated, ie, 'falseifjustthis' is allowed, but only if 'butifalsothisthentrue' is also there. Should be able to cope though! Exercise left to the reader, as they say!
      cheers!
Re: If Simplification
by edoc (Chaplain) on Aug 07, 2003 at 11:05 UTC

    untested 8)

    if (tester()){ } sub tester{ return unless $ENV{'HTTP_REFERER'}; return if $ENV{'REMOTE_ADDR'} eq "129.215.67.96"; return 1 if ( ($ENV{'REQUEST_URI'} =~ /FalseIfJustThis/) && ($ENV{'R +EQUEST_URI'} =~ /ButIfAlsoThisThenTrue/) ); return if $ENV{'REQUEST_URI'} =~ /FalseIfJustThis/; my @bad = ( '/bulletin/', '.css', '.ico', 'formmail.', '/prosp/', '/calendar/', '/edit/', 'tmp/calendar2002-3/pgradh/regs/029.html', 'FalseIfJustThis', ); return if $ENV{'REQUEST_URI'} =~ /\Q$_\E/ foreach(@bad); return 1; )

    Update: /\Q$_\E/ foreach(@bad); # (thanks anon)

    cheers,

    J

      You probably want to @bad = map quotemeta, @bad; or @bad = map {qr{$_}i} @bad;.
      Again thanks! Im going to have a play with the suggestions, and wil probably render them unrecognisable at the end, will repost what i settle with once the headache clears!!