leonarduk.com

Breadcrumbs

Home Knowledge bank Technical Skills Perl Tips
Perl Tips PDF Print E-mail
Written by Steve Leonard   
Monday, 23 February 2009 12:20

This page will probably end up getting split out.

 

add to include path

BEGIN {
push @INC, ("/home/credprod/apps/CRM/riskMgmt/infra/cgi-bin/");
}



usage
  &CheckVarSet("Description", $value);
sub CheckVarSet
{
    unless($_[1])
      {
        print "$_[0] not set\n";
        &usage;
      }
    print ("$_[0] = $_[1]\n");
}

or

usage
  &CheckVarSet("variable"); # where exists $variable

sub CheckVarSet
{
        my ($field)=@_;
        my ($value) = eval ("\$$field");
        unless($value)
        {
                print "$field not set\n";
                &usage;
        }
        print ("$field = $value\n");
}


#*****************************************************************************
# Open connection with db.
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
use Sybase::DBlib;
Sybase::DBlib::dbmsghandle("Sybase::DBlib::nsql_message_handler");
$Sybase::DBlib::nsql_strip_whitespace = true;
$db=Sybase::DBlib->dblogin($DATE_USER,$DATE_PASSWD,$DATE_SERVER);
$status = $db->dbuse($DATE_DB);
die "Unable to connect to $DATE_SERVER.$DATE_DB\n" if ( $status != 1 );



  eval "\$destdir = \${${report}_LOCATION}";


get filename minus path

$PROG = $0;
$PROG =~ s/\.pl$//;   # remove .pl suffix
@names = split (/\//, $PROG );
$PROG = pop(@names);

 



hash tables

STORING
                if ( exists $BusinessDates{ $datekey } )
                {
                        $BusinessDates{ $datekey } = $BusinessDates{ $datekey } + 1;
                }
                else
                {
                        $BusinessDates{ $datekey } = 1;
                }

FETCHING
                foreach $datekey ( sort keys %BusinessDates )
                {
                        &log_print("        $BusinessDates{$datekey} x $datekey");
                        delete $BusinessDates{$datekey};
                }



sub log
{
        my ($msg) = @_;
        my ($date) = `date`;
        chomp $date;
        $msg = "[$date] $msg";

        #print LOGFILE "$msg\n";
        print "$msg\n";
}



 when prints don't appear to happen till end
For STDOUT:
$| = 1; enable flushing buffer
$| = 0; dissable flushing buffer

For files need to do this:
use IO::Handle;
open (LOGFILE , ">$log_file")  || die "Can't create $log_file!";
LOGFILE->autoflush(1);



Perl cmd line options
use Getopt::Long;

    &GetOptions ("box_name:s",
                "wait_interval:i",
                "mail_to:s",
                "jobs_file:s",
                "final_job:s",
                "check_times",
                "instance:s");

$box_name = $opt_box_name if ($opt_box_name);



redirect cmd line output to perl
open (FROM_BCP, "$cmd 2>&1 |") || die "Can't execute BCP: $!\n";
while( <FROM_BCP> )


use MIME::Entity;

sub send_email
{
        my ($subject)=@_;

        # Create the top-level, and set up the mail headers:
        $top = build MIME::Entity Type     => "multipart/mixed",
                      -To      => $mail_to,
                      -Subject => $subject;
        # Create a html text message
        attach $top Type => "text/html",
        Data => [ "$email_body" ];

        #$top->print(\*STDOUT);

        # Send it
        open MAIL, "| /usr/lib/sendmail -t -i" or die "open: $!";
        $top->print(\*MAIL);
        close MAIL;
}



$db2 = Sybase::DBlib->dblogin ($db_user, $db_password, $db_server, $db_database) ||
           die "Cannot connect to database: USER:$db_user, PWD:$db_password, DB_SERVER:$db_ser
ver, DB:$db_database";
$db2->dbuse ($db_database);

        $db->dbcmd ($sqlcmd);
        $db->dbsqlexec;
        $db->dbresults ();

        while ( @dat = $db->dbnextrow ) {
                $feedHash {$dat [1]} = join ("\t", $feedHash {$dat [1]}, $dat [2]);
        }

$db->dbclose();