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

Rhys

by Rhys (Pilgrim)
 | Log in | Create a new user | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | Snippets | 
 | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | 
 | Code | Poetry | Recent Threads | Newest Nodes | Donate | What's New | 

on Jan 10, 2001 at 03:05 UTC ( #50802=user: print w/ replies, xml ) Need Help??

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

Login:
Password
remember me
password reminder
Create A New User

Community Ads
Chatterbox
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users
Others about the Monastery: (29)
zentara
moritz
shmem
kyle
Roy Johnson
jmcnamara
holli
atcroft
FunkyMonk
mr_mischief
herveus
toolic
pfaut
thezip
Eyck
clinton
MidLifeXis
doran
nedals
oko1
Perlbotics
rovf
anothermarcus
SuicideJunkie
SilasTheMonk
perreal
kennethk
ParseException
Basileus
As of 2008-12-02 20:12 GMT
Sections
The Monastery Gates
Seekers of Perl Wisdom
Meditations
PerlMonks Discussion
Categorized Q&A
Tutorials
Obfuscated Code
Perl Poetry
Cool Uses for Perl
Snippets Section
Code Catacombs
Perl News
Information
PerlMonks FAQ
Guide to the Monastery
What's New at PerlMonks
Voting/Experience System
Tutorials
Reviews
Library
Perl FAQs
Other Info Sources
Find Nodes
Nodes You Wrote
Super Search
List Nodes By Users
Newest Nodes
Recently Active Threads
Selected Best Nodes
Best Nodes
Worst Nodes
Saints in our Book
Leftovers
The St. Larry Wall Shrine
Offering Plate
Awards
Craft
Quests
Editor Requests
Buy PerlMonks Gear
PerlMonks Merchandise
Perl Buzz
Perl.com
Perl 5 Wiki
Perl Jobs
Perl Mongers
Planet Perl
Use Perl
Perl Directory
Perl documentation
CPAN
Random Node
Voting Booth

I work...

at home
from the nearest Starbucks
in an office
in a cubicle farm
shoulder to shoulder with my team-mates
wherever there's a free Wifi connection
all of the above
Work? What is this work?
Other

Results (188 votes), past polls