Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
So I have a few global variables declared at the top of my script, call them "red", "blue" and "yellow" for instance, and I have a subroutine that takes in these variables and does some computation and updates them to that value. I made a few variables global because I'm writing a rather large script and these are just the values I need to compute on rather frequently so I thought that'd just be easiest.
so essentially I have...
#!/usr/bin/perl my $red = undef; my $yellow = undef; my $blue = undef; sub do_something { # some computation $_[0] = (some new value); $_[1] = (some new value); $_[2] = (some new value); } ... do_something(color1, color2, color3);
This works just fine, but I would really like to improve readability because I have a few other larger modules of this form (with more arguments) and to be honest I'm not happy with the way it looks. Its just not to clear what $_[0] is or what $_5 is without looking up what the arguments are for that routine.
So what I'd like to do is rename them somehow within the function to make things more easily understandable.
Something like this, but not quite
sub do_something { my ($red, $yellow, $blue) = @_; # some computation $red = (some new value); $yellow = (some new value); $blue= (some new value); }
I realize "my" is lexical scope so its only visible within that block and doing that would just make a copy of the argument value to the new block variable defined and the actual argument variable doesn't get changed/updated, only passed as reference.
I know I can define other variables out of the scope and just assign them inside but that's just not necessary as I'll only be using them once inside some subroutine for clarity's sake. I just need a way to give a name to the passed in values "temporarily".
So, is there a simple way to just assign a temporary "alias" to the global arguments?
I would guess this would be a fairly easy problem for all of you. I'm very much still a perl newb and not much of a programmer so I would be totally gracious if you could give me a quick tip. (I did do a google search but couldn't find a good answer)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: how to "rename" a global argument in subroutine?
by stevieb (Canon) on Aug 11, 2015 at 18:30 UTC | |
by Anonymous Monk on Aug 11, 2015 at 18:46 UTC | |
|
Re: how to "rename" a global argument in subroutine?
by FreeBeerReekingMonk (Deacon) on Aug 11, 2015 at 18:23 UTC | |
by FreeBeerReekingMonk (Deacon) on Aug 11, 2015 at 18:26 UTC | |
by Anonymous Monk on Aug 11, 2015 at 18:48 UTC | |
by Anonymous Monk on Aug 11, 2015 at 18:42 UTC | |
by FreeBeerReekingMonk (Deacon) on Aug 11, 2015 at 18:52 UTC | |
|
Re: how to "rename" a global argument in subroutine?
by RonW (Parson) on Aug 12, 2015 at 17:49 UTC |