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

Discussion in 'Perl Misc' started by C.DeRykus, Feb 19, 2013.

  1. C.DeRykus

    C.DeRykus Guest

    On Saturday, February 16, 2013 4:16:33 PM UTC-8, 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.
    >
    >
    >
    > Any suggestions?
    >
    >


    I haven't used it but there's a module providing a
    a pragma to lexically disable autovivifying:

    http://search.cpan.org/~vpit/autovivification-0.11/lib/autovivification.pm

    so, untested, you could do something like this:

    no autovivification;

    $is_freight = 1
    if ebay_record->...->{ShippingService} eq 'Freight';


    and $is_freight would remain undefined unless all levels
    existed.

    Needs a careful read for default behavior and
    CAVEATS section.

    --
    Charles DeRykus
     
    C.DeRykus, Feb 19, 2013
    #1
    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:
    297
    Peter Hansen
    Jun 29, 2004
  2. Lilith
    Replies:
    8
    Views:
    360
    Dave Rahardja
    Sep 2, 2005
  3. Dr.Ruud
    Replies:
    1
    Views:
    176
    Ted Zlatanov
    Feb 21, 2013
  4. George Mpouras
    Replies:
    0
    Views:
    163
    George Mpouras
    Feb 17, 2013
  5. Ted Zlatanov
    Replies:
    0
    Views:
    158
    Ted Zlatanov
    Feb 21, 2013
Loading...

Share This Page