Hi monks!
I'm attempting find all "pl" files in machine,and store to database.below is my code:
#!/usr/bin/perl
#use Getopt::Long;
use strict;
use File::Find;
use DBI qw(:sql_types);
my @search_result;
# find files
find (\&wanted,".");
#save to database.
foreach (@search_result) {
my @file_stat = stat;
save_data(@file_stat,$_);
}
sub wanted {
if(/\.pl$/i) {
push @search_result,$File::Find::name;
}
}
sub save_data {
my $sql = q(insert into file_stat values(?,?,?,?,?,?,?,?,?,?,?,?,?,?))
+;
my $dbh = DBI->connect('dbi:ODBC:test','test','test123',{AutoCommi
+t => 1,RaiseError => 1}) or die "error!!\n";
my $sth = $dbh->prepare($sql);
my $count = 1;
foreach (@_) {
if($count==14) {
$sth->bind_param(14,$_);
}
else {
$sth->bind_param($count,$_,SQL_INTEGER);
}
$count ++;
}
$sth->execute;
}
questions:
1. @search_results will be much large if files is too many.I want to deal with files "streamly",other than push all to array. any suggestion?
2. $count is ugly,have any built-in variable?
Thanks in advance!!!!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.