Rather than dropping little code bits on you, I HIGHLY recommend that you take a look at
Perl for System Administration. The fourth chapter is titled
User Activity, and at a glance, it seems to have lots of tips pertaining to your question.
Good luck!
Update: Okay, maybe that 'glance' should have been longer... I haven't found much for pacct interfaces in Perl, but
Proc::ProcessTable looks like it may be of some help to you.