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

Am trying to get button to do something, and its working well.

But how can i get rid of this error

Use of uninitialized value in string eq at jj.pl

if (param('btn') eq 'btn') { #Do something }
<button type="submit" name="btn" value="btn">Do something</button>

Replies are listed 'Best First'.
Re: uninitialized value
by hippo (Archbishop) on Jun 12, 2024 at 08:50 UTC

    Idiomatically,

    my $btn = param ('btn'); if (defined ($btn) && $btn eq 'btn') { ... }

    or

    use 5.010; my $btn = param ('btn') // ''; if ($btn eq 'btn') { ... }

    Or see warnings for taking off the seatbelts - but don't even think about doing that until you understand what you're doing.


    🦛

      thanks

Re: uninitialized value
by soonix (Chancellor) on Jun 12, 2024 at 08:54 UTC
    you have to make sure that param('btn') returns a defined value, which, according to the error message, currently isn't the case.
Re: uninitialized value
by The_Dj (Scribe) on Jun 13, 2024 at 01:25 UTC
    If you don't want extra variables, you can also just use //

     if ( (param('btn')//'') eq 'btn' ) {

    (or if you're playing code golf:  if(param('btn')//''eq'btn'){)
Re: uninitialized value
by harangzsolt33 (Deacon) on Jun 15, 2024 at 02:35 UTC
    <button type="submit" name="btn" value="btn">Do something</button>

    Excuse me, but I have some questions. First of all, why do you request the value of "btn" here? The object that you create here is a button with the handle name ("btn") and face value "btn." In other words, it creates a button that says "btn" on the face. So, if the user clicks the button that has "btn" written on it, then something happens. Okay, but why would you want to read the value of the button itself? Usually, people want to read a text value that is provided in a form such as:

    <INPUT TYPE="TEXT" NAME="EmployeeAddress" VALUE="">

    Or if you want the value to be hidden from view, then you would do this:

    <INPUT TYPE="HIDDEN" NAME="EmployeeID" VALUE="376192">

    Very rarely we would have a situation where we want to read the face value of a button.

    Anyway, my second question is why would this "btn" value be undefined when you clearly give it a value. The value of "btn" object is "btn" So, this should never be undefined. I don't understand why you are getting this error or warning in the first place.

      Most probably because the HTML calling the CGI has no btn ...

      Don't ask me if this is good design or if this OP even cares. 🤷🏻‍♂️

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

      Dude, look at this users posts, don't expect a sensible response.