Solved

how to make a function lvalue?

Posted on 2001-08-02
5
231 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 75 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

821 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