http://www.perlmonks.org?node_id=50802
Rhys's user image
User since: Jan 10, 2001 at 03:05 UTC (23 years ago)
Last here: Oct 13, 2014 at 20:06 UTC (9 years ago)
Experience: 769
Level:Pilgrim (8)
Writeups: 72
Location:Columbia, MO
User's localtime: Mar 28, 2024 at 08:39 CDT
Scratchpad: View
For this user:Search nodes
Watch for posts by this user

Tutorial

Check out the Net-SNMP Tutorial I wrote and tell me if there's anything hideous in it. It's here: Using the SNMP module from the Net-SNMP library

Latest Perl Conundrum

Some of these are simple, some quite complex. All of them are real problems I'm trying to solve for one reason or another. (Usually, I'm trying to tighten code from N lines to N/5 lines.)

So here's the latest one. In many of my applications using SNMP, I have to use the structure below over and over, almost verbatim. The general structure is always the same, but certain parts - that I hope are clearly marked below - change to suit the particular task. Anyway, here goes:

$mib = 'someMibObject'; $vb = new SNMP::Varbind([$mib]); LOOP: while ( 1 ) { $var = $sess->getnext($vb); # Get the next instance. if ( $sess->{ErrorNum} ) { # There was an error. Print $sess->{ErrorStr}, then: last LOOP; } if ( $vb->tag ne $mib ) { # This happens when we've read past the end of this # MIB object. last LOOP; } # If we get here, we use $vb->iid or $var or both to do # something useful with the response. }

So the conundrum is whether or not there's a shorter way of doing this. Perhaps with $vb->tag eq $mib in the while test, but that creates other issues.

Another way of doing this that is successful, but actually duplicates code (though is still a bit more succint and doesn't have the dreaded 'while 1') is like this:

$mib = 'someMibObject'; $vb = new SNMP::Varbind([$mib]); # Note the duplicate getnext()'s... LOOP: for ( $var = $sess->getnext($vb) ; ($vb->tag eq $mib) and (not $sess->{ErrorNum}) ; $var = $sess->getnext($vb) ) { # If we get here, we use $vb->iid or $var or both to do # something useful with the response. } if ( $sess->{ErrorNum} ) { # There was an error. Print $sess->{ErrorStr} and/or # croak() or whatever. }

Please /msg if you have any ideas. If they're big, post to your scratchpad? I'm mostly looking for style/efficiency opinions and insight, although if you come up with some genius method that eliminates four lines, I'll engrave your PM home node URL into the credits of all s‎crip‎ts that use your method. :-)

General Stuff About Rhys

Net admin, net security guy, sys admin, Perl hacker, jack of all trades. Perl hacker means 'Perl hacker'. I usually try to just figure it out rather than look it up.

Fortunately, that's just the code, not the algorithm. Design once, code five or six times is the idea. :-)

I'm a Linux fan, a Dead Zone fan, and hope these guys and these other guys are out of business by 2010.

Oh, yeah. I highly recommend this to anybody still looking, although you can't put too much faith in a Web site on the topic. Like anything else, there's a lot of (ahem) discussion on most of the more interesting topics. All I can say is there's way too much arguing.

And it's often worth watching these, if only because every now and then, you learn something about yourself.

By the way, I'm boycotting any store that already has freakin' Christmas decorations up. (Posted 11/3/2003.) That includes Target and JC Penney. Starting early is how the accountants boost their 'Christmas Sales' figures. It's all crap, so I'm not playing.

Picture Explanation

This fat cat is Josephine ('Jo the Cat'). We don't really know much about Jo, except that she's a typical, sorta-neurotic, very opinionated female cat. I picked her up in the mosque parking lot one day last January and have been taking care of her ever since.

Jo likes fighting with Snowplow (picture pending), waking everyone up at 4:00am, and generally forcing her way onto my lap. Laptops, books, food, etc. are not obstacles. I'm convinced she has created and fixed several Perl bugs with her keyboard dancing.

The picture above is Jo in full catness: napping in the warm spot on the carpet on a cold Missouri March afternoon. What's amazing is that she wasn't in the laundry basket off to the bottom right...

Nodes I Think Are Handy

Hundreds of monks have 'handy nodes' sections! This is one of them!

Matching against list of patterns
improve ugly flow control
Generate Random IP Addresses
Getting different things from routers and switches via SNMP
borg's belly (point and stare)
A Strange print() predicament.
How can I create an array of filehandles?
Re: Wassercrats::Improved, Part Deaux