[comp.lang.perl] affectation to $1

stef@zweig.sun (Stephane Payrard) (11/20/90)

using perl level 36 on a Sun 4/110 running SunOS 4.1:

I want to convert to lower case a string I have
matched. I want to do it in place (in $1);
I discover that I can't write to $1;
Is this a bug or a known/justified feature?

demonstrated by the following script.

#! /usr/local/bin/perl
$_='A';
m/(.*)/;
$1 =~ y/A-Z/a-z/;
print "I expect 'a'; I get '$1'\n";
$1='B';
print "I expect 'b'; I get '$1'\n";



        thanks
--
Stephane Payrard -- stef@eng.sun.com -- (415) 336 3726
SMI 2550 Garcia Avenue M/S 10-09  Mountain View CA 94043

                     
                     

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (11/21/90)

In article <STEF.90Nov19220552@zweig.sun> stef@zweig.sun (Stephane Payrard) writes:
: 
: 
: using perl level 36 on a Sun 4/110 running SunOS 4.1:
: 
: I want to convert to lower case a string I have
: matched. I want to do it in place (in $1);
: I discover that I can't write to $1;
: Is this a bug or a known/justified feature?

$1, $2, etc. are readonly currently.  What would it mean to say this?

	$x = 'foo';
	$x =~ /(fo*)/;
	$x = 'bar';
	$1 =~ y/a-z/A-Z/;


: demonstrated by the following script.
: 
: #! /usr/local/bin/perl
: $_='A';
: m/(.*)/;
: $1 =~ y/A-Z/a-z/;
: print "I expect 'a'; I get '$1'\n";
: $1='B';
: print "I expect 'b'; I get '$1'\n";

There are two ways to do this.

    s#(.*)#($tmp = $1) =~ y/A-Z/a-z/, $tmp#e;

or

    /.*/ && substr($_, length($`), length($&)) =~ y/A-Z/a-z/;

Larry