Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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]
[Corion]: Hurr. Yesterday I played around with ffmpeg as a new toy and found its "scene" filter great - it detects scene changes. Now I could write a module that splits a given video on its cuts into different scenes. Except I have no use case for that :)
[Corion]: (and also, writing yet another FFmpeg module just to wrap system() and grep through its output isn't all that great ...)
[erix]: cut out advertisements from movies? :)
[erix]: robably not possible (or it would have been done already)

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (11)
As of 2018-05-24 11:14 GMT
Find Nodes?
    Voting Booth?