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

I am trying to modify some Perl code, and cannot seem to get it correct. This is an already established application, and all I am trying to do is put in one additional field, and based on the answer to that field (radio button) set the subject in the email which is being generated at the submit time. My basic code looks like this: Placing the button on the form for end user view-ability.
<tr> <td> Is this a Branch Manager performing a Business Call? </td> <td> <input type="radio" name="businessCall" value="Yes_bus" />Yes + <input type="radio" name="businessCall" value="No_bus" />No * </td> </tr>
If statement - this have been changed many times, and no matter what I do, it is still given me an error. If I keep out the if statement and just display the field it works fine, so it is not an issue with the field names, just my formatting of the code itself. I still classify myself as a newbie to Perl.
if $businessCall.value = "Yes_bus" { my $subject = "Branch Manager Business Call" } else if $businessCall.value = "No_bus" { my $subject = "Merchant Referral" }
Thank you in advance for any assistance you can give me to get what I believe is a very easy if then statement working here.

Replies are listed 'Best First'.
Re: If statement based on Radio Button Values
by stevieb (Canon) on Nov 04, 2015 at 22:14 UTC

    That is not perl code...

    if (...){ .... } elsif (...){ ... }

    ...is a proper if/else if statement. Also, in perl5, variables aren't objects (except in certain situations), and they certainly can't have methods called on them in that way.

    It would be prudent to show us the error message(s). I doubt the following will fix your problem, but this code is at least proper Perl:

    my $subject; if ($businessCall eq "Yes_bus"){ $subject = "Branch Manager Business Call"; } elsif ($businessCall eq "No_bus"){ $subject = "Merchant Referral"; }
      The message received is as follows:

      Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. More information about this error may be available in the server error log.

      The message is generic. Not having my statement in Perl, is not a shocker to me. I am still new and still learning the ins and outs of formatting in Perl. Thanks for the note - I will try to research a bit more, as the small amount of changes I made in reference to your IF example still produced the same result.
        More information about this error may be available in the server error log.

        That's the big hint. If you look in the server error log as the message suggests you will likely find the real problem (which is probably that your code doesn't compile given the snippet above).

        To get a more helpful error message put this line up near the top of your script:
        use CGI::Carp('fatalsToBrowser');
Re: If statement based on Radio Button Values
by jeffa (Bishop) on Nov 04, 2015 at 22:46 UTC

    I recommend learning Mojolicious

    use strict; use warnings; use Mojolicious::Lite; our %subject = ( Yes_bus => 'Branch Manager Business Call', No_bus => 'Merchant Referral', ); get '/' => sub { my $self = shift; my $selection = $self->param( 'businessCall' ); $self->stash( selection => $selection, subject => $subject{$select +ion} ); $self->render( template => 'index' ); }; app->start; __DATA__ @@ index.html.ep <form> <input type="radio" name="businessCall" value="Yes_bus" />Yes <input type="radio" name="businessCall" value="No_bus" />No <input type="submit" name="Go" /> </form> % if ($selection) { You picked <tt><%= $selection %></tt> which yields: <tt><%= $subje +ct %></tt> % }

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: If statement based on Radio Button Values
by davido (Cardinal) on Nov 05, 2015 at 02:19 UTC

    Please read perlintro. It contains examples of Perl's if statements, and of the proper use of the relational equality operator. Plus, you'll pick up a few other tips along the way. It will take about 20 minutes to get through.


    Dave

Re: If statement based on Radio Button Values
by jaacmmason (Acolyte) on Nov 05, 2015 at 20:19 UTC
    Thank you all for the info given thus far! I have done more research (not enough yet) and have made modifications so my IF statement now looks like this:
    if (businessCall.value = "Yes_bus") { my $subject = "Branch Manager Business Call"; } else { my $subject = "Merchant Referral"; }
    My error message is now a Software error: Can't modify concatenation (.) or string in scalar assignment near ""Yes_bus") " Global symbol "$subject" requires explicit package name at line 382 (which is where I use the smtp to send the email I am trying to generate).

    If I just have a line of code stating:
    my $subject = "Merchant Referral";
    it works without issue. I am 100% messing up the If statement. I have tried without the ".value", with and without '==', as well as with an else if, and else. Most of the example I have looked at are dealing with "If (this happens) { Print statement here }; Not in setting value.
      businessCall.value is Javascript not Perl. Without seeing your script I can only guess you are using CGI and it should be something like
      my $subject = "Merchant Referral"; if (param('businessCall') eq "Yes_bus") { $subject = "Branch Manager Business Call"; }
      poj
      if (businessCall.value = "Yes_bus") {
      ...
      Can't modify concatenation (.) or string in scalar assignment near ""Yes_bus")

      That's an entirely descriptive message. You have a scalar assignment (=) when what you almost certainly want is a string equivalence test (eq). On the left hand side you have a concatenaction (.) between two barewords.

      Perhaps it's time to stop typing and have a nice cup of tea and a slow, steady read through perlintro and perlsyn?