Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Is it ok to assign a popped value to a variable as shown.

by TJPride (Pilgrim)
on Dec 05, 2011 at 13:57 UTC ( #941854=note: print w/ replies, xml ) Need Help??


in reply to Is it ok to assign a popped value to a variable as shown.

This is really more a question of aesthetics and coding practice than whether it works or not. Should "my" just be thrown into the middle of the code, where it's nearest to where the variable is needed, or stated at the top, where it's easier to see all your variables and where the "my" doesn't cause things to potentially not line up? For instance, if you were doing something like this:

my $saved = pop @array; print "Now we have @array and I have saved $saved\n"; $saved = pop @array; print "Now we have @array and I have saved $saved\n";

Personally, I like declaring at the top, especially if a variable is used several times, but others prefer declaring at the point at which the variable is first used. I still haven't figured out which method is really superior.

Here's a few more samples of interesting things you can do with arrays:

Using the standard output variable instead of declaring a new one:

$_ = pop @arr; print "Value popped is $_\n";

Cycling through the array, method 1 (each value is put in standard output variable in turn):
print "Value is $_\n" for @arr;

Cycling through the array, method 2 ($# represents the subscript of the last item in the array):
print "Value is $arr[$_]\n" for 0..$#arr;


Comment on Re: Is it ok to assign a popped value to a variable as shown.
Select or Download Code
Re^2: Is it ok to assign a popped value to a variable as shown.
by GrandFather (Cardinal) on Dec 06, 2011 at 01:01 UTC
    Should "my" just be thrown into the middle of the code, where it's nearest to where the variable is needed

    Absolutely yes! Always declare variables in the smallest possible scope. It makes the life time of the variable clearer and makes it easier to see initialisation. Both are very important for making the intent of code easer to understand.

    "Prettiness" of the code is great for Poetry, but is generally not a large factor in understanding the code and at the end of the day that is what determines how easy it is to write the code correctly and debug it if there are errors.

    On a related note, it is always worth spending the time to find a variable name that is "just right" because the time is really spent thinking about what the variable is for and how it fits the code. For trivial code the time spent is likely to be trivial, and for complicated code the extra time spent finding the right name is well justified. Later the time spent up front is likely to recouped many fold by saving time trying to figure out what the code is doing while validating the code or debugging it.

    True laziness is hard work

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://941854]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2014-07-26 02:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls