Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Error:Modification of a read-only value ....

Reply
Thread Tools

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

 
 
xyoavx
Guest
Posts: n/a
 
      12-05-2005
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

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      12-05-2005
"xyoavx" <(E-Mail Removed)> wrote in news:1133817514.264369.34920
@g47g2000cwa.googlegroups.com:

> 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

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

 
Reply With Quote
 
 
 
 
Jeff Stampes
Guest
Posts: n/a
 
      12-05-2005
xyoavx wrote:
> #!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
 
Reply With Quote
 
xyoavx
Guest
Posts: n/a
 
      12-05-2005
Hello Jeff Stampes
Thanks a lot.
Regards,
xyoavx

Jeff Stampes wrote:
> xyoavx wrote:
> > #!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


 
Reply With Quote
 
axel@white-eagle.invalid.uk
Guest
Posts: n/a
 
      12-05-2005
xyoavx <(E-Mail Removed)> wrote:
> 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


 
Reply With Quote
 
xyoavx
Guest
Posts: n/a
 
      12-05-2005
Hi,
Thak you very much.
Regards,
xyoavx

 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      12-05-2005
"xyoavx" <(E-Mail Removed)> writes:

> 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--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
xyoavx
Guest
Posts: n/a
 
      12-06-2005
Hi Sherm Pendley,
Thank you very much
Regards,
xyoavx

Sherm Pendley wrote:
> "xyoavx" <(E-Mail Removed)> writes:
>
> > 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--
>
> --
> Cocoa programming in Perl: http://camelbones.sourceforge.net
> Hire me! My resume: http://www.dot-app.org


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how this is calculated "BW Util has exceeded the threshold value of 60%,The present value is 62% -" philip007 Cisco 1 01-31-2006 03:09 PM
The value of MCSD: added value to a degree? uv2003 MCSD 4 06-23-2005 05:41 AM
DataView Find method - Error: Expecting 2 value(s) for the key being indexed, but received 3 value(s) Zenobia ASP .Net 2 06-23-2004 05:15 PM
RE: Textbox postback putting value,value =?Utf-8?B?QW5hY2hvc3RpYw==?= ASP .Net 1 06-21-2004 10:50 AM
copying value of DDL in a Datagrid "pre-edit command" to value in "post edit command" San Diego Guy ASP .Net 0 08-07-2003 08:59 PM



Advertisments