![]() |
|
good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Re: SNMP - contby davido (Cardinal) |
on Aug 27, 2004 at 13:56 UTC ( #386372=note: print w/replies, xml ) | Need Help?? |
You've got a lot of scoping problems. The clues others have provided are definately on target, but the problem is that you've got more than one problem. Don't discount their remarks until you've hunted down each and every time that you're declaring the same variable multiple times. You can't just pepper a script with 'my' until strict stops complaining. You have to pay attention to the scope in which variables are declared, and to whether or not one declaration is masking another at broader scope. First, if you are declaring @test1 at a fairly broad scope to be accessible several places thoughout the script, then when you re-declare it at narrower scope and assign to it, that declaration masks the previous. You are ok assigning to it in that narrower scope, but the effects of the assignment vanish when the narrower scope ends. The culprit is this line: my @test1 = @serial;. If anything, it should be @test1=@serial;, so that the assignment is to the outter @test1, not the inner @test1 (these are two different variables, since you declared them twice at different scopes). Later, in an entirely different lexical block you're saying, "print my @test1;". How many times are you going to declare @test1? Because this is the third time, and consequently, a third @test1 (completely unrelated to the other two). It lives in its own lexical scope, and that masks any other @test1's declared at broader lexical scope. Ok, those are the most glaring scoping issues. But they're biggies. Don't just throw 'my' around like that. Declare a variable in the narrowest scope you can get away with, so long as that scope permits the variable to be accessible to all the code that needs it. You're declaring it once in that nice broad scope, but then two more times in narrower scope. The outter @test1 is never receiving a value, and never printing a value. The first inner @test1 is reeiving a value but then it falls out of scope and is lost forever. The secind inner @test1 is being printed, but it never received a value. That's what 'my' is doing for you. Next, don't place calls to the operating system without checking to see that they worked. That means, you should always check for failure in your opens. And if the filehandle is opened for output, check for success in your closes too. See perlopentut for details on that. Even your initialization of Net::SNMP->session() should have its return value checked for success. Otherwise, you'll just never be sure at what point the script is failing. Dave
In Section
Seekers of Perl Wisdom
|
|