Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

How to modify the actual arguments

by perladdict (Chaplain)
on Jun 17, 2006 at 18:45 UTC ( #555999=perlquestion: print w/replies, xml ) Need Help??
perladdict has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks
#!/user/bin/perl -w upper($1,$2); sub upper { for(@_) { tr/a-z/A-Z/; } }
I am trying to modify the nameless farmal arguments to actual arguments ,the above code is giving an error as "modification to readonly value attempted".so,suggest me so,that i can trap my mistake.

Replies are listed 'Best First'.
Re: How to modify the actual arguments
by Zaxo (Archbishop) on Jun 17, 2006 at 19:01 UTC

    $1 and $2 are read-only, set internally by perl as a result of regex matching. If you call upper() on $foo and $bar, your function will do what you intend.

    You can guard against fatal errors by wrapping in eval{}. You can catch some such errors at compile time with the (\$) prototype, but that wouldn't catch an attempt to modify $1.

    Your function can be made more independent of language by writing it with uc,

    sub upper { eval { $_ = uc for @_; 1; } }

    After Compline,

Re: How to modify the actual arguments
by GrandFather (Sage) on Jun 17, 2006 at 19:07 UTC

    There is something you are not telling us.

    Update: your function is fine, it is your call that is the issue as Zaxo pointed out.

    use strict; use warnings; my $str1 = "This is string 1."; my $str2 = "String 2 this one is."; upper ($str1, $str2); print "$str1 $str2"; sub upper { tr/a-z/A-Z/ for @_; }



    DWIM is Perl's answer to Gödel
Re: How to modify the actual arguments
by sgifford (Prior) on Jun 17, 2006 at 19:44 UTC
    If that's your whole program, I suspect you mean $ARGV[0] and $ARGV[1], which refer to the command-line arguments to your program, and not $1 and $2, which refer to the last two captures from a regular expression match. Making that change, your program doesn't give an error.
      HI Monk, thankx for suggation,i am trying to modify the constants in this program,so,we can't use the subscript on constant items.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://555999]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2018-01-22 15:25 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (235 votes). Check out past polls.