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

Re: Wrong content of variable passed to sub

by AnomalousMonk (Monsignor)
on Aug 20, 2013 at 23:33 UTC ( #1050266=note: print w/ replies, xml ) Need Help??


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) "; ... }


Comment on Re: Wrong content of variable passed to sub
Select or Download Code
Re^2: Wrong content of variable passed to sub
by olafmar (Novice) on Aug 21, 2013 at 20:28 UTC

    I read the article you suggested and I removed the prototypes, thank you very much for linking that article!

    The second issue is minor, since I modified the script just enough to show you the useful part. Actually I was using readpipe, so I had the newline at end and no manual assignment of $_

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2014-07-12 15:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (240 votes), past polls