Use of uninitialised value, how to avoid?

Discussion in 'Perl Misc' started by Justin C, Jul 22, 2010.

  1. Justin C

    Justin C Guest

    I'm trying to avoid warnings in my Apache logs. Here's the code that's
    causing the problem:

    unless ($methods->{$param{method}}) {
    $param{method} = "default";
    }

    If the web-page in question is called without any parameters then
    $param{method} is undef, in which case I want $param->{method} =
    "default".

    When the page is called with parameters all is OK.

    What is a better way of doing the above to avoid "Use of uninitialized
    value $param{"method"}" ?

    Justin.

    --
    Justin C, by the sea.
     
    Justin C, Jul 22, 2010
    #1
    1. Advertising

  2. Justin C <> wrote:
    > I'm trying to avoid warnings in my Apache logs. Here's the code that's
    > causing the problem:


    > unless ($methods->{$param{method}}) {
    > $param{method} = "default";
    > }


    > If the web-page in question is called without any parameters then
    > $param{method} is undef, in which case I want $param->{method} =
    > "default".


    > When the page is called with parameters all is OK.


    > What is a better way of doing the above to avoid "Use of uninitialized
    > value $param{"method"}" ?


    In the 'unless' condition you use

    $methods->{$param{method}}

    so if $param{method} is undefined you may be asking for a hash
    element with an undefined key. Shouldn't that line actually be

    unless ($param{method}) {
    $param{method} = "default";
    }

    or just

    $param{method} = "default" unless $param{method};

    if you want to check if $param{method} is defined and assign a
    default value otherwise?
    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Jul 22, 2010
    #2
    1. Advertising

  3. Justin C

    Guest

    On Thu, 22 Jul 2010 14:34:08 +0100, Justin C <> wrote:

    >I'm trying to avoid warnings in my Apache logs. Here's the code that's
    >causing the problem:
    >
    >unless ($methods->{$param{method}}) {
    > $param{method} = "default";
    >}
    >
    >If the web-page in question is called without any parameters then
    >$param{method} is undef, in which case I want $param->{method} =
    >"default".
    >
    >When the page is called with parameters all is OK.
    >
    >What is a better way of doing the above to avoid "Use of uninitialized
    >value $param{"method"}" ?
    >
    > Justin.


    Depends on what you want:

    # (undef,'') fail ; (0) pass
    unless (defined $param{method} and length $param{method}) {
    $param{method} = "default";
    }
    # (undef,0,'') fail
    $param{method} ||= "default";

    # (undef) fail ; (0,'') pass
    $param{method} //= "default";

    -sln
     
    , Jul 22, 2010
    #3
  4. On 22.07.2010 16:20, Jens Thoms Toerring wrote:
    > Justin C <> wrote:
    >> When the page is called with parameters all is OK.

    >
    >> What is a better way of doing the above to avoid "Use of uninitialized
    >> value $param{"method"}" ?

    >
    > In the 'unless' condition you use
    >
    > $methods->{$param{method}}
    >
    > so if $param{method} is undefined you may be asking for a hash
    > element with an undefined key. Shouldn't that line actually be
    >
    > unless ($param{method}) {
    > $param{method} = "default";
    > }


    Probably this is not a solution because this removes the check whether
    there exists a key in %$methods. If you omit this check, the user could
    specify any method, also non existing ones.

    To get rid of the warning, one can use for example:

    unless ($param{'method'} && $methods->{$param{'method'}}) { ... }

    Something like

    $param{method} ||= "default";
    die "invalid method\n" unless $methods->{$param{method}};

    might be better than overwriting wrong methods with the default. But
    this of course depends.

    Wolf
     
    Wolf Behrenhoff, Jul 22, 2010
    #4
  5. Justin C

    Guest

    On Thu, 22 Jul 2010 08:03:25 -0700, wrote:

    >On Thu, 22 Jul 2010 14:34:08 +0100, Justin C <> wrote:
    >
    >>I'm trying to avoid warnings in my Apache logs. Here's the code that's
    >>causing the problem:
    >>
    >>unless ($methods->{$param{method}}) {
    >> $param{method} = "default";
    >>}
    >>
    >>If the web-page in question is called without any parameters then
    >>$param{method} is undef, in which case I want $param->{method} =
    >>"default".
    >>
    >>When the page is called with parameters all is OK.
    >>
    >>What is a better way of doing the above to avoid "Use of uninitialized
    >>value $param{"method"}" ?
    >>
    >> Justin.

    >
    >Depends on what you want:
    >
    ># (undef,'') fail ; (0) pass
    > unless (defined $param{method} and length $param{method}) {
    > $param{method} = "default";
    > }
    ># (undef,0,'') fail
    > $param{method} ||= "default";
    >
    ># (undef) fail ; (0,'') pass
    > $param{method} //= "default";
    >


    So perhaps:

    use strict;
    use warnings;

    my %param;
    my $methods = {
    default => sub {print "Default handler method ..\n"},
    } ;
    $param{method} //= "default" ;
    exists $methods->{ $param{method} }
    and $methods->{ $param{method} }()
    or die "Cannot find $param{method}() method" ;

    -sln
     
    , Jul 22, 2010
    #5
  6. Justin C

    Justin C Guest

    On 2010-07-22, Justin C <> wrote:
    > I'm trying to avoid warnings in my Apache logs. Here's the code that's
    > causing the problem:
    >
    > unless ($methods->{$param{method}}) {
    > $param{method} = "default";
    > }
    >
    > If the web-page in question is called without any parameters then
    > $param{method} is undef, in which case I want $param->{method} =
    > "default".
    >
    > When the page is called with parameters all is OK.
    >
    > What is a better way of doing the above to avoid "Use of uninitialized
    > value $param{"method"}" ?


    Thanks to Jens and Wolf. Between you both you spotted a flaw in my code
    and provided the work-around.

    And sln, I think the last two of your suggestions were 5.10 solutions,
    these are operators I've yet to explore, though I can see, from your
    explanation, what each is doing. I'll investigate them further.

    Justin.

    --
    Justin C, by the sea.
     
    Justin C, Jul 22, 2010
    #6
  7. Justin C

    Justin C Guest

    On 2010-07-22, <> wrote:
    > On Thu, 22 Jul 2010 14:34:08 +0100, Justin C <> wrote:
    >
    >>I'm trying to avoid warnings in my Apache logs. Here's the code that's
    >>causing the problem:
    >>
    >>unless ($methods->{$param{method}}) {
    >> $param{method} = "default";
    >>}
    >>
    >>If the web-page in question is called without any parameters then
    >>$param{method} is undef, in which case I want $param->{method} =
    >>"default".
    >>
    >>When the page is called with parameters all is OK.
    >>
    >>What is a better way of doing the above to avoid "Use of uninitialized
    >>value $param{"method"}" ?
    >>
    >> Justin.

    >
    > Depends on what you want:
    >
    > # (undef,'') fail ; (0) pass
    > unless (defined $param{method} and length $param{method}) {
    > $param{method} = "default";
    > }
    > # (undef,0,'') fail
    > $param{method} ||= "default";
    >
    > # (undef) fail ; (0,'') pass
    > $param{method} //= "default";


    What I want is to test that $param{method} is a valid method (if it has
    been supplied at all). If it's not a valid method, or is not defined,
    then it is to be set to "default".

    I've gone with (line wrapped to avoid break):
    $param{method} = "default"
    unless ( ($param{method}) && ($methods->{$param{method}}) );

    This avoids the "Use of uninitialized.." in my Apache logs.

    I'm trying to get my head around '||=' and '//=', they're great
    shortcuts, I've just got to use them a few times to get it set in my
    head.

    Justin.

    --
    Justin C, by the sea.
     
    Justin C, Jul 23, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Fish
    Replies:
    7
    Views:
    554
    David Carlisle
    Jan 10, 2005
  2. Cognition Peon

    use of uninitialised value

    Cognition Peon, Apr 15, 2004, in forum: Perl Misc
    Replies:
    5
    Views:
    150
    Ala Qumsieh
    Apr 15, 2004
  3. Len
    Replies:
    3
    Views:
    193
  4. Jess
    Replies:
    2
    Views:
    170
  5. Mark

    uninitialised value & valgrind

    Mark, Jun 26, 2012, in forum: C Programming
    Replies:
    4
    Views:
    1,127
    Johann Klammer
    Jun 27, 2012
Loading...

Share This Page