Re: Is there a shorter, more elegant way to write this deep hashlookup statement

Discussion in 'Perl Misc' started by Dr.Ruud, Feb 17, 2013.

  1. Dr.Ruud

    Dr.Ruud Guest

    On 2013-02-17 01:16, Ignoramus329 wrote:

    > As I always say 'use strict; use warnings', I have this statement in my program:
    >
    > $is_freight = 1
    > if $ebay_record
    > && $ebay_record->{ShippingDetails}
    > && $ebay_record->{ShippingDetails}->{ShippingServiceOptions}
    > && $ebay_record->{ShippingDetails}->{ShippingServiceOptions}->{ShippingService}
    > && $ebay_record->{ShippingDetails}->{ShippingServiceOptions}->{ShippingService} eq 'Freight';
    >
    > This is a correct statement and it does not give errors or warnings,
    > however, I would like to know if I can write it in a shorter way.


    That kind of code smells. If you really need such a convoluted
    structure, and are also worried about autovivication (both of which
    probably come from the same basic issue), then what keeps you from
    creating a sub for it? Call it deep_exists() or so.
    You could make it return the hashref or undef, a bit like can().


    Or wait for the &-> operator:

    $is_freight =
    $ebay_row&->{ShippingDetails}&->{ShippingServiceOptions}&->{ShippingService}
    eq 'Freight';

    (with the wild assumption that the shortcutting will even disarm the 'eq')

    --
    Ruud
    Dr.Ruud, Feb 17, 2013
    #1
    1. Advertising

  2. Dr.Ruud

    Ted Zlatanov Guest

    Re: Is there a shorter, more elegant way to write this deep hash lookup statement

    On Sun, 17 Feb 2013 12:18:47 +0100 "Dr.Ruud" <> wrote:

    R> Or wait for the &-> operator:

    R> $is_freight = $ebay_row&->{ShippingDetails}&->{ShippingServiceOptions}&->{ShippingService} eq 'Freight';

    Oh, that's ugly.

    Ted
    Ted Zlatanov, Feb 21, 2013
    #2
    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. Kamilche
    Replies:
    7
    Views:
    282
    Peter Hansen
    Jun 29, 2004
  2. Lilith
    Replies:
    8
    Views:
    347
    Dave Rahardja
    Sep 2, 2005
  3. George Mpouras
    Replies:
    0
    Views:
    154
    George Mpouras
    Feb 17, 2013
  4. C.DeRykus
    Replies:
    0
    Views:
    133
    C.DeRykus
    Feb 19, 2013
  5. Ted Zlatanov
    Replies:
    0
    Views:
    139
    Ted Zlatanov
    Feb 21, 2013
Loading...

Share This Page