Solved

how to make a function lvalue?

Posted on 2001-08-02
5
227 Views
Last Modified: 2010-03-05
hi experts.

some functions in perl can be lvalue. eg. substr.

$foo=substr($bar,1,2); #rvalue
substr($bar,1,2)=$foo; #lvalue

how can i mimic this behaviour in a self-written perl-function?
0
Comment
Question by:holli
  • 2
  • 2
5 Comments
 
LVL 8

Expert Comment

by:bebonham
Comment Utility
basically you use the modifier lvalue


something like


@arr=qw/one two three four/;

sub ref : lvalue
{
$pt=shift;
for($i=0;$i<4;$i++)
{
if($pt->[$i]=="three")
{
$po=$i;
}}
$pt->[$po];

}
print $arr[3];
ref(\@arr)="eighteen";
print $arr[3];

0
 
LVL 8

Expert Comment

by:bebonham
Comment Utility
sorry, that was about the worst example I could ever give...

try this:



@arr=qw/one two three four/;

sub ref2 : lvalue
{
$pt=shift;
for($i=0;$i<scalar @$pt;$i++)
{
if($pt->[$i]=~/three/)
{
$po=$i;
}}
$pt->[$po];
}
print $arr[2] . "\n";
ref2(\@arr)="eighteen";
print $arr[2];
0
 
LVL 6

Author Comment

by:holli
Comment Utility
0
 
LVL 6

Author Comment

by:holli
Comment Utility
so i need a global variable?
that's bad. i wanted to implement something like this:

package foo;

sub new { return bless ( {}, shift ); }

sub property : lvalue
{ my $this=shift; my $prop=shift;

  $this->{$prop}=lvalue if lvalue;
  return $this->$prop

}  

1;

----

use foo;

my $f=foo->new();
$f->property("color")="red";
print $f->property("color"); #->red


is there a way to do that?
0
 

Accepted Solution

by:
zhire earned 75 total points
Comment Utility
package PropTest;

sub new { bless {},shift; }
sub property : lvalue {
  my($self,$prop) = @_;

  # lvalue subroutines must return existing thing.
  # so we use reference trick.
  ${\$self->{$prop}};
}

1;

----

use PropTest;

my $f = PropTest->new();
$f->property("color") = "red";
print $f->property("color"); # => "red"

----

This sample works properly.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now