Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

uninitialized value

by frank1 (Scribe)
on Jun 12, 2024 at 08:41 UTC ( [id://11159914]=perlquestion: print w/replies, xml ) Need Help??

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

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


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


    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.



Re: uninitialized value
by soonix (Canon) 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 (Chaplain) 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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11159914]
Approved by Corion
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-07-12 10:41 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.