Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

jdavidb (1361)

jdavidb
  (email not shown publicly)
http://voiceofjohn.blogspot.com/

J. David Blackstone has a Bachelor of Science in Computer Science and Engineering and nine years of experience at a wireless telecommunications company, where he learned Perl and never looked back. J. David has an advantage in that he works really hard, he has a passion for writing good software, and he knows many of the world's best Perl programmers.

Journal of jdavidb (1361)

Monday June 17, 2002
01:56 PM

Deficiency

[ #5718 ]

I have a hash of hashes. Slices in this context are notably underrepresented in the Perl literature I've consulted (POD's and Camel).

I've done this before, but always by experimentation. Time has taught me not to depend on something I've only learned by verifying against an implementation.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • $hash{hash} = {five => 5, six => 6, seven => 7};
    @vals = @{$hash{hash}}{qw(five seven)};

    Note, don't make the mistake of

    @vals = $hash{hash}->{qw(five seven)};
    which parses correctly but does not give you what you're looking for.

    I don't think I'd even know about slices if it weren't for the warning you get with @array[$i].

    --
    J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
  • One of my beefs about perl is that it doesn't come with a good interactive environment (unlike python). I frequently test things against an implementation, just to see whether something works or not; I usually go to the documentation afterwords. :-)

    I must say though, that hash slices are one of those things that I nearly always avoid if possible; they're just too visually cluttered.

    • It's not a very good interactive environment, but you can use the debugger with a command like perl5.6.1 -de 1. I do it often, including yesterday when playing with slices.

      I haven't completely decided if I should avoid hash slices or not. I'll agree they seem cluttered. I'll have to wait until I'm maintaining this code three years from now to decide. :) For now I'm using them because they seem less cluttered than the alternative.

      --
      J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
    • I don't understand what you mean. Yes, there is perl -d, as mentioned, and you can always use perl -e itself, too. But not knowing what you mean, I can't say if these are sufficient to your desires.

      Testing is sometimes a problem. For testing Slash, I wrote Slash::Test, which exports the four major objects, Dumper(), the entire API, all the constants, all the available plugin objects, etc. so I can do stuff like:

      % perl -MSlash::Test=useperl -e 'print Dumper $slashdb->getUserFoo($user->{uid})'

      Ever

    • There are a few different interactive environments out there including the perl -dead that folks have mentioned.
      For interactive exploration of complex structures, I've found Data::Walker [cpan.org] to be very useful.