in reply to Re^3: Switch.pm Failure
in thread Switch.pm Failure

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; }; } }

Replies are listed 'Best First'.
Re^5: Switch.pm Failure
by haukex (Archbishop) on Feb 25, 2017 at 17:37 UTC

    The only way that this Switch { ... } block would make sense is if somewhere there was a sub Switch (&), either defined in the code or coming from a module. However, it's not coming from the Switch module nor, at least as far as I can tell, CGI::Switch. I suspect you may have omitted the source of this Switch from the code you posted, since I also don't see where the manageusers package is coming from. And by the way, is it uppercase Switch, or lowercase switch like in the root node?

    Unfortunately, what you've posted so far is not enough information to help you further. Please try to reduce the amount of code needed to reproduce the problem into a Short, Self-Contained, Correct Example. Also a good read is How do I post a question effectively? and I know what I mean. Why don't you?

    Also, assuming you were the one who posted this anonymously:

    I don't see the post How long does it take a moderator to approve or does that occur ordid I screw up?

    Posts should appear immediately, if they do not, something probably went wrong.

      "The only way that this Switch { ... } block would make sense is if somewhere there was a sub Switch (&);

      wow, but even then i would have done Switch sub {...} just like i say my $n=scalar(@ar);, im not into golf

        i would have done Switch sub {...}

        Ok, but without the sub is also not uncommon, for example I'd say Try::Tiny's sub try (&;@) clearly looks better when written try { ... } catch { ... };.

        In regards to your other post:

        the whole (logical-test) && do { process-stmts} ; setup while legal is very "un-perlish"

        I disagree, I think it's very perlish, in fact probably "too" perlish! ;-)

        given/when

        Note that these were retroactively marked experimental as of v5.16, and will generate warnings in Perl v5.18 and above:

        It is clear that smartmatch is almost certainly either going to change or go away in the future. Relying on its current behavior is not recommended.
Re^5: Switch.pm Failure
by huck (Prior) on Feb 25, 2017 at 17:11 UTC

    just take the word Switch off, it does nothing. It is not used as a label, it is not a use CGI::Switch construct, nor a use Switch construct.

    there seems to be no need for use CGI::Switch either, i dont think you know what that did anyway, but it is depreciated now.

    what is it that you wanted to do?

Re^5: Switch.pm Failure
by huck (Prior) on Feb 25, 2017 at 19:05 UTC

    Ya know, this kinda reeks of hand converting some other language into perl the hard way. the whole (logical-test) && do { process-stmts} ; setup while legal is very "un-perlish". with the liberal use if return (to return from Main) a more perl-like sequence would be

    if (logical-test1) { process-stmts1; return; } if (logical-test2) { process-stmts2; return; } ...
    or without the returns
    if (test1) { process-stmts1; } elsif (test2) { process-stmts2; } ...
    or in modern perls the given/when structure as in http://perldoc.perl.org/perlsyn.html#Switch-Statements

    it reminds me of the time i saw a guy translate fortran into sas by coding it all inside a single "do while(1)" statement rather than 3 simpler data steps.