Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Wrong content of variable passed to sub

by AnomalousMonk (Canon)
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
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

    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?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2015-11-26 15:38 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (701 votes), past polls