Joe Smith on Tue, 25 Apr 2000 19:01:02 -0400 (EDT)


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Perl 5.6 question


On Tue, Apr 25, 2000 at 03:58:58PM -0400, mjd-perl-pm@plover.com wrote:
> 
> > Perl 5.6 question: Why would you want to use an lvalue sub?
> 
> Suppose you have an object, $car.  $car has a piece of member data
> called `color'.
> 
>         $the_color = $car->color();
> 
> This gets the car's color.
> 
>         $car->color() = 'blue';
> 
> This sets the car's color to blue.

Duh!  Now I get it.  Much nicer to read than: $car->color('blue').
Too bad this example wasn't in the 'What's New' article that came out
last week.

What does this look like in the method?  Is it as simple as

  sub color:lvalue { $_[0]->{'_color'} }

Can subs be marked :'lvalue' dynamically, so AUTOLOAD can whip up such
nifty attribute accessors on-the-fly?

Is it easy to code something that still accepts a new value as an
argument, for compatibility?

  sub color:lvalue { @_ > 1 ? $_[0]->{'_color'} = $_[1] : $_[0]->{'_color'} }

All in all, the 5.6 release looks pretty cool.

BTW, does anyone have a URL that summarizes the C/C++ discussion for
perl 6?  I'm sure it's been discussed to death, but I'd like to feel
more confident that this is a Good Thing for Perl.

<Joe
-- 
 Joe Smith
 jes@presto.med.upenn.edu

**Majordomo list services provided by PANIX <URL:http://www.panix.com>**
**To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**