Error:Modification of a read-only value ....

X

xyoavx

Hi,
I don't understand why the following program displays the above
mentioned error.

#!perl -w
sub total
{
foreach (@_)
{
$_[0]=7;
}
$_[0];
}
print &total(4,5,6,7),"\n";

Running.....
C:\perl-pro>p ex1-p71s
Modification of a read-only value attempted at ex1-p71s line 6.

Please help.
Thanks in advance,
xyoavx
 
A

A. Sinan Unur

Hi,
I don't understand why the following program displays the above
mentioned error.

#!perl -w
sub total
{
foreach (@_)
{
$_[0]=7;
}
$_[0];
}
print &total(4,5,6,7),"\n";

Running.....
C:\perl-pro>p ex1-p71s
Modification of a read-only value attempted at ex1-p71s line 6.

Please help.

You can help yourself by reading the docs yourself:

perldoc perlsyn

Foreach Loops

....

If any element of LIST ...

Sinan
 
J

Jeff Stampes

xyoavx said:
#!perl -w
sub total
{
foreach (@_)
{
$_[0]=7;
}
$_[0];
}
print &total(4,5,6,7),"\n";

Running.....
C:\perl-pro>p ex1-p71s
Modification of a read-only value attempted at ex1-p71s line 6.

Your foreach loop expands into:

4=7
4=7
4=7
4=7

You're confused about @_, and how to access the elements of it. I can't
fathom what you're trying to do with this routine, so I hesitate to
suggest a 'correct' course of action

~Jeff
 
X

xyoavx

Hello Jeff Stampes
Thanks a lot.
Regards,
xyoavx

Jeff said:
xyoavx said:
#!perl -w
sub total
{
foreach (@_)
{
$_[0]=7;
}
$_[0];
}
print &total(4,5,6,7),"\n";

Running.....
C:\perl-pro>p ex1-p71s
Modification of a read-only value attempted at ex1-p71s line 6.

Your foreach loop expands into:

4=7
4=7
4=7
4=7

You're confused about @_, and how to access the elements of it. I can't
fathom what you're trying to do with this routine, so I hesitate to
suggest a 'correct' course of action

~Jeff
 
A

axel

xyoavx said:
Hi,
I don't understand why the following program displays the above
mentioned error.
#!perl -w
sub total
{
foreach (@_)
{
$_[0]=7;
}
$_[0];
}
print &total(4,5,6,7),"\n";
Running.....
C:\perl-pro>p ex1-p71s
Modification of a read-only value attempted at ex1-p71s line 6.

In calling a subroutine, the elements of @_ are
aliases to the the parameters passed to the sub.

Hence any alteration to an element of @_
also changes the actual parameter passed.

You passed a list (4,5,6,7) and are trying
to change the first element of this read-only
list which is not possible.

If you want to change the parameters passed,
first copy @_ and then play with the copy...

e.g.

#!/usr/local/bin/perl

use strict; # These should always be used
use warnings; # unless you have a reason not to

sub total {
my @in = @_; # Here a copy of @_ is made

foreach (@in) {
$in[0]=7; $in[0] can be safely changed
}
$in[0];
}

print total(4,5,6,7),"\n";

=====

output:
7

Axel
 
S

Sherm Pendley

xyoavx said:
I don't understand why the following program displays the above
mentioned error.

#!perl -w
sub total
{
foreach (@_)
{
$_[0]=7;
}
$_[0];
}
print &total(4,5,6,7),"\n";

What everyone else said... and in addition:

You shouldn't use & to call subroutines unless you know precisely what it
does and why you need it. The total() sub has no prototype to bypass and
no reason to be concerned about stack usage, so in this case you don't need
it. So, the above should be

print total(4,5,6,7), "\n";

Have a look at "perldoc perlsub" for more.

sherm--
 
X

xyoavx

Hi Sherm Pendley,
Thank you very much
Regards,
xyoavx

Sherm said:
xyoavx said:
I don't understand why the following program displays the above
mentioned error.

#!perl -w
sub total
{
foreach (@_)
{
$_[0]=7;
}
$_[0];
}
print &total(4,5,6,7),"\n";

What everyone else said... and in addition:

You shouldn't use & to call subroutines unless you know precisely what it
does and why you need it. The total() sub has no prototype to bypass and
no reason to be concerned about stack usage, so in this case you don't need
it. So, the above should be

print total(4,5,6,7), "\n";

Have a look at "perldoc perlsub" for more.

sherm--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top