correct syntax if hash does not exist

Discussion in 'Perl Misc' started by John, Aug 15, 2011.

  1. John

    John Guest

    Hi

    Part of code.

    If hash does not exist make it null.

    Is this the correct syntax?

    my %data; my $hash=\%data;
    ........
    my $error=$hash->{error} or '';

    Regards
    John
    John, Aug 15, 2011
    #1
    1. Advertising

  2. "John" <> writes:
    > Part of code.
    >
    > If hash does not exist make it null.
    >
    > Is this the correct syntax?
    >
    > my %data; my $hash=\%data;
    > .......
    > my $error=$hash->{error} or '';


    It is correct, but more than a little weird: There is no reason to
    declare a hash and assign a reference to that to a scalar variable to
    get a hash reference. Provided that a hash reference is what it
    actually desired,

    my $hash = {};

    or just using $hash as hash references in an lvalue context would be
    sufficient, eg

    my $hash;
    $hash->{key} = 'value';

    Also, for a sufficiently recent version of Perl,

    my $error = $hash->{error} // '';

    might be a better choice because it only uses the empty string in
    place of the value stored in the hash when this value happened to be
    undefined, as opposed to 'something evaluating to false', ie, an empty
    string or a numerical zero.

    Lastly, this smells a lot like trying to obfuscate one's way around
    the "It is undef! Panic in the streets!!" warning somebody has chosen
    to code into Perl for some weird reason. A good solution for the
    'computer prints nonsense when doing X' problem is 'not doing X', in
    this case, not running with warnings enabled.
    Rainer Weikusat, Aug 15, 2011
    #2
    1. Advertising

  3. Rainer Weikusat <> writes:
    > "John" <> writes:
    >> Part of code.


    [...]

    >> my $error=$hash->{error} or '';


    [...]

    > Lastly, this smells a lot like trying to obfuscate one's way around
    > the "It is undef! Panic in the streets!!" warning somebody has chosen
    > to code into Perl for some weird reason.


    As opposed to what I assumed, the assignment dosn't trigger this
    warning.
    Rainer Weikusat, Aug 15, 2011
    #3
  4. John

    John Guest

    "Rainer Weikusat" <> wrote in message
    news:...
    > "John" <> writes:
    >> Part of code.
    >>
    >> If hash does not exist make it null.
    >>
    >> Is this the correct syntax?
    >>
    >> my %data; my $hash=\%data;
    >> .......
    >> my $error=$hash->{error} or '';

    >
    > It is correct, but more than a little weird: There is no reason to
    > declare a hash and assign a reference to that to a scalar variable to
    > get a hash reference. Provided that a hash reference is what it
    > actually desired,
    >
    > my $hash = {};
    >
    > or just using $hash as hash references in an lvalue context would be
    > sufficient, eg
    >
    > my $hash;
    > $hash->{key} = 'value';
    >
    > Also, for a sufficiently recent version of Perl,
    >
    > my $error = $hash->{error} // '';
    >
    > might be a better choice because it only uses the empty string in
    > place of the value stored in the hash when this value happened to be
    > undefined, as opposed to 'something evaluating to false', ie, an empty
    > string or a numerical zero.
    >
    > Lastly, this smells a lot like trying to obfuscate one's way around
    > the "It is undef! Panic in the streets!!" warning somebody has chosen
    > to code into Perl for some weird reason. A good solution for the
    > 'computer prints nonsense when doing X' problem is 'not doing X', in
    > this case, not running with warnings enabled.


    Many thanks.

    Cannot find any reference to //.

    I might as well stick to if (not defined ..

    Regards
    John
    John, Aug 16, 2011
    #4
  5. Rainer Weikusat <> writes:
    > "John" <> writes:
    >> Part of code.
    >>
    >> If hash does not exist make it null.
    >>
    >> Is this the correct syntax?
    >>
    >> my %data; my $hash=\%data;
    >> .......
    >> my $error=$hash->{error} or '';

    >
    > It is correct, but more than a little weird:


    There's also a possible precedence problem here: The precedence of
    'or' is lower than that of =, meaning,

    my $error = $hash->{error} or '';

    equivalent to

    (my $error = $hash->{error}) or '';

    and Perl will quite appropriately warn[*] about that:

    [rw@sapphire]~ $perl -cwe 'my $hash; my $error = $hash->{error} or "";'
    Useless use of a constant in void context at -e line 1.
    -e syntax OK

    What was likely intended would look like this:

    my $error = $hash->{error} || '';
    Rainer Weikusat, Aug 16, 2011
    #5
  6. Rainer Weikusat <> writes:
    > Rainer Weikusat <> writes:


    [...]

    > and Perl will quite appropriately warn[*] about that:


    Forgotten footnote: Not using perl compile-time warnings is IMHO very
    unwise because (as opposed to, say, gcc warnings) they usually point
    at actual problems.
    Rainer Weikusat, Aug 16, 2011
    #6
  7. John

    Jim Gibson Guest

    In article <j2d1e6$4vp$>, John <>
    wrote:

    > "Rainer Weikusat" <> wrote in message
    > news:...


    > >
    > > Also, for a sufficiently recent version of Perl,
    > >
    > > my $error = $hash->{error} // '';
    > >

    >
    > Cannot find any reference to //.
    >
    > I might as well stick to if (not defined ..


    It is new in Perl 5.10. See 'perldoc perlop' and search for "C-style
    Logical Defined-Or".

    The statement

    $x //= $y;

    is equivalent to:

    $x = defined($x) ? $x : $y;

    --
    Jim Gibson
    Jim Gibson, Aug 16, 2011
    #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. Bill Johnson
    Replies:
    0
    Views:
    1,224
    Bill Johnson
    Jul 8, 2005
  2. LT
    Replies:
    7
    Views:
    2,076
    Phlip
    Jul 25, 2004
  3. Bill Johnson

    CS0234 Global does not exist ... but it genuinely does

    Bill Johnson, Jul 8, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    184
    Bill Johnson
    Jul 8, 2005
  4. rp
    Replies:
    1
    Views:
    491
    red floyd
    Nov 10, 2011
  5. mike
    Replies:
    4
    Views:
    92
    Paul Lalli
    Jun 6, 2007
Loading...

Share This Page