in reply to Switch.pm Failure

There is a whole lot missing from that example, like two right braces at least. is switch supposed to be a label?

switch: { warn("manage_users.cgi line 54 '$action'"); #Request from User for Login Form ($action eq "GetLoginForm") && do { manageusers::OpenConnection(); } }

or a call to the switch method in CGI::Switch? http://search.cpan.org/~chips/perl5.004/lib/CGI/Switch.pm CGI::Switch - Try more than one constructors and return the first object available

my $q = new CGI::Switch;

Added:

or use of the plain switch module? http://search.cpan.org/~chorny/Switch-2.17/Switch.pm

use Switch; switch ($val) { case 1 { print "number 1" } case "a" { print "string a" } case [1..10,42] { print "number in list" } case (\@array) { print "number in list" } case /\w+/ { print "pattern" } case qr/\w+/ { print "pattern" } case (\%hash) { print "entry in hash" } case (\&sub) { print "arg to subroutine" } else { print "previous case not true" } }
I originally discounted this because of the lack of the switch constructs like case, but decided to add it too

Replies are listed 'Best First'.
Re^2: Switch.pm Failure
by choroba (Cardinal) on Feb 24, 2017 at 04:31 UTC
    Please, don't recommend Switch. Being a source filter, it causes more problems than it solves. Maybe you just wanted to point to it for completeness, or because that might be what the OP had in mind, but then it still lacks the warning.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      I didnt mean to recommend it, it was a sortof completeness thing. The partial example didnt match much, nothing like a call to CGI:switch, lacking a last switch; like a labeled block might use, and lacking the test-variable/case of the source filter, and with statements inside the block too. I added it as an after thought just in case it was what was meant.

        use strict; use diagnostics -verbose; #use CGI::Carp qw/fatalsToBrowser/; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use CGI; use CGI::Session; use CGI::Switch; use Time::HiRes qw(usleep);
        Here is the whole switch tree again. Didn't post yesterday. Was probably not logged in.
        # CGI Switch Tree #-------------------------------------------------------- Main(); sub Main { my $action = $query->param('action'); Switch { warn("manage_users.cgi line 54: '$action'"); #Request from User for Login Form ($action eq "GetLoginForm") && do { manageusers::OpenConnection(); #process login request - create session my ($result,$message0,$message1,$message2) = ProcessLoginReque +st($query); if(!$result){ #Tell client that login failed manageusers::CloseConnection(); LoginUserFailedForm("The Login Request failed. Please try a +gain or contact the office."); return; #exit; } elsif ($result == 1) { #Client already logged in so tell them manageusers::CloseConnection(); CreateAlreadyLoggedinForm($message0); return; } elsif ($result == 2){ #Not logged in so send client login form manageusers::CloseConnection(); CreateLoginForm($message0, $message1, $message2); return; #exit; } }; #--------------------------------------------------------------------- +---------- #Request from User for Update Existing Info Form ($action eq "GetUpdateForm") && do { #Get temp session id and unique temp Id my ($sid) = GetUserSessionCookie(); if($sid eq 0) { CreateUserFeedbackForm("Authentication failed. You need to + log in first to update your member data."); return; #exit; } else{ manageusers::OpenConnection(); if(!CheckForAuthorizedUser($sid)) { manageusers::CloseConnection(); CreateUserFeedbackForm("Authentication failed. Your auth +orization cookie has expired or been tampered with. For access to cha +nge parameters, please log in again."); return; #exit; } usleep(100); manageusers::CloseConnection(); CreateUpdateInfoForm(); return; #exit; } }; #--------------------------------------------------------------------- +---------- #Request from User for Missing Information Form ($action eq "GetMissingForm") && do { #Get temp session id and unique temp Id my ($tsid, $tsession_id) = ProcessLostDataRequest(); usleep(100); manageusers::CloseConnection(); CreateMissingInfoForm($tsid, $tsession_id); return; #exit; }; #--------------------------------------------------------------------- +---------- #Login User ($action eq "LoginUser") && do { #Attempt the login manageusers::OpenConnection(); warn("The Login Usr Query: $query"); my ($result, $login_timeout) = LoginUser($query); if ($result == 0){ manageusers::CloseConnection(); CreateLoginUserFailedForm("The Login User Name or Password +is invalid. Please try again or contact the office."); return; #exit; } elsif ($result == 1){ #Client already logged in so tell them manageusers::CloseConnection(); CreateAlreadyLoggedinForm("You are already logged in."); return; #exit; } elsif ($result == 2){ manageusers::CloseConnection(); CreateLoginUserFailedForm("Your Membership Has Expired. To r +enew membership, use the form on this site or contact the office."); return; #exit; } elsif ($result == 3) { manageusers::CloseConnection(); CreateUserLoginSucceededForm("Login succeeded."); return; #exit; } elsif ($result == 4){ manageusers::CloseConnection(); CreateLoginUserMaxAttemptsFailedForm("Maximum allowed number + of login attempts exceeded for session. Please contact the office.") +; return; #exit; } elsif ($result == 5){ manageusers::CloseConnection(); CreateLoginUserFailedForm("The '$login_timeout' second Login + window has expired. Please try again or contact the office"); return; #exit; } else { manageusers::CloseConnection(); CreateLoginUserFailedForm("Login failed for unknown reasons. + Please contact the office."); return; #exit; } return; #exit; }; #--------------------------------------------------------------------- +---------- #Update logged in user data ($action eq "UpdateUserData") && do { #Extract the query data from CGI parameters my $password = $query->param("password"); my $email = $query->param("email"); my $secret = $query->param("secret"); #Check for illegal characters in the input fields if (!CheckValidLoginChar($password)){ CreateUpdateUserInvalidCharForm("The new Passw +ord you entered contained invalid characters and did not succeed."); return; #exit; } if ($email ne ""){ if (!CheckValidEmailChar($email)){ CreateUpdateUserInvalidCharForm("The new e-M +ail Address you entered contained invalid characters and did not succ +eed."); return; #exit; } } if (!CheckValidLoginChar($secret)){ CreateUpdateUserInvalidCharForm("The new secre +t you entered contained invalid characters and did not succeed."); return; #exit; } #Try to get the cookie from the user for authenticatio +n for updating user data my ($sid) = GetUserSessionCookie(); if(!$sid) { CreateUserFeedbackForm("Authentication failed. Yo +u must be logged in under your old username and password to perform t +his update."); return; #exit; } manageusers::OpenConnection(); if (!UpdateUserData($sid,$password,$email,$secret)) { manageusers::CloseConnection(); CreateUserFeedbackForm("Update of Member informatio +n failed. Please contact the office."); return; #exit; } else { manageusers::CloseConnection(); CreateUserFeedbackForm("Update of Member informatio +n succeeded."); return; #exit; } return; #exit; }; #--------------------------------------------------------------------- +---------- #Verify that the user has access rights to controlled areas ($action eq "CheckUserAuth") && do { #Try to get the cookies from the user my ($sid) = GetUserSessionCookie(); warn("Check User Authorization SID returned from GetUserSessi +onCookie SID: $sid"); if($sid eq 0) { CreateUserFeedbackForm("Authentication failed. You need to l +og in to access secured areas."); return; #exit; } else { manageusers::OpenConnection(); if(!CheckForAuthorizedUser($sid)) { manageusers::CloseConnection(); CreateUserFeedbackForm("Authentication failed. Your autho +rization cookie has expired or been tampered with. Please log in agai +n."); return; #exit; } manageusers::CloseConnection(); CreateUserFeedbackForm("Authentication succeded."); #not nece +ssary return; #exit; } return; #exit; }; #--------------------------------------------------------------------- +---------- #Logout the user ($action eq "LogoutUser") && do { warn("Logout User Enter Function Line 238"); #Try to get the cookies from the user my ($sid) = GetUserSessionCookie(); warn("Logout User SID returned from GetUserSessionCookie SID: +$sid"); if($sid eq 0) { CreateUserFeedbackForm("You are not logged in, your authoriz +ation cookie has expired or been tampered with. For access, please lo +g in again if you wish."); return; #exit; } else { manageusers::OpenConnection(); warn("action Logout User Line 216 SID: $sid"); my $result = LogoutUser($sid); if ($result == 0){ manageusers::CloseConnection(); CreateUserFeedbackForm("The logout failed. Perhaps the co +okie was corrupted or tampered with. Please log in again to clear thi +s problem."); return; #exit; } elsif ($result == 1) { manageusers::CloseConnection(); CreateUserFeedbackForm("You are already logged out."); return; #exit; } elsif ($result == 2) { manageusers::CloseConnection(); CreateUserFeedbackForm("Logout succeded."); return; #exit; } } return; #exit; }; #--------------------------------------------------------------------- +---------- #Lost User Data equest ($action eq "LostUserData") && do { #Extract the query data from CGI parameters my $username = $query->param("username"); my $secret = $query->param("secret"); #Check for illegal characters in the input fields if (!CheckValidLoginChar($username)){ CreateLoginUserInvalidCharForm("The login User + Name you submitted contained invalid characters and did not succeed. +"); return; #exit; } if (!CheckValidLoginChar($secret)){ CreateLoginUserInvalidCharForm("The My Secret name +you submitted contained invalid characters and did not succeed."); return; #exit; } #Attempt the login manageusers::OpenConnection(); my $result = GetUserLostData($username,$secret,$fbacro +nym); if ($result == 0){ manageusers::CloseConnection(); CreateUserFeedbackForm("We could not locate your rec +ords based on the information supplied. Please try again or contact t +he office"); return; #exit; } if ($result == 1){ manageusers::CloseConnection(); CreateUserFeedbackForm("Your missing information has + been sent."); return; #exit; } return; #exit; }; } }
Re^2: Switch.pm Failure
by Anonymous Monk on Feb 25, 2017 at 14:38 UTC
    I relied yesterday with the whole block of code. I don't see the post How long does it take a moderator to approve or does that occur ordid I screw up? ??