William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

Perl 2

package perl_demo;

# William Jiang's Perl Demo for SOAP Client.

use constant USER => 'test';
use constant PASSWD => 'test';
use constant DB => 'PERL_DB';

# SOAP logfile can't used in private directory, such as /home/william/bin/.
use constant LOGFILE => "/tmp/europa_log";
use constant LOGFILE_BAK => "/tmp/europa_log.bak";
use constant MAXLOGSIZE => 524288; 	#5242880;

my ($user, $passwd) = (USER, PASSWD);

sub connect_database
{
	my $in = shift;

	my $db		= $in->{'database'} || DB;
	my $server = $in->{'dataserver'} || die "Which database server?";
	my $dbimesg = qq{dbi:Sybase:database=$db;server=$server};

	my ($dbh, $fh);
	$dbh = DBI->connect($dbimesg,$user,$passwd,
		{RaiseError=>1,PrintError=>1,AutoCommit=>1}) || die $DBI::errstr;

	# Reseve normal input and error input to a same file.
	# in case of httpd, stdout can't redirect, stderr can.
	if( -e LOGFILE && -w _ ) {

		my $size = -s LOGFILE; # get size of logfile
		if ($size > MAXLOGSIZE) {
			rename(LOGFILE, LOGFILE_BAK) or die "$!";
			$fh = new FileHandle( LOGFILE, 'w') or die "$!";
		}
		else {
			$fh = new FileHandle( LOGFILE, 'a') or die "$!";
		}
	}
	else {
		$fh = new FileHandle( LOGFILE, 'a') or die "$!";
	}

	# redirect stderr into logfile, so SQL's print can be trapped into
	# this log file.
	# stdout can not be trapped, coz httpd uses it. let it there.
	open (STDERR, ">>" . LOGFILE) || die "stderr: $!";

	# stderr needn't autoflush.
	$fh->autoflush( 1 );

	return ($dbh, $fh);
}

sub select_trackingNo
{
	my ($class, $in) = @_;

	my ($dbh,$fh) = connect_database($in);

    #	where	box.SHIPMENT_BOX_OID *= pod.SHIPMENT_BOX_OID
	#	and		pod.POD_DATETIME is null
	my $sql = qq{
		select  distinct box.MPS_TABLE_ID, box.TRKNO, pod.STATUS
        from    MPS box, POD pod
        where	box.MPS_TABLE_ID = pod.MPS_TABLE_ID
		and		pod.STATUS != 2
		and		convert(datetime, substring(box.MPS_TABLE_ID, 1, 8))
				>= (select dateadd(mm, -1, getdate()))
	};
	my $ship_aref = $dbh->selectall_arrayref($sql);

	close($fh);
	return $ship_aref;
}

# do() = prepare-execute-fetch-deallocate
# william uses {AutoCommit}=0 + commit to instead {AutoCommit}=1
# At mean time, proc doesn't use 'begin transaction'.
sub process_sql_statements
{
	my ($class, $in) = @_;
	my ($dbh, $fh) = connect_database($in);
	$dbh->{AutoCommit} = 0;
	$dbh->{RaiseError} = 1;

	eval {
		foreach my $t ( @{$in->{sqls}} ) {
			print $fh $t ."\n";
			$dbh->do( $t );
		}
		$dbh->commit;
	};
	if ($@) {
		print $fh "Transaction aborted because $@:";
		postMail($@ . "  at " . __PACKAGE__ . "[" . __LINE__ . "]");
		$dbh->rollback;
	}
	close($fh);
	$dbh->disconnect;
}

# sendmail for positively control.
sub postMail
{
    my $message = shift || "from soap: encuse.pm";
    my $emailaddr = shift || "william\@dummy.com";

    open (SENDMAIL, "|/usr/lib/sendmail -oi -t -odq")
        or die "Can't sendmail: $!\n";

print SENDMAIL <<"EOF";
From:   william <william\@dummy.com>
To:     $emailaddr
Subject: cci-ext(encuse.pm): dummy Database ECSHIP Report

$message

EOF
close(SENDMAIL) or warn "sendmail didn't close nicely";
}

1;

Advertisements

One response to “Perl 2

  1. powercashadvance.com 10/26/2011 at 5:09 pm

    It truly is rare to encounter an expert in whom you can have some faith. In the world in the present day, nobody genuinely cares about showing others the solution in this subjecttopic. How happy I am to have now found a wonderful web site as this. It truly is people like you exactly who make a true difference nowadays through the strategies they talk about.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: