NAME

MATT::Utility - Common Perl scripting functions


SYNOPSIS

MATT::Utility is a grouping of frequently used functions I've written.


DESCRIPTION

Just a big hodge podge of useful subs that I use in scripts all over the place. Peruse through, surely you too can find something of use.

IsProcessRunning

Verify if a process is running or not.

        use MATT::Utility;
        IsProcessRunning($process);

InstallMailToaster

        use MATT::Utility;
        InstallMailToaster();

Downloads and installs Mail::Toaster.

files_diff

        use MATT::Utility;
        files_diff($file1, $file2, $type, $debug);
        if ( files_diff("foo", "bar") ) 
        { 
                print "different!\n"; 
        };

Determine if the files are different. $type is assumed to be text unless you set it otherwise. For anthing but text files, we do a MD5 checksum on the files to determine if they're different or not.

YesOrNo

        YesOrNo ($question)
        use MATT::Utility;
        my $r = YesOrNo("Would you like fries with that?");
        print "yes" if $r;
        Passing $question is optional.
        $r is a scalar

StripLastDirFromPath

        StripLastDirFromPath ($dir)

Takes a path like ``/usr/home/matt'' and returns ``/usr/home'' and ``matt''

You (and I) should be using File::Basename instead as it's more portable.

WriteFile

        WriteFile ($file, @lines)
        use MATT::Utility;
        my $file = "/tmp/foo";
        my @lines = "1", "2", "3";
        print "success" if (WriteFile($file, @lines));

$file is the file you want to write to @lines is a an array, each array element is a line in the file

A result code is returned, undef is failure, anything else is success.

ReadFile

        use MATT::Utility;
        my @lines = ReadFile($file)

Reads in a file, and returns an array with the files contents, one line per array element. All lines in array have already been chomped.

FindTheBin

        use MATT::Utility;
        my $r = FindTheBin($bin, $dir)

Check all the ``normal'' locations for a binary that should be on the system and returns the full path to the binary. Return zero if we can't find it.

If the optional $dir is sent, then check that directory first and if the binary exists there, returns it's full path.

SysCmd

        use MATT::Utility;
        my $r = SysCmd ($cmd)
        print "SysCmd: error result: $r\n" if ($r != 0);

Just a little wrapper around system calls, that returns any failure codes and prints out the error(s) if present.

ArchiveFile

        ArchiveFile ($file)

Make a backup copy of a file by copying the file to $file.timestamp.

GetDirFiles

        GetDirFiles($dir)

$dir is a directory. The return will be an array of files names contained in that directory.

CleanTmpDir

        CleanTmpDir ($dir)

$dir is a directory. Running this will empty it. Be careful!

DeleteFile

        use MATT::Utility;
        DeleteFile($file, $warn);

GetTheDate

        GetTheDate ($bump, $debug)

$bump is the optional offset (in seconds) to subtract from the date.

        use MATT::Utility;
        my ($dd, $mm, $yy, $lm, $hh, $mn, $ss) = GetTheDate();
        $dd = day
        $mm = month
        $yy = year
        $lm = last month
        $hh = hours
        $mn = minutes
        $ss = seconds

FetchFile

        use MATT::Utility;
        FetchFile($url);

Use an appropriate URL fetching utility (fetch, curl, wget, etc) based on your OS to download a file from the $url handed to us. Return a result code of 1 for success, 0 is failure.

FileAppend

        FileAppend ($file, @lines)

Pass this sub a filename and an array and it'll append the array to the file. It's that simple.

LogFileAppend

        LogFileAppend ($file, @lines)

Pass this sub a filename and an array and it'll append a timestamp and the array contents to the file. It's that simple.

ParseConfigFile

        use MATT::Utility;
        my $vals = ParseConfigFile ( "example.conf", 1);
        ParseConfigFile ($file, $debug, $etcdir)

$file is the file to be parsed. $etcdir is where the file should be found. It defaults to /usr/local/etc and will also check the current working directory.

A hashref is returned with the key/value pairs.

SudoSetup

        use MATT::Utility;
        my $sudo = SudoSetup();
        $r = SysCmd("$sudo mv /tmp/foo /etc/root-owned-file");

Often you want to run a script as an unprivileged user. However, the script may need elevated privileges for a plethora of reasons. Rather than running the script suid, or as root, configure sudo allowing the script to run system commands with appropriate permissions.


AUTHOR

        Matt Simerson <matt@cadillac.net>


BUGS

        None known. Report any to author.


TODO


SEE ALSO

        http://www.tnpi.biz/computing/perl/
        http://www.tnpi.biz/internet/
    MATT::Apache, MATT::DNS, MATT::FreeBSD, MATT::Mysql,
    MATT::Passwd, MATT::Perl, MATT::Qmail, MATT::Quota
    MATT::SSL, MATT::Utility


COPYRIGHT

Copyright 2003, The Network People, Inc. All Rights Reserved.