SPUG: Do you dream in Perl? (dualvar and contextual return)

Michael R. Wolf MichaelRWolf at att.net
Thu Aug 17 06:57:04 PDT 2006

It's a little past 6 a.m. on the first day of our vacation.  I should
be asleep!!!  That's one of the things vacations are good for, except
my asthma woke me up.

In that semi-lucid transition between sleep and consciousness, I
realized that I was dreaming in Perl.  Not just random ideas, I was
thinking through a problem.  It had something to do with the dual-slot
magic of $!, and wondering if it's magic gets replicated, specifically
by the 'x' operator in a list context, and therefore available for
sprintf.  I guess the test code I had almost written was this.

open my $goofy, '<', q{!@#$$%!$!%#@}
  or warn sprintf "\$! -> numy '%d', stringy '%s'\n", ($!) x 2;

BTW, the answer is: "Yes, the magic is replicated".

My thoughts on that 'magic' had morphed into a question of contextual
return, and eventually to the realization that the basic
DBI::fetchrow_*() commands could all be incorporated into one
DBI::fetchrow() through the use of contextual return, ala

   @fields     = $sth->fetchrow_array();
   $field_aref = $sth->fetchrow_arrayref();
   $field_href = $sth->fetchrow_hashref();

   @fields     = $sth->fetchrow();
   $field_aref = $sth->fetchrow();
   $field_href = $sth->fetchrow();

Hey, pretty cool!!!

I don't always dream in Perl.  Yesterday's asthma-induced dream
remembrance involved snuggling with my honey while flying naked,
realizing we were at our destination, then freefalling into a river,
trying to swim ashore, only to be overtaken by the current and ending
up befriending some hostile natives who told us a shortcut back to
where we needed to be and teaching us how to disable some fierce
alligators with dental floss and pot lids.

I think that my memory the dreams is enhanced by my asthma, which by
it's very nature is slowly dragging me from unconscious breathing and
memory into conscious breathing and memory.

I'll leave the interpretation of the
sex/flying/freefall/swimming/monster dream to other experts, but
apropos of this list, I'm psyched to know some things about the
two-slot magic of $OS_ERROR (aka $!) and its propagation that I didn't
know when I went to sleep.

And the possible rewrite of fetchrow_*() family into a single
fetchrow() function is a cool possibility, too.

I wouldn't recommend asthma as a Perl learning tool, but I still have
the question:  

    Do you dream in Perl?

Dream on,

P.S.  For further reading, I reread Scalar::Util::dualvar and

P.P.S.  I really am on vacation now.  The computer's going down (and
shipped for repairs) while we take a low-tech trip to the Canadian
Rockies.  I'll check replies on our return.

Michael R. Wolf
    All mammals learn by playing!
        MichaelRWolf at att.net

More information about the spug-list mailing list