Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Wrong content of variable passed to sub

by AnomalousMonk (Chancellor)
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) "; ... }

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2018-02-25 08:23 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (312 votes). Check out past polls.