Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.perl.misc > #4801

Re: Embedding code in qq{}?

From tmcd@panix.com (Tim McDaniel)
Newsgroups comp.lang.perl.misc
Subject Re: Embedding code in qq{}?
Date 2012-03-19 02:21 +0000
Organization Tim McDaniel's at Panix
Message-ID <jk657m$383$1@reader1.panix.com> (permalink)
References <87limyk471.fsf@Gmail.com> <1fjg39-qnv2.ln1@anubis.morrow.me.uk> <jk5nht$ot8$1@reader1.panix.com> <86fwd5wl9d.fsf@red.stonehenge.com>

Show all headers | View raw


In article <86fwd5wl9d.fsf@red.stonehenge.com>,
Randal L. Schwartz <merlyn@stonehenge.com> wrote:
>>>>>> "Tim" == Tim McDaniel <tmcd@panix.com> writes:
>
>Tim> print "scalar: ", ${tester()}, "\n";
>
>I'm amazed this works.

Why?  The basic syntax for a scalar variable is $identifier, and man
perlref (Perl 5.10):

"Anywhere you'd put an identifier (or chain of identifiers) as part of
a variable or subroutine name, you can replace the identifier with a
BLOCK returning a reference of the correct type."

So replace "identifier" with a BLOCK.  The BLOCK is
    {tester()}
That sub returns the required reference to a scalar.  It's no more
surprising than
    ${\(22)}
which is what it returns.

>Tim> print "array: ", @{tester()}, "\n";
>
>You forgot the square brackets.

No, I didn't.  They are not required -- just a BLOCK that returns a
reference to an array.  Of course, square brackets are a convenient
way to generate such a reference.  Had tester been called in an array
context (which, as I noted, in retrospect would make no sense), it
would have returned [ 11, 12, 13 ].

I just ran this in Perl 5.10:

#! /usr/bin/perl
use strict;
use warnings;
sub poit { return [ 11, 22, 33]; }
sub pondering { my @a = (44, 55, 66); return \@a; }
print join(' narf ', @{poit();}), "\n";
print join(' zort ', @{pondering();}), "\n";
exit 0;

Output:
$ perl local/test/090.pl
11 narf 22 narf 33
44 zort 55 zort 66

In the "pondering" case, nary a square bracket in sight.

-- 
Tim McDaniel, tmcd@panix.com

Back to comp.lang.perl.misc | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Embedding code in qq{}? XeCycle <XeCycle@Gmail.com> - 2012-03-18 10:56 +0800
  Re: Embedding code in qq{}? Uri Guttman <uri@stemsystems.com> - 2012-03-17 23:04 -0400
    Re: Embedding code in qq{}? XeCycle <XeCycle@Gmail.com> - 2012-03-18 12:14 +0800
  Re: Embedding code in qq{}? tmcd@panix.com (Tim McDaniel) - 2012-03-18 07:07 +0000
    Re: Embedding code in qq{}? Ben Morrow <ben@morrow.me.uk> - 2012-03-18 08:37 +0000
      Re: Embedding code in qq{}? tmcd@panix.com (Tim McDaniel) - 2012-03-18 22:28 +0000
        Re: Embedding code in qq{}? merlyn@stonehenge.com (Randal L. Schwartz) - 2012-03-18 16:18 -0700
          Re: Embedding code in qq{}? tmcd@panix.com (Tim McDaniel) - 2012-03-19 02:21 +0000
            Re: Embedding code in qq{}? Uri Guttman <uri@stemsystems.com> - 2012-03-19 00:14 -0400
              Re: Embedding code in qq{}? tmcd@panix.com (Tim McDaniel) - 2012-03-19 06:42 +0000
                Re: Embedding code in qq{}? Ben Morrow <ben@morrow.me.uk> - 2012-03-19 16:40 +0000
        Re: Embedding code in qq{}? Uri Guttman <uri@stemsystems.com> - 2012-03-18 19:29 -0400
    Re: Embedding code in qq{}? Uri Guttman <uri@stemsystems.com> - 2012-03-18 15:24 -0400
      Re: Embedding code in qq{}? tmcd@panix.com (Tim McDaniel) - 2012-03-18 22:33 +0000
        Re: Embedding code in qq{}? Uri Guttman <uri@stemsystems.com> - 2012-03-18 19:24 -0400
          Re: Embedding code in qq{}? Ben Morrow <ben@morrow.me.uk> - 2012-03-19 00:49 +0000
            Re: Embedding code in qq{}? Uri Guttman <uri@stemsystems.com> - 2012-03-18 22:05 -0400
          Re: Embedding code in qq{}? tmcd@panix.com (Tim McDaniel) - 2012-03-19 02:55 +0000
            Re: Embedding code in qq{}? Uri Guttman <uri@stemsystems.com> - 2012-03-19 00:12 -0400
              Re: Embedding code in qq{}? tmcd@panix.com (Tim McDaniel) - 2012-03-19 07:00 +0000
                Re: Embedding code in qq{}? Ben Morrow <ben@morrow.me.uk> - 2012-03-19 16:43 +0000

csiph-web