Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- perl - help reworking code to include use of a sub routine
- # Get list of tables
- my @tblist = qx(mysql -u foo-bar -ppassw0rd --database $dbsrc -h $node --port 3306 -ss -e "show tables");
- # Data output
- foreach my $tblist (@tblist)
- {
- my $data = '';
- chomp $tblist;
- #Create file
- my $out_file = "/home/$node-$tblist.$dt.dat";
- open (my $out_fh, '>', $out_file) or die "cannot create $out_file: $!";
- my $dbh = DBI->connect("DBI:mysql:database=$dbsrc;host=$node;port=3306",'foo-bar','passw0rd');
- my $sth = $dbh->prepare("SELECT UUID(), '$node', ab, cd, ef, gh, hi FROM $tblist limit 1");
- $sth->execute();
- while (my($id, $nd,$ab,$cd,$ef,$gh,$hi) = $sth->fetchrow_array() ) {
- $data = $data. "__pk__^A$id^E1^A$nd^E2^A$ab^E3^A$cd^E4^A$ef^E5^A$gh^E6^A$hi^E7^D";
- }
- $sth->finish;
- $dbh->disconnect;
- #Create file
- print $out_fh $data;
- close $out_fh or die "Failed to close file: $!";
- };
- my $dt = "2011-02-25";
- my $dbsrc = "...";
- my $node = "...";
- # Get list of tables
- my @tblist = qx(mysql -u foo-bar -ppassw0rd --database $dbsrc -h $node --port 3306 -ss -e "show tables");
- my $dbh = DBI->connect("DBI:mysql:database=$dbsrc;host=$node;port=3306",'foo-bar','passw0rd');
- foreach my $tblist (@tblist)
- {
- # This breaks - chomp is given a list-context
- #extract_data($dbh, chomp($tblist));
- chomp $tblist;
- extract_data($dbh, $tblist);
- };
- $dbh->disconnect;
- sub extract_table
- {
- my($dbh, $tblist) = @_;
- my $out_file = "/home/$node-$tblist.$dt.dat";
- open (my $out_fh, '>', $out_file) or die "cannot create $out_file: $!";
- my $sth = $dbh->prepare("SELECT UUID(), '$node', ab, cd, ef, gh, hi FROM $tblist limit 1");
- $sth->execute();
- while (my($id, $nd,$ab,$cd,$ef,$gh,$hi) = $sth->fetchrow_array() ) {
- print $out_fh "__pk__^A$id^E1^A$nd^E2^A$ab^E3^A$cd^E4^A$ef^E5^A$gh^E6^A$hi^E7^D";
- }
- $sth->finish;
- close $out_fh or die "Failed to close file: $!";
- };
Add Comment
Please, Sign In to add comment