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

Discussion in 'Perl Misc' started by xyoavx, Dec 5, 2005.

  1. xyoavx

    xyoavx Guest

    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
    xyoavx, Dec 5, 2005
    #1
    1. Advertising

  2. "xyoavx" <> 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 <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Dec 5, 2005
    #2
    1. Advertising

  3. xyoavx

    Jeff Stampes Guest

    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
    Jeff Stampes, Dec 5, 2005
    #3
  4. xyoavx

    xyoavx Guest

    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
    xyoavx, Dec 5, 2005
    #4
  5. xyoavx

    Guest

    xyoavx <> 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
    , Dec 5, 2005
    #5
  6. xyoavx

    xyoavx Guest

    Hi,
    Thak you very much.
    Regards,
    xyoavx
    xyoavx, Dec 5, 2005
    #6
  7. "xyoavx" <> 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
    Sherm Pendley, Dec 5, 2005
    #7
  8. xyoavx

    xyoavx Guest

    Hi Sherm Pendley,
    Thank you very much
    Regards,
    xyoavx

    Sherm Pendley wrote:
    > "xyoavx" <> 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
    xyoavx, Dec 6, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vnssoftware
    Replies:
    4
    Views:
    453
    Dmitry R
    Dec 31, 2003
  2. keithb
    Replies:
    2
    Views:
    7,982
    keithb
    Jun 7, 2006
  3. Phillip Vong

    VS2005 - Run Code only in Read Only mode.

    Phillip Vong, Apr 6, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    311
    Phillip Vong
    Apr 6, 2007
  4. Jerry Preston

    Modification of a read-only value attempted

    Jerry Preston, Feb 15, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    291
    Brian McCauley
    Feb 15, 2005
  5. Tony  Lawrence
    Replies:
    5
    Views:
    192
    Tony Lawrence
    Sep 16, 2006
Loading...

Share This Page