Groups | Search | Server Info | Login | Register


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

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:55 +0000
Organization Tim McDaniel's at Panix
Message-ID <jk677e$9vk$1@reader1.panix.com> (permalink)
References <87limyk471.fsf@Gmail.com> <87ty1lu2z7.fsf@stemsystems.com> <jk5nsj$ot8$2@reader1.panix.com> <87pqc9trvt.fsf@stemsystems.com>

Show all headers | View raw


In article <87pqc9trvt.fsf@stemsystems.com>,
Uri Guttman  <uri@stemsystems.com> wrote:
>>>>>> "TM" == Tim McDaniel <tmcd@panix.com> writes:
>
>  TM> In article <87ty1lu2z7.fsf@stemsystems.com>,
>  TM> Uri Guttman  <uri@stemsystems.com> wrote:
>  >> bad answer.  first off it isn't needed as the /e modifier to
>  >> s/// does it.
>
>  TM> Which I tried to make clear but didn't.  I wasn't trying to
>  TM> address the s/// case; everyone who pointed out s///e covered
>  TM> it quite nicely.  I was trying to cover the case of being
>  TM> outside the RHS of s///.
>
>but the OP was only really asking about s///.

I'm not allowed to bring up new stuff?  I'm only allowed to address
the one case that someone asks about and not tangent off onto
something else?

And in any event, it works on the right-hand side of s///:

#! /usr/bin/perl
use strict;
use warnings;
$_ = 'abcde';
s/c/ before ${my $q = 23; \(9+$q)} after /;
print "$_\n";
exit 0;

Output:

$ perl local/test/092.pl
ab before 32 after de

>  TM> It does help clarify for me what ${...} means, that ... is
>  TM> simply a block that returns a value.
>
>${foo} is not such a thing.

To repeat again, man perlref says it is.

    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.

>the more i think about it, it isn't a true block in general. it is a
>dereference operation that allows statements. you can't last out of
>it

"Being able to last out of it" is not Perl's definition of block.
man perlsyn says

    In Perl, a sequence of statements that defines a scope is called a
    block.  Sometimes a block is delimited by the file containing it
    (in the case of a required file, or the program as a whole), and
    sometimes a block is delimited by the extent of a string (in the
    case of an eval).

    But generally, a block is delimited by curly brackets, also known
    as braces.  We will call this syntactic construct a BLOCK.

As I explained before, it's a block just the same as do {...}.
You can't last out of do{...} either, or map or grep, but
perlfunc still explains the syntax as
    do BLOCK
    map BLOCK LIST
    grep BLOCK LIST
or, for that matter,
    if (EXPR) BLOCK
    if (EXPR) BLOCK else BLOCK
    if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK
    sub [NAME] BLOCK
or even the source file itself.  There are all sorts of blocks you
can't last or next in.  Are you saying that the body of a sub isn't a
block?

>(which you can from a bare block).

"last" is specifically a *loop* control structure, and a bare block,
as the perlfunc do docs says

    "do BLOCK" does not count as a loop, so the loop control
    statements "next", "last", or "redo" cannot be used to leave or
    restart the block.  See perlsyn for alternative strategies.

man perlsyn explains:

    A BLOCK by itself (labeled or not) is semantically equivalent to a
    loop that executes once.  Thus you can use any of the loop control
    statements in it to leave or restart the block.  (Note that this
    is NOT true in "eval{}", "sub{}", or contrary to popular belief
    "do{}" blocks, which do NOT count as loops.)  The "continue" block
    is optional.

As for it being limited, as you note and I snipped: There are all
sorts of limitations in various contexts.  The value of a sub is
limited too, if it returns something the caller will die on in
context.

As for it being useful: a general block in $BLOCK or @BLOCK is not in
practice useful, readable, helpful, or whatever.  Still, that's how
it's explained in the documentation.

-- 
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