Created
May 27, 2015 13:45
-
-
Save stemid/c07f0e80ff2c676f5f2d to your computer and use it in GitHub Desktop.
Simple TSM perl interface made by an old co-worker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
# Oskar Liljeblad 2008-10-17 | |
package TSM; | |
use strict; | |
use Encode; | |
$TSM::dsmadmc = 'dsmadmc'; | |
$TSM::username = 'admin'; | |
$TSM::password = 'secrets'; | |
sub set_database { | |
my ($dsmadmc, $username, $password) = @_; | |
$TSM::dsmadmc = $dsmadmc; | |
$TSM::username = $username; | |
$TSM::password = $password; | |
} | |
sub do_query { | |
my ($query) = @_; | |
open(my $fh, '-|', $TSM::dsmadmc, '-id='.$TSM::username, '-password='.$TSM::password, '-tabdelimited', '-outfile', '-noconfirm', $query) || die "$!\n"; | |
while (<$fh>) { | |
# no operation | |
} | |
close($fh); | |
return ($? == 0); | |
} | |
sub get_query_result ($) { | |
my ($query) = @_; | |
open(my $fh, '-|', $TSM::dsmadmc, '-id='.$TSM::username, '-password='.$TSM::password, '-tabdelimited', '-outfile', '-noconfirm', $query) || die "$!\n"; | |
my @rows = (); | |
my $state = 0; | |
while (<$fh>) { | |
chomp; | |
if ($state < 2) { | |
$state++ if (/^$/); | |
} elsif ($state == 2) { | |
if (/^$/) { | |
$state++; | |
} else { | |
push @rows, $_; | |
} | |
} | |
} | |
close($fh); | |
return ($?, @rows) if $? != 0; | |
return ($?, map { [ split(/\t/, decode('IBM437', $_), -1) ] } @rows); | |
} | |
sub get_query_results ($) { | |
my ($r,@rows) = get_query_result(@_); | |
return $r == 0 ? @rows : (); | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment