[comp.lang.perl] Command substitution or Common novice bug

jand@kuling.UUCP (Jan Dj{rv) (03/27/90)

Should command substitution work inside double quotes?
It sure doesn't for me:

% perl -e 'print "`date` ", `date`, "\n";'
`date` Tue Mar 27 10:50:02 METDST 1990

Is there any reason for this behaviour? As an seasoned shell programmer
I often make this mistake. This is my most common novice bug (I never seem
to learn...) and I'm sure others who program in shell make the same mistake.

Can't perl change in this respect (please).

BTW, perl -v:
$Header: perly.c,v 3.0.1.4 90/02/28 18:06:41 lwall Locked $
Patch level: 15

Copyright (c) 1989, Larry Wall

Perl may be copied only under the terms of the GNU General Public License,
a copy of which can be found with the Perl 3.0 distribution kit.

	Jan D.

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (03/28/90)

In article <1460@kuling.UUCP> jand@kuling.UUCP (Jan Dj{rv) writes:
: Should command substitution work inside double quotes?
: It sure doesn't for me:
: 
: % perl -e 'print "`date` ", `date`, "\n";'
: `date` Tue Mar 27 10:50:02 METDST 1990
: 
: Is there any reason for this behaviour? As an seasoned shell programmer
: I often make this mistake. This is my most common novice bug (I never seem
: to learn...) and I'm sure others who program in shell make the same mistake.
: 
: Can't perl change in this respect (please).

Sorry.  Not only would it break already existing scripts, but I don't like
multiple-substitution-pass semantics.  It's part of what makes shells so
messy.  You'll note that it's already documented under TRAPS that

    The backtick operator does variable interpretation without regard to the
    presence of single quotes in the command.

This is another manifestation of the same thing.  Just because you get into
double quotes or backticks doesn't mean you're in a shell.  It only means
that you can interpolate variables and backslash sequences in the string
so delimited.

The fact that I've selected the features of Perl from many languages you like
implies that I've rejected some features from some of the languages you like.

Larry

chip@tct.uucp (Chip Salzenberg) (03/30/90)

According to jand@kuling.UUCP (Jan Dj{rv):
>Should command substitution work inside double quotes?

Unix shells distinguish between "`command`" and "command" because
sometimes you feel like a nut... er, sometimes you want to keep
spaces, tabs, etc. and sometimes you want to collapse them.  Given
Perl's strength in string manipulation, whitespace collapse is better
written explicitly:

    Shell		Perl
    -----		----
    x="`ls`"		$x = `ls`; chop($x);
    x=`ls`		$x = `ls`; chop($x); $x =~ s/[\s\n]+/ /g;

As for the interpolation in quoted strings, that's easy to simulate:

	sub B {
		local($x);
		$x = `@_`;
		chop($x);
		$x;
	}
	print "Today is " . &B("date") . ", or so Unix claims.\n";

Pack another jack huster,
-- 
Chip Salzenberg at ComDev/TCT   <chip%tct@ateng.com>, <uunet!ateng!tct!chip>
          "The Usenet, in a very real sense, does not exist."