> Huh. So it does... that doesn't look like it's making a reference,
> though. I think I would write that as @{$localtime}{ ... } for
> clarity.
> 

I only got out my "snippy" voice because it will be a snowy day here
when I post example code to the list that hasn't actually been tested.
It does not boost ones reputation as a source of Good Advice to give
non-functioning examples, or suggest breaking known-working code.

As to the clarity question.  To my eyes, I find spurious {} tend to
diminish rather than enhance, especially in common idioms, but I am
wiling to accept it as a question of taste.

Last night, my associate Will remarked, "You are replying to someone
who is confused by simple arrays with an example of a hashref-slice?"

And I responded: "Hash slices (and their reference form) are such a
wonderfully useful tool that they should be taught early and often."

To that end: a humble example... 

------------- begin perl code --------------------------

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper qw(Dumper);

#
# Suppose we want to collect a little information
# about a person
#
my %person; 

## brute force technique
# Gets the job done.  Not very pretty.

$person{first_name} = 'Lawrence';
$person{last_name} = 'Statton';
$person{ocupation} = 'Perl Hacker';
$person{city} = 'Guadalajara';
$person{country} = 'Mexico';


## Slightly easier on the fingers version:

%person = ( first_name => 'Lawrence',
            last_name => 'Statton',
            occupation => 'Perl Hacker',
            city => 'Guadalajara',
            country => 'Mexico' ); 

## Here is the hash slice notation 

@person{ qw / first_name last_name occupation city country / } =
    ( 'Lawrence', 'Statton', 'Perl Hacker', 'Guadalajara', 'Mexico' ); 

## what can I do with that?
## suppose you have a line from a text file of the form

my $line = 'Lawrence:Statton:Perl Hacker:Guadalajara:Mexico';
@person{ qw / first_name last_name occupation city country/ } = split(':', 
$line ); 

##
## NOW - let's do that whole thing, instead of using a hash (%person) we'll 
## use a hashref $person
##

my $person; 

$person->{first_name} = 'Lawrence';
$person->{last_name} = 'Statton';
$person->{ocupation} = 'Perl Hacker';
$person->{city} = 'Guadalajara';
$person->{country} = 'Mexico';

# or ... 

%$person = ( first_name => 'Lawrence',
             last_name => 'Statton',
             occupation => 'Perl Hacker',
             city => 'Guadalajara',
             country => 'Mexico' );

# even better ... 

$person = { first_name => 'Lawrence',
            last_name => 'Statton',
            occupation => 'Perl Hacker',
            city => 'Guadalajara',
            country => 'Mexico' };

## now, combining the hashref with the hash-slice notation, we get:

@$person{ qw / first_name last_name occupation city country/ } = split(':', 
$line ); # creates $person = { ... } 

## compare it to a duplicate of the hash-slice --> hash notation

@person{ qw / first_name last_name occupation city country/ } = split(':', 
$line ); # creates %person = ( ... ) 


-------------- end perl code ---------------------------

> > Computer  software  consists of  only  two  components: ones  and
> > zeros, in roughly equal proportions.   All that is required is to
> > sort them into the correct order.
> 
> Heh. I like that.

Thanks.  A friend and I came up with that in a late-night debugging
session in 1985.  I think it was the same sleep-deprivation-induced
stupor that produced the spoonerism 'iso-optilator'.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to