我正在尝试使用Perl将数据写入MySQL数据库.但是,当我运行我的脚本时,我收到以下错误:
Can't locate loadable object for module DBD::mysql in @INC (@INC contains: C:/Perl/site/lib C:/Perl/lib .) at LargeLDAPSearch.pl line 10.
我确实有这些"使用"语句和以下代码.这只是一个小摘录,因为脚本在我尝试数据库连接之前工作:
use DBD::mysql;
use strict;
use warnings;
my $query_handle;
my ($platform,$database,$host,$port,$db_user,$pw) = ("mysql","results","localhost","3306","root","mysql123");
my $dsn = "dbi:$platform:$database:$host:$port";
my $connect = DBI->connect($dsn,$db_user,$pw) || die "Could not connect to database";
my $query_insert = "INSERT INTO " . $dbname . "(uid,status,lstpwdset,reset) VALUES (" . $strSAMA . "," . $strAcctControl . "," . $pwLS . "," . $reset . ")";
$query_handle = $connect->prepare($query_insert);
$query_handle->execute();
Run Code Online (Sandbox Code Playgroud)
我已经进入我本地的Perl文件夹,并搜索了lib文件目录.在 …
这是我的问题,我对Perl知之甚少,而且我有这个功能需要修复.
deviceModelMenu()调用此函数时,CLI将显示以下文本:
The following models are available ================================================== 1. 2. Cisco1240 3. Catalyst3750 4. Catalyst3650 5. HP2524
第一项是空的,这是错误的,我需要修复它,显示此菜单的代码片段是:
my $features = shift;
print "=" x 50, "\n";
print "The following models are available\n";
print "=" x 50, "\n";
my $i=1;
foreach (keys %{$features->{features}[0]->{deviceModel}})
{
print "$i. $_ \n";
$i++;
}
Run Code Online (Sandbox Code Playgroud)
如果我添加以下行:
warn Dumper($features->{features}[0]->{deviceModel});
Run Code Online (Sandbox Code Playgroud)
它抛弃了这个:
$VAR1 = {
'deviceModel' => {
'' => {
'cfg' => []
},
'Cisco1240' => {
'cfg' => [
'cisco1240feature.cfg'
]
},
'Catalyst3750' => { … 寻找有关如何删除句子中的句点字符但不删除缩写句点的一些想法.例如
"The N.J. turnpike is long. Today is a beautiful day."
将改为:
"The N.J. turnpike is long Today is a beautiful day"
基本上我要做的就是浏览目录并对所有文件执行操作,在本例中为子searchForErrors.这个子工作.到目前为止我所拥有的是:
sub proccessFiles{
my $path = $ARGV[2];
opendir(DIR, $path) or die "Unable to open $path: $!";
my @files = readdir(DIR);
@files = map{$path . '/' . $_ } @files;
closedir(DIR);
for (@files){
if(-d $_){
process_files($_);
}
else{
searchForErrors;
}
}
}
proccessFiles($path);
Run Code Online (Sandbox Code Playgroud)
任何帮助/建议都会很棒.而且,我是Perl的新手,所以解释越多越好.谢谢!
我一直在玩Perl中的哈希.以下按预期工作:
use strict;
use warnings;
sub cat {
my $statsRef = shift;
my %stats = %$statsRef;
print $stats{"dd"};
$stats{"dd"} = "DDD\n";
print $stats{"dd"};
return ("dd",%stats);
}
my %test;
$test{"dd"} = "OMG OMG\n";
my ($testStr,%output) = cat (\%test);
print $test{"dd"};
print "RETURN IS ".$output{"dd"} . " ORIG IS ". $test{"dd"};
Run Code Online (Sandbox Code Playgroud)
输出是:
OMG OMG
DDD
OMG OMG
RETURN IS DDD
ORIG IS OMG OMG
Run Code Online (Sandbox Code Playgroud)
当我在混合中添加一个数组时,它会出错.
use strict;
use warnings; sub cat {
my $statsRef = shift;
my %stats = %$statsRef;
print $stats{"dd"}; …Run Code Online (Sandbox Code Playgroud) use Text::Diff;
for($count = 0; $count <= 1000; $count++){
my $data_dir="archive/oswiostat/oracleapps.*dat";
my $data_file= `ls -t $data_dir | head -1`;
while($data_file){
print $data_file;
open (DAT,$data_file) || die("Could not open file! $!");
$stats1 = (stat $data_file)[9];
print "Stats: \n";
@raw_data=<DAT>;
close(DAT);
print "Stats1 is :$stats1\n";
sleep(5);
if($stats1 != $stats2){
@diff = diff \@raw_data, $data_file, { STYLE => "Context" };
$stats2 = $stats1;
}
print @diff || die ("Didn't see any updates $!");
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
$ perl client_socket.pl
archive/oswiostat/oracleapps.localdomain_iostat_12.06.28.1500.dat
Stats:
Stats1 is …Run Code Online (Sandbox Code Playgroud) use threads;
use threads::shared;
sub test {
my $s :shared = 22;
my $thread = threads->new(\&thrsub);
$thread->join();
print $s;
}
sub thrsub {
$s = 33;
}
test;
Run Code Online (Sandbox Code Playgroud)
为什么不在线程中共享数据?
奇怪的是,当我在Perl中执行代码时,输出总是出现在命令行的左侧.例如.
admin@admin-machine:~$ perl my_program
1 2 3 4 5 admin@admin-machine:~$
Run Code Online (Sandbox Code Playgroud)
如何让它在一行上显示输出,如下所示?
admin@admin-machine:~$ perl my_program
1 2 3 4 5
admin@admin-machine:~$
Run Code Online (Sandbox Code Playgroud) 我想将几个单行内容移动到一个脚本中.
例如:
perl -i.bak -pE "s/String_ABC/String_XYZ/g" Cities.Txt
perl -i.bak -pE "s/Manhattan/New_England/g" Cities.Txt
Run Code Online (Sandbox Code Playgroud)
以上对我来说效果很好,但代价是两个磁盘I/O操作.
我想将上述逻辑移动到一个脚本中,以便所有替换都在文件打开和编辑一次的情况下完成.
编辑1:根据您的建议,我在一个脚本中编写了这个片段,当从Windows批处理文件调用时,它只是挂起:
#!/usr/bin/perl -i.bak -p Cities.Txt
use strict;
use warnings;
while( <> ){
s/String_ABC/String_XYZ/g;
s/Manhattan/New_England/g;
print;
}
Run Code Online (Sandbox Code Playgroud)
编辑2:好的,所以这是我实施你的建议的方式.奇迹般有效!
批处理文件:
perl -i.bal MyScript.pl Cities.Txt
Run Code Online (Sandbox Code Playgroud)
MyScript.pl
#!/usr/bin/perl
use strict;
use warnings;
while( <> ){
s/String_ABC/String_XYZ/g;
s/Manhattan/New_England/g;
print;
}
Run Code Online (Sandbox Code Playgroud)
非常感谢所有贡献的人.
在Perl 5.20中,我需要在子字符串上运行正则表达式而不将其复制到新字符串中.所以有些相当于$str[$to]C语言.
原因是它在循环中执行,如果它每次都复制字符串,那么结果代码就是O(n^2)没有复制的情况O(n).因此,如果输入2MB字符串,则无法使用.
或者,我将欢迎有关如何重写代码的建议:我需要使用查找表进行搜索和替换.
示例输入
$str: "abcde"
$tbl: {ab=>"xy", bc=>"rq", e=>"a"}
$reg: qr/ab|bc|e/
expected output: xycda
Run Code Online (Sandbox Code Playgroud)
这是我当前的代码,对于短字符串运行良好,但它不适用于大字符串:
#translate $str: if $reg match found, replace the match with a value in $tbl hash that corresponds to the match
sub internalEncode {
my ($str, $tbl, $reg) = @_;
my $res="";
my $prevTo = 0;
my $to = 0;
#the substr($str,$to) makes it slow; in C with 0 terminated strings
#I would need to write here something …Run Code Online (Sandbox Code Playgroud) perl ×10
hash ×2
dbd ×1
dbi ×1
directory ×1
filesystems ×1
linux ×1
mysql ×1
pcre ×1
reference ×1
regex ×1
replace ×1
string ×1
subroutine ×1
substitution ×1
substring ×1
traversal ×1
ubuntu-10.04 ×1