in reply to Wrong content of variable passed to sub
In addition to the critical problem diagnosed by state-o-dis-array, here are my candidates for things I would do differently:
sub get_sensor_id($) { ... }
Don't use prototypes (the ($) thingy in the above function definition). It didn't help you in the present case, and they probably don't do what you think they do, so why bother? Please see Prototypes in perlsub and Far More than Everything You've Ever Wanted to Know about Prototypes in Perl -- by Tom Christiansen.
sub add_iLO3_data() { $_ = "Memory (a whole buncha stuff) "; chomp; ... }
chomp chomps a newline (in the default case) from the end of the $_ default scalar if no other variable is specified to chomp, as in the code above. But you just finished assigning a string to $_. If you don't want a newline at the end of it, just don't put one there in the first place — as indeed you did not! No point in chomp-ing.
(Same code extract.) Don't use global variables, e.g. $_ in the code above, unless you really know why you're using them. Use of global variables leads to "spooky action at a distance" problems such as the one pointed out by state-o-dis-array. Better (lexical variable, no chomp, no prototype):
sub add_iLO3_data { my $str = "Memory (a whole buncha stuff) "; ... }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Wrong content of variable passed to sub
by olafmar (Novice) on Aug 21, 2013 at 20:28 UTC |