Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Re: Is there a shorter, more elegant way to write this deep hashlookup statement

Reply
Thread Tools

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

 
 
Dr.Ruud
Guest
Posts: n/a
 
      02-17-2013
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

 
Reply With Quote
 
 
 
 
Ted Zlatanov
Guest
Posts: n/a
 
      02-21-2013
On Sun, 17 Feb 2013 12:18:47 +0100 "Dr.Ruud" <(E-Mail Removed)> wrote:

R> Or wait for the &-> operator:

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

Oh, that's ugly.

Ted
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Is there a shorter, more elegant way to write this deep hash lookup statement Ted Zlatanov Perl Misc 0 02-21-2013 04:35 PM
Re: Is there a shorter, more elegant way to write this deep hashlookup statement C.DeRykus Perl Misc 0 02-19-2013 07:40 PM
Re: Is there a shorter, more elegant way to write this deep hash lookup statement George Mpouras Perl Misc 0 02-17-2013 12:07 PM
There's got to be a more elegant way Lilith C++ 8 09-02-2005 06:34 AM
Is there a more elegant way to do this? Kamilche Python 7 06-29-2004 04:33 AM



Advertisments