SPUG: Re: for-here; sub interpolation

Doug Beaver dougb at scalar.org
Wed Sep 20 17:47:55 CDT 2000


On Tue, Sep 19, 2000 at 05:06:48PM -0700, El JoPe Magnifico wrote:
> I've long pined for a tab-removal option for perl's for-here operator,
> as is found in the implementation in bash and possibly other shells:
> 
> If the delimiter is preceded by a hyphen, ignore all leading tabs, up
> to and including the delimiter.  I find this to be an enormous help,
> allowing you to avoid interrupting your code indentation.  Sample
> usage, if this were added to perl:
> 
>   $quote = <<-DELIMITER;
>     this text will not be indented
>     when you use it later
>     DELIMITER
> 
> The alternative, which they're still propping up in the third edition
> of the camel book, is cumbersome, inefficient, and still requires you
> to keep your delimiter flush to the left:
> 
>   ($quote = <<DELIMITER) =~ s/^\s+//gm;
>     this text will not be indented
>     when you use it later
> DELIMITER
> 
> Yeah, yeah, I know: Hack it in myself.  Not today.  =) =)

I used to have a perl module called UnQuote, I would do this:

print unquote(<<EOT);
    indented text goes
    here
EOT

When unquote() was called, it would look at the lines of text and figure
out the rightmost column in the text that is still all whitespace and
then it would strip all the whitespace to that column for each line.

print unquote(<<EOT);
    text
      text
     text
    text
EOT

Would become:

text
  text
 text
text

It worked pretty well but it still made you keep the delimiter on the
far left column.  I've since lost the code to it but you could re-create
it pretty easily if you found it useful.

> Speaking of no-brainers, here's an unrelated quoting question:
> 
> Has interpolation of &subroutine_name inside double quotes been added
> recently?  Derhaag's 5.6 build on Solaris apparently provided that
> ability, but I couldn't find any mention of it in the new camel book
> or online, nor could I reproduce with the 5.004 or 5.5 builds I had
> available to me.
> 
> Actually a handy feature, when I thought about it, and natural given
> that $scalar_name and @list_name already interpolate.  Gotta wonder
> why that hasn't been available since day one.  Instead, I end up doing
> @{[subroutine_name]} which isn't too bad, but not particularly
> intuitive.

Perhaps it's just a parsing problem.  I bet it's hard to tell the
difference between a function call with no punctuation and a normal
string, and even if you check to see if the string matches an available
function call, you don't know whether or not they wanted it to be
called.  I guess you could reverse my argument and ask me how perl knows
that "$arr[0]" means 0th element of @arr and not a random sequence of
characters, but I would argue that you can tell from punctuation what
the programmer is expecting perl to do.

There are a few different ways to call the function Func (and I probably
forgot one of them too):

Func;
Func();
&Func;
&Func();

Perhaps it's just too hard to look at the string "this is some text ('not
really')" and figure out whether or not to call text('not really') or to
just leave it alone?

I think it would be cleaner if you only could interpolate basic arrays
and scalars inside of double-quoted strings.  I can never remember if
"$arr->[0]->[0]" is going to work or not, so I use printf or sprintf for
anything worse than $scalar or @array.  Besides, I like lining up the
arguments to printf so they are all pretty:

printf "The method returned something like %s for date %s\n",
       $obj->method_call($date), $date;

Doug

-- 
Smithers: I'm afraid we have a bad image, Sir.  Market research shows
          people see you as somewhat of an ogre.
   Burns: I ought to club them and eat their bones!

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/





More information about the spug-list mailing list