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

John W. Krahn krahnj at telus.net
Thu Aug 17 12:31:26 PDT 2006

Michael R. Wolf wrote:
> 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;

Or you could do:

   or warn sprintf "\$! -> numy '%d', stringy '%1\$s'\n", $!;

And use the same variable for both fields.

> 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();

There is no contextual difference between '$field_aref =' and '$field_href ='.
 They are both in scalar context.

use Perl;

More information about the spug-list mailing list