Guest User

Untitled

a guest
Sep 5th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. perl - help reworking code to include use of a sub routine
  2. # Get list of tables
  3. my @tblist = qx(mysql -u foo-bar -ppassw0rd --database $dbsrc -h $node --port 3306 -ss -e "show tables");
  4.  
  5. # Data output
  6. foreach my $tblist (@tblist)
  7. {
  8. my $data = '';
  9. chomp $tblist;
  10.  
  11. #Create file
  12. my $out_file = "/home/$node-$tblist.$dt.dat";
  13. open (my $out_fh, '>', $out_file) or die "cannot create $out_file: $!";
  14.  
  15. my $dbh = DBI->connect("DBI:mysql:database=$dbsrc;host=$node;port=3306",'foo-bar','passw0rd');
  16. my $sth = $dbh->prepare("SELECT UUID(), '$node', ab, cd, ef, gh, hi FROM $tblist limit 1");
  17. $sth->execute();
  18. while (my($id, $nd,$ab,$cd,$ef,$gh,$hi) = $sth->fetchrow_array() ) {
  19. $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";
  20. }
  21. $sth->finish;
  22. $dbh->disconnect;
  23.  
  24. #Create file
  25. print $out_fh $data;
  26. close $out_fh or die "Failed to close file: $!";
  27. };
  28.  
  29. my $dt = "2011-02-25";
  30. my $dbsrc = "...";
  31. my $node = "...";
  32.  
  33. # Get list of tables
  34. my @tblist = qx(mysql -u foo-bar -ppassw0rd --database $dbsrc -h $node --port 3306 -ss -e "show tables");
  35. my $dbh = DBI->connect("DBI:mysql:database=$dbsrc;host=$node;port=3306",'foo-bar','passw0rd');
  36. foreach my $tblist (@tblist)
  37. {
  38. # This breaks - chomp is given a list-context
  39. #extract_data($dbh, chomp($tblist));
  40. chomp $tblist;
  41. extract_data($dbh, $tblist);
  42. };
  43. $dbh->disconnect;
  44.  
  45. sub extract_table
  46. {
  47. my($dbh, $tblist) = @_;
  48.  
  49. my $out_file = "/home/$node-$tblist.$dt.dat";
  50. open (my $out_fh, '>', $out_file) or die "cannot create $out_file: $!";
  51.  
  52. my $sth = $dbh->prepare("SELECT UUID(), '$node', ab, cd, ef, gh, hi FROM $tblist limit 1");
  53. $sth->execute();
  54. while (my($id, $nd,$ab,$cd,$ef,$gh,$hi) = $sth->fetchrow_array() ) {
  55. 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";
  56. }
  57. $sth->finish;
  58.  
  59. close $out_fh or die "Failed to close file: $!";
  60. };
Add Comment
Please, Sign In to add comment