"Thomas A. Lowery" wrote:
> 
> OK Randal, how does this work?  I put it in code and see it WORKS, but my
> brains hurts trying to understand it.
> 
> > $rc = $sth->bind_columns(\@column{qw(one two three four five)});

Step back a bit. 

An array element is $array[0]. 

An array slice is @array[1,2,3,4], for example.

A hash element is $hash{'hi_there'}. I know you don't NEED the quotes,
but they make sense for the purposes of the demo.

A hash slice is @hash{'hi_there','bye_there','over_there'} for example. 

<aside>
As I understand it the reason that a HASH slice is defined with an ARRAY
indicator (@) is that in this context, @ is NOT an array indicator -
it's a LIST indicator. The type of list is defined by the brackets; []
is an array, a list of elements, {} is a hash, a list of paired
elements.
</aside>

Therefore, @column{'one', 'two', 'three', 'four', 'five'} is a hash
slice, which is simply a list of the values at these keys of the hash
%column.

Typing all the quotes is a PITA, so we use the "quote word" operator to
simplify things to

@column{qw(one two three four five)}

Now, according to the docs (http://www.perldoc.com/cpan/DBI.html)

        bind_columns 

          $rc = $sth->bind_columns(@list_of_refs_to_vars_to_bind);

        Calls /bind_col for each column of the SELECT statement. The
bind_columns method will die
        if the number of references does not match the number of fields.

So, in order to get a list_of_refs, you put \ before the list. This does
NOT create a reference to the whole list, as you'd might expect. Rather
the "ref"-ness gets distributed to each element of the list, creating a
list of REFs.

Personally, I think this is a perfect example of the "beauty" of Perl.
Visually, \@column{qw(one two three four five)} LOOKS like a ref of a
list. Bit, I don't think there IS such a thing.

\@foobar IS a ref of an ARRAY.
\%baba is a ref of a HASH.
\(1,2,3,4) is a ref of a LIST. But what's the point of that? If you want
an ARRAY ref, you can do [1,2,3,4]. If you want a hash ref, do
\{1,2,3,4} (or, more clearly, \{1=>2,3=>4}). A ref of a LIST is
ambiguous, as used in this context. Therefore, it can't be used that
way. So, I guess, it was decided that defining this construct as
converting a list to a list of refs would be useful and therefore, it
was done.

<shameless flame-bait>
Now, IMHO, the majority of compsci language police would end up in
straight jackets if they tried to create a BNF for this scenario. Tough.
Perl gets the job done w/o slavish devotion to an academic concept. Let
Gossling, and Guido chew on that! harumph! Perl "just makes sense". It's
as close to a DWIM language as we have today.
</shameless flame-bait>

HTH

Oh, and by the way, just because I think I can explain it, don''t for
one momment belive that I'd EVER dream that one up on my own. :-)

But you shouldn't be surprised that Randal did. Remember, he's the
author of the Schwarzian transform.

-- 
Matthew O. Persico
    
http://www.acecape.com/dsl
AceDSL:The best ADSL in Verizon area

Reply via email to