tags:

views:

181

answers:

3

I have index.pl and subs.pl. When I run the program, the user inserts the date of birth and then it is passed to the getage() subroutine in subs.pl, which has many subroutines. getage() than implicitly calls another subroutine called validate() which validates the date entered by user.

When I run the index.pl and the user enters the date as 03-04-2005, the following error comes out:

can't modify non-lvalue subroutine call at subs.pl line 85, <> line 1

At 85th line of subs.pl I have:

list(my $val,my @value) = validate($dob);

validate() returns a message and the date($dob) which is sent from getage().

Some code from validate():

sub validate
{
my $dob = shift;
my $error;
my @test;
@test = split("-",$dob);
if (!@test)
{
$error = "date separator should be - ";
return ($error,@test);
}...
+1  A: 

An lvalue is a variable you can modify. (one that can be on the left side of an assignment, hence the name). In most circumstances, a value returned by a sub is not one you can modify.

In your example, you are attempting exactly that: assigning the return value of validate($dob) to the nonmodifiable return value of list($val, @value).

JB
+2  A: 

The solution seems to be:

my ($val, @value) = validate($dob);

based on my intuitive understanding of what that code intended - but I can't be certain till you provide some more context (what does validate() return, what does list() mean?)

If you meant list() as a means to force $val and @value into a list, you merely need to enclose both in parenthesis: ($val, @value) to do that

DVK
FYI `list()` is the PHPism for destructuring list assignment.
Ven'Tatsu
A: 

in line

list(my $val,my @value) = validate($dob);

remove 'list' and it works fine

ie

(my $val,my @value) = validate($dob);

thanks to Kayra and others who answered

dexter