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

I want to check a session variable, against the contents of a text file, I have the file open but cannot form regex correctly, format of data is 3 character sitecode ex. bar, rml, mrt, and the sessionvar is $Session->{'usrSystem'} which will match whatever site is selected in the main web app. if a match is found I want to send off to another subroutine if no match is found I just want to drop out of the loop. Here is what I have so far, I must have something wrong.
my $site_file='<c:\sitefile.txt'; open data, $site_file || die "Horrible death"; my @lines=<data>; my $str=""; close data; for my $line(@lines){ if($Session->{'usrSystem'})=~/$line/g; $Response->Write("Found a match! Hooray"); }
many thanks to all for their help, this brings up a new question I would like to change the value of a subroutine being called via a regex in another file on the same web application using patternmatching so for instance..my sub..
sub tSupport { my $str = ""; SWITCH: { if ($Request->item("View")->item() eq "HowTo"){ $str .= mHowTo() +; last SWITCH; } if ($Request->item("View")->item() eq "Tutorials"){ $str .= mTut +orials(); last SWITCH; } if ($Request->item("View")->item() eq "Tempotutorials"){ $str .= + mtempotutorials(); last SWITCH; } if ($Request->item("View")->item() eq "TManuals") {$str.=mTManua +ls(); last SWITCH;} if ($Request->item("View")->item() eq "PCMaint"){ $str .= mPCMai +nt(); last SWITCH; } if ($Request->item("View")->item() eq "Manuals"){ $str .= mManua +ls(); last SWITCH; } if ($Request->item("View")->item() eq "FAQ"){ $str .= mFAQ(); la +st SWITCH; } $str .= mSearch(); } return $str; }
would change say tutorials to mMLX40Tutorials() if site is equal to a 4.0 site.

Replies are listed 'Best First'.
Re: problem with a regex loop
by grep (Monsignor) on Oct 30, 2007 at 21:26 UTC
    This is one big reason to indent.

    If you had indented and put in some whitespace you would see:

    for my $line (@lines){ if( $Session->{'usrSystem'} )=~/$line/g; $Response->Write("Found a match! Hooray"); }
    Right there I can look at that code and see that the if isn't closed. And I notice that the if is missing an opening curly {.

    If I just look at (instead of read) this code I can instantly tell the blocks look balanced

    foreach my $line (@lines) { if ( $Session->{'usrSystem'} =~ /$line/g ) { $Response->Write("Found a match! Hooray"); } }

    grep
    One dead unjugged rabbit fish later...

      your right, I will definitely try to indent more often, and thanks for the help, I am getting closer to my goal which is to make this a switch for the main app, due to upgrades content needs to change on a rolling upgrade schedule.
Re: problem with a regex loop
by GrandFather (Saint) on Oct 30, 2007 at 21:26 UTC

    Show us a code sample that we can run and that doesn't need anything outside the sample script. You may find Re^3: loop through file checking for session variable provides a useful starting point.

    Use strictures (use strict; use warnings;).

    if($Session->{'usrSystem'})=~/$line/g;

    is not syntacticly correct. If it is intended as a statement modifier it needs a statement before the if. If it is an if it needs a block and the expression must be in parenthesis.

    Use the three parameter open:

    my $site_file = 'c:\sitefile.txt'; open my $Data, '<', $site_file or die "Horrible death";

    Perl is environmentally friendly - it saves trees
Re: problem with a regex loop
by toolic (Bishop) on Oct 30, 2007 at 21:24 UTC
    You may need to chomp your input lines to remove the newline (\n).
    chomp @lines;

    You may not want to use the /g modifier on your regex.

    Other coding style issues:

    In order to avoid a common operator precedence issue, you should use or instead of || in your open statement as Sidhekin previously pointed out.

    Adding whitespace to your code, in the form of indentation and around operators, would improve its readability.

Re: problem with a regex loop
by Jenda (Abbot) on Oct 31, 2007 at 21:26 UTC

    Your code would call the two item() methods several times, it's wastefull both at run time and when you typed it.

    SWITCH: for ($Request->item("View")->item()) { if ($_ eq 'HowTo') { $str .= mHowTo(); last SWITCH; if ($_ eq 'Tutorials') { $str .= mTutorials(); last SWITCH; ... }

    It might be even better to use a hash like this:

    my %actions = ( HowTo => \&mHowTo, Tutorials => \&mTutorials, ... ); ... sub tSupport { my $view = $Request->item("View")->item(); return exists($actions{$view}) ? $actions{$view}->() : mSearch(); }