Tim Yohn wrote:
>
> Michael,
>
> On Fri, 2003-07-11 at 11:56, Michael Weber wrote:
> > $line =~ s/reject/$red.reject.$normal/gi ;
> > $line =~ s/ from /$yellow from $normal/gi ;
> > $line =~ s/status/$blue status $normal/gi ;
>
> The following does work however I don't know exactly how valid it is
> (which is why I'm also sending this to the list so they can make their
> comments on it if they have any :-)
>
> $line =~ s/reject/${red}reject${normal}/gi ;
> $line =~ s/from/${yellow}from${normal}/gi ;
> $line =~ s/status/${blue}status${normal}/gi ;
>
> Now, the reason I'm saying it might not be 'valid' is that it does work
> however it was just a technique I stole from bash scripting, never used
> it in a perl context before. Encasing the variable name in {} lets it
> know that the variable name has ended, thus you can place a variable
> name right next to another word and things won't get confused. I tested
> it out and like I said, it does work, just not sure if it's the best way
> to go about it.
Yes it is valid and is documented in perldata.pod
perldoc perldata
[snip]
As in some shells, you can enclose the variable name in
braces to disambiguate it from following alphanumerics.
You must also do this when interpolating a variable into a
string to separate the variable name from a following dou�
ble-colon or an apostrophe, since these would be otherwise
treated as a package separator:
$who = "Larry";
print PASSWD "${who}::0:0:Superuser:/:/bin/perl\n";
print "We use ${who}speak when ${who}'s here.\n";
Without the braces, Perl would have looked for a $whos�
peak, a `$who::0', and a `$who's' variable. The last two
would be the $0 and the $s variables in the (presumably)
non-existent package `who'.
In fact, an identifier within such curlies is forced to be
a string, as is any simple identifier within a hash sub�
script. Neither need quoting. Our earlier example,
`$days{'Feb'}' can be written as `$days{Feb}' and the
quotes will be assumed automatically. But anything more
complicated in the subscript will be interpreted as an
expression.
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]