Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to make a function lvalue?

Posted on 2001-08-02
5
Medium Priority
?
236 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
ID: 6344633
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
ID: 6344662
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
ID: 6346171
0
 
LVL 6

Author Comment

by:holli
ID: 6346176
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 300 total points
ID: 6348000
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Six Sigma Control Plans

926 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