unintialised warning

Discussion in 'Perl Misc' started by John, Nov 1, 2009.

  1. John

    John Guest

    Hi

    The following can cause a warning if undefined

    my $value=$PAR{$name};

    What the best solution?

    my $value=$PAR{$name }or $value='';
    or
    my $value=''; if (defined $PAR{$name}) {$value=$PAR{$name}}
    or
    is there a cleaner solution?

    Regards
    John
     
    John, Nov 1, 2009
    #1
    1. Advertising

  2. John

    Guest

    On Sun, 1 Nov 2009 15:32:17 -0000, "John" <> wrote:

    >Hi
    >
    >The following can cause a warning if undefined
    >
    >my $value=$PAR{$name};

    ^^^^^
    only if undefined

    -sln
     
    , Nov 1, 2009
    #2
    1. Advertising

  3. body { font: Helvetica, Arial, sans-serif; } p { font: Helvetica, Arial, sans-serif; } ..standout { font-family: verdana, arial, sans-serif; font-size: 12px; color: #993333; line-height: 13px; font-weight: bold; margin-bottom: 10px; } ..code { border-top: 1px solid #ddd; border-left: 1px solid #ddd; border-right: 2px solid #000; border-bottom: 2px solid #000; padding: 10px; margin-top: 5px; margin-left: 5%; margin-right: 5%; background: #ffffea; color: black; -moz-border-radius: 10px; } ..codedark { border-top: 10px solid #03f; border-left: 1px solid #ddd; border-right: 2px solid grey; border-bottom: 2px solid grey; padding: 10px; margin-top: 5px; margin-left: 5%; margin-right: 5%; background: black; color: yellow; -moz-border-radius: 10px; } #code { color: black; font-size: 14px; font-family: courier; padding-left: 5px; } #line-number { color: #804000; font-family: Arial; font-size: 14px; padding-right: 5px; border-right: 1px dotted #804000; } blockquote[type=cite] { padding: 0em .5em .5em .5em !important; border-right: 2px solid blue !important; border-left: 2px solid blue !important; } blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid maroon !important; border-left: 2px solid maroon !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid teal !important; border-left: 2px solid teal !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid purple !important; border-left: 2px solid purple !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid green !important; border-left: 2px solid green !important; } On 11/01/2009 08:32 AM, John wrote: Hi

    The following can cause a warning if undefined

    my $value=$PAR{$name};

    What the best solution?

    my $value=$PAR{$name }or $value='';
    or
    my $value=''; if (defined $PAR{$name}) {$value=$PAR{$name}}
    or
    is there a cleaner solution?
    my $value ||= $PAR{$name}


    --
    Andrew DeFaria
    If a book about failures does not sell, is it a success?
     
    Andrew DeFaria, Nov 1, 2009
    #3
  4. John

    Dr.Ruud Guest

    John wrote:

    > The following can cause a warning if undefined
    >
    > my $value=$PAR{$name};


    my $value = defined($name) ? $PAR{$name} : undef;


    This means that $value itself can still be undef.


    Or did you mean 'name' in stead of $name?

    --
    Ruud
     
    Dr.Ruud, Nov 1, 2009
    #4
  5. Andrew DeFaria wrote:
    > On 11/01/2009 08:32 AM, John wrote:
    >>
    >> The following can cause a warning if undefined
    >>
    >> my $value=$PAR{$name};
    >>
    >> What the best solution?
    >>
    >> my $value=$PAR{$name }or $value='';
    >> or
    >> my $value=''; if (defined $PAR{$name}) {$value=$PAR{$name}}
    >> or
    >> is there a cleaner solution?

    >
    > my $value ||= $PAR{$name}


    That is exactly the same as saying:

    my $value = $PAR{$name}

    Because my() creates a variable that starts out undefined and an
    undefined variable is false so the || test will always fail.


    John
    --
    The programmer is fighting against the two most
    destructive forces in the universe: entropy and
    human stupidity. -- Damian Conway
     
    John W. Krahn, Nov 1, 2009
    #5
  6. John

    Guest

    On Sun, 1 Nov 2009 15:32:17 -0000, "John" <> wrote:

    >Hi
    >
    >The following can cause a warning if undefined
    >
    >my $value=$PAR{$name};
    >
    >What the best solution?
    >
    >my $value=$PAR{$name }or $value='';
    >or
    >my $value=''; if (defined $PAR{$name}) {$value=$PAR{$name}}
    >or
    >is there a cleaner solution?
    >
    >Regards
    >John
    >
    >


    The only uninitialized warning would be if $name were undefined
    when you try to use it, ie: $PAR{$name}. It is always legitimate
    to assing undef to a scalar variable.

    Don't be confused between logical's 'or' and ||. They both do the
    same logical test form, but 'or' is used for control flow.

    So, my $value2 = 0 or 1; is functionally equivalent to
    (my $value2 = 0) or 1; which assigns 0 to $value2 because
    ^^^^^^^^^^^^^^^
    this expression is evaluated and the result of the assignment is always
    *TRUE*, and because of that is always meaningless, a non-conditional.

    You have to be carefull with || as well. Perl interprets 'undef' logically
    equal to 0. So $value = undef || 1; and $value = 0 || 1; both assign
    1 to $value.

    Some examples follow below ..

    -sln

    ---------
    # 'or' can be used like '||' when used for control flow.

    use strict;
    use warnings;

    my $value;

    $value = undef || 1;
    print $value,"\n";

    $value = 0 || 1;
    print $value,"\n\n";

    # The ||, // and && operators return the last value evaluated
    # (unlike C's || and &&, which return 0 or 1).
    # --------------------
    # For Perl 5.10 :
    # $a // $b is exactly equivalent to defined($a) ? $a : $b

    $value = defined(0) ? 0 : 3;
    print $value,"\n";

    $value = defined(undef) ? 0 : 3;
    print $value,"\n";

    $value = 0 // 3;
    print $value,"\n";

    $value = undef // 3;
    print $value,"\n";

    $value = undef;
    my $value2 = 0 or 1;
    print $value2,"\n";
    __END__

    Found = in conditional, should be == at gg.pl line 34.
    1
    1

    0
    3
    0
    3
    0
     
    , Nov 1, 2009
    #6
  7. On 2009-11-01 18:43, <> wrote:
    > On Sun, 1 Nov 2009 15:32:17 -0000, "John" <> wrote:
    >>The following can cause a warning if undefined
    >>
    >>my $value=$PAR{$name};
    >>
    >>What the best solution?
    >>
    >>my $value=$PAR{$name }or $value='';
    >>or
    >>my $value=''; if (defined $PAR{$name}) {$value=$PAR{$name}}
    >>or
    >>is there a cleaner solution?

    >
    > The only uninitialized warning would be if $name were undefined
    > when you try to use it, ie: $PAR{$name}. It is always legitimate
    > to assing undef to a scalar variable.
    >
    > Don't be confused between logical's 'or' and ||. They both do the
    > same logical test form, but 'or' is used for control flow.


    Both 'or' and '||' have the same effect on control flow, i.e., both are
    short-circuiting operators. The only difference is that 'or' has lower
    precedence so sometimes you can avoid typing a pair of parentheses.

    hp
     
    Peter J. Holzer, Nov 1, 2009
    #7
  8. John

    Justin C Guest

    On 2009-11-01, Andrew DeFaria <> wrote:
    ><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    > my $value ||= $PAR{$name}<br>


    Why do you find it necessary to post over 100 lines of html just to
    reply with one line? We understand your meaning by reading the text
    that you post, not the code that your software generates. Please do
    not post html here, plain-text only in this newsgroup.

    Justin.

    --
    Justin C, by the sea.
     
    Justin C, Nov 3, 2009
    #8
  9. John

    Guest

    On Nov 1, 8:32 pm, "John" <> wrote:
    > Hi
    >
    > The following can cause a warning  if undefined
    >
    > my $value=$PAR{$name};
    >
    > What the best solution?
    >
    > my $value=$PAR{$name }or $value='';
    > or
    > my $value=''; if (defined $PAR{$name}) {$value=$PAR{$name}}
    > or
    > is there a cleaner solution?
    >


    Not sure if it's a cleaner solution, but you could do this way:

    ##1
    my $EMPTY_STR = q{};
    my $value = ( !defined $name ) ? $EMPTY_STR # assign an empty
    incase scalar $name not defined
    : ( !exists $PAR{$name} ) ? $EMPTY_STR # assign an empty
    incase key $name not found
    : ( !defined $PAR{$name} ) ? $EMPTY_STR # assign an empty
    incase key $name => value undef
    : $PAR{$name}# lastly get the
    defined value against the key $name
    ;

    ##2
    my $value = q{} if !defined $name or !exists $PAR{$name} or !defined
    $PAR{$name};
    $value ||= $PAR{$name};

    --Rakesh
     
    , Nov 3, 2009
    #9
  10. Justin C <> wrote:
    >On 2009-11-01, Andrew DeFaria <> wrote:
    >><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    >> my $value ||= $PAR{$name}<br>

    >
    >Why do you find it necessary to post over 100 lines of html just to
    >reply with one line? We understand your meaning by reading the text
    >that you post, not the code that your software generates. Please do
    >not post html here, plain-text only in this newsgroup.


    This has been discussed ad nauseum a while ago. Mr. DeFaria is resistant
    to any attempt of reasoning and therefore ended up in most peoples
    filters.
    You must have missed that thread.

    jue
     
    Jürgen Exner, Nov 3, 2009
    #10
  11. body { font: Helvetica, Arial, sans-serif; } p { font: Helvetica, Arial, sans-serif; } ..standout { font-family: verdana, arial, sans-serif; font-size: 12px; color: #993333; line-height: 13px; font-weight: bold; margin-bottom: 10px; } ..code { border-top: 1px solid #ddd; border-left: 1px solid #ddd; border-right: 2px solid #000; border-bottom: 2px solid #000; padding: 10px; margin-top: 5px; margin-left: 5%; margin-right: 5%; background: #ffffea; color: black; -moz-border-radius: 10px; } ..codedark { border-top: 10px solid #03f; border-left: 1px solid #ddd; border-right: 2px solid grey; border-bottom: 2px solid grey; padding: 10px; margin-top: 5px; margin-left: 5%; margin-right: 5%; background: black; color: yellow; -moz-border-radius: 10px; } #code { color: black; font-size: 14px; font-family: courier; padding-left: 5px; } #line-number { color: #804000; font-family: Arial; font-size: 14px; padding-right: 5px; border-right: 1px dotted #804000; } blockquote[type=cite] { padding: 0em .5em .5em .5em !important; border-right: 2px solid blue !important; border-left: 2px solid blue !important; } blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid maroon !important; border-left: 2px solid maroon !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid teal !important; border-left: 2px solid teal !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid purple !important; border-left: 2px solid purple !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid green !important; border-left: 2px solid green !important; } a:link { color: blue; } a:visited { color: darkblue; } a:hover { color: black; background-color: #ffffcc; text-decoration: underline; } a:active { color: red; } On 11/03/2009 08:32 AM, Justin C wrote: On 2009-11-01, Andrew DeFaria &lt;&gt; wrote:
    &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
    my $value ||= $PAR{$name}&lt;br&gt;

    Why do you find it necessary to post over 100 lines of html just to reply with one line? For much the same reason why you feel the need to complain about what other people do.


    --
    Andrew DeFaria
    What is a free gift? Aren't all gifts free?
     
    Andrew DeFaria, Nov 3, 2009
    #11
  12. body { font: Helvetica, Arial, sans-serif; } p { font: Helvetica, Arial, sans-serif; } ..standout { font-family: verdana, arial, sans-serif; font-size: 12px; color: #993333; line-height: 13px; font-weight: bold; margin-bottom: 10px; } ..code { border-top: 1px solid #ddd; border-left: 1px solid #ddd; border-right: 2px solid #000; border-bottom: 2px solid #000; padding: 10px; margin-top: 5px; margin-left: 5%; margin-right: 5%; background: #ffffea; color: black; -moz-border-radius: 10px; } ..codedark { border-top: 10px solid #03f; border-left: 1px solid #ddd; border-right: 2px solid grey; border-bottom: 2px solid grey; padding: 10px; margin-top: 5px; margin-left: 5%; margin-right: 5%; background: black; color: yellow; -moz-border-radius: 10px; } #code { color: black; font-size: 14px; font-family: courier; padding-left: 5px; } #line-number { color: #804000; font-family: Arial; font-size: 14px; padding-right: 5px; border-right: 1px dotted #804000; } blockquote[type=cite] { padding: 0em .5em .5em .5em !important; border-right: 2px solid blue !important; border-left: 2px solid blue !important; } blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid maroon !important; border-left: 2px solid maroon !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid teal !important; border-left: 2px solid teal !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid purple !important; border-left: 2px solid purple !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid green !important; border-left: 2px solid green !important; } a:link { color: blue; } a:visited { color: darkblue; } a:hover { color: black; background-color: #ffffcc; text-decoration: underline; } a:active { color: red; } On 11/03/2009 11:44 AM, Sherm Pendley wrote:
    Some *providers* even filter him, although I doubt it's personal; they are probably just filtering broken HTML-only articles that lack the correct MIME type header.
    Everybody's free to do what they want.
    I use x-privat.org, for example, and I didn't even know he was still posting here until now. Ignorance is indeed bliss in this case. :)
    I haven't posted here in a while. Your assumption is incorrect as usual. Part of ignorance is the willful ignoring of the facts. You seem to have this down cold.


    --
    Andrew DeFaria
    Calling atheism a religion is like calling baldness a hair color.
     
    Andrew DeFaria, Nov 3, 2009
    #12
  13. John

    Justin C Guest

    On 2009-11-03, Jürgen Exner <> wrote:
    > Justin C <> wrote:
    >>On 2009-11-01, Andrew DeFaria <> wrote:
    >>><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    >>> my $value ||= $PAR{$name}<br>

    >>
    >>Why do you find it necessary to post over 100 lines of html just to
    >>reply with one line? We understand your meaning by reading the text
    >>that you post, not the code that your software generates. Please do
    >>not post html here, plain-text only in this newsgroup.

    >
    > This has been discussed ad nauseum a while ago. Mr. DeFaria is resistant
    > to any attempt of reasoning and therefore ended up in most peoples
    > filters.
    > You must have missed that thread.


    Yes, I must have done. I shall make a note and not chastise the deviant
    again.

    Justin.

    --
    Justin C, by the sea.
     
    Justin C, Nov 4, 2009
    #13
    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. Mike Treseler

    Re: Quartus warning in NUMERIC_STD.vhd

    Mike Treseler, Jul 11, 2003, in forum: VHDL
    Replies:
    2
    Views:
    1,888
    Clyde R. Shappee
    Jul 13, 2003
  2. Pete Becker
    Replies:
    0
    Views:
    1,406
    Pete Becker
    Feb 10, 2005
  3. B. Williams

    warning C4267 and warning C4996

    B. Williams, Oct 26, 2006, in forum: C++
    Replies:
    17
    Views:
    2,678
  4. WARNING! Prosoftstore.com is a SCAM! WARNING!

    , Jul 8, 2007, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    348
  5. Julian Mehnle
    Replies:
    17
    Views:
    959
    Julian Mehnle
    May 18, 2006
Loading...

Share This Page