Solved

how to make a function lvalue?

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

772 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