• Home
  • Services
    • Consultancy
    • Custom Software Solutions
    • Systems Adminsitration
    • SCM
      • Clearcase
      • Multisite
      • Clearquest
      • Release Managment
      • CVS
    • Web Applications
    • Customers
  • Publications
    • Clearcase
      • Triggers
      • Open Source Builds
    • Clearquest
      • Clearquest Daemon
      • DB Conversions
    • Systems Admin
      • Unix/Linux
      • Windows
    • Scripting
      • Perl
      • PHP
      • ECRDig
  • About
    • Services
    • Our People
    • Our Philosophy
    • Contact Us
 

ClearSCM Inc.

You are viewing an unstyled version of this page. Either your browser does not support Cascading Style Sheets (CSS) or CSS styling has been disabled.

NAME Utils.pm

Utils - Simple and often used utilities

VERSION

Author

Andrew DeFaria <Andrew@ClearSCM.com>

Revision

Revision 1.7

Created

Thu Jan 5 15:15:29 PST 2006

Modified

Modifed 2010/05/22 02:43:06

SYNOPSIS

This module seeks to encapsulate useful utilities, things that are often done over and over again but who's classification is miscellaneous.

  EnterDaemonMode

  my @children = GetChildren ($pid);

  my @lines = ReadFile ("/tmp/file");

  print "Found foo!\n" if InArray ("foo", @bar);

  my ($status, @output) = Execute ("ps -ef");

DESCRIPTION

A collection of utility type subroutines.

ROUTINES

The following routines are exported:

EnterDaemonMode ($logfile, $errorlog)

There is a right way to enter ``daemon mode'' and this routine is for that. If you call EnterDaemonMode your process will be disassociated from the terminal and enter into a background mode just like a good daemon.

Parameters:

$logfile

File name of where to redirect STDOUT for the daemon (Default: $null)

$errorlog

File name of where to redirect STDERR for the daemon (Default: $null)

Returns:

Doesn't return

Execute ($command)

We all execute OS commands and then have to deal with the output and return codes and the like. How about an easy Execute subroutine. It takes one parameter, the command to execute, executes it and returns two parameters, the output in a nice chomped array and the status.

Parameters:

$command

Command to execute

Returns:

A status scalar and an array of lines output from the command (if any).

Note, no redirection of STDERR is included. If you want STDERR included in STDOUT then do so in the $command passed in.

GetChildren ($pid)

Returns an array of children pids for the passed in $pid.

NOTE: This assumes that the utility pstree exists and is in the callers PATH.

Parameters:

$pid

$pid to return the subtree of (Default: pid of init)

Returns:

Array of children pids

InArray ($item, @array)

Find an item in an array.

Parameters:

$item

Item to search for

@array

Array to search

Returns:

$true if found - $false otherwise

ReadFile ($filename)

How many times have you coded a Perl subroutine, or just staight inline Perl to open a file, read all the lines into an array and close the file. This routine does that very thing along with the associated and proper checking of open failure and even trims the lines in the output array of trailing newlines? This routine returns an array of the lines in the filename passed in.

Parameters:

$filename

Filename to read

Returns:

Array of lines in the file

Stats ($total, $log)

Reports runtime stats

Parameters:

$total

Reference to a hash of total counters. The keys of the hash will be the labels and the values of the hash will be the counters.

$log

Logger object to log stats to (if specified)

Returns:

Nothing

Usage ($msg)

Reports usage using perldoc

Parameters:

$msg

Message to output before doing perldoc

Returns:

Does not return

CONFIGURATION AND ENVIRONMENT None

DEPENDENCIES

 POSIX

 OSDep
 Display
 Logger

INCOMPATABILITIES

None yet...

BUGS AND LIMITATIONS

There are no known bugs in this module.

Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.

LICENSE AND COPYRIGHT

This Perl Module is freely available; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This Perl Module is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License (http://www.gnu.org/copyleft/gpl.html) for more details.

You should have received a copy of the GNU General Public License along with this Perl Module; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. reserved.