Phi*_*son 0 html perl slurp file
我想要做的是从另一台服务器获取文件的内容.既然我不和perl一致,也不知道它的mods和函数iv'e就是这样:
my $fileContents;
if( $md5Con =~ m/\.php$/g ) {
my $ftp = Net::FTP->new($DB_ftpserver, Debug => 0) or die "Cannot connect to some.host.name: $@";
$ftp->login($DB_ftpuser, $DB_ftppass) or die "Cannot login ", $ftp->message;
$ftp->get("/" . $root . $webpage, "c:/perlscripts/" . md5_hex($md5Con) . "-code.php") or die $ftp->message;
open FILE, ">>c:/perlscripts/" . md5_hex($md5Con) . "-code.php" or die $!;
$fileContents = <FILE>;
close(FILE);
unlink("c:/perlscripts/" . md5_hex($md5Con) . "-code.php");
$ftp->quit;
}
Run Code Online (Sandbox Code Playgroud)
我认为id做的是从服务器获取文件,放在我的本地机器上,编辑内容,上传到任何地方然后删除临时文件.
但我似乎无法弄清楚如何获取文件的内容;
open FILE, ">>c:/perlscripts/" . md5_hex($md5Con) . "-code.php" or die $!;
$fileContents = <FILE>;
close(FILE);
Run Code Online (Sandbox Code Playgroud)
不断出错;
使用未初始化的值$ fileContents
我猜测它不会返回一个值.
任何帮助非常感谢.
>>>>>>>>>>编辑<<<<<<<<<<
my $fileContents;
if( $md5Con =~ m/\.php$/g ) {
my $ftp = Net::FTP->new($DB_ftpserver, Debug => 0) or die "Cannot connect to some.host.name: $@";
$ftp->login($DB_ftpuser, $DB_ftppass) or die "Cannot login ", $ftp->message;
$ftp->get("/" . $root . $webpage, "c:/perlscripts/" . md5_hex($md5Con) . "-code.php") or die $ftp->message;
my $file = "c:/perlscripts/" . md5_hex($md5Con) . "-code.php";
{
local( $/ ); # undefine the record seperator
open FILE, "<", $file or die "Cannot open:$!\n";
my $fileContents = <FILE>;
#print $fileContents;
my $bodyContents;
my $headContents;
if( $fileContents =~ m/<\s*body[^>]*>.*$/gi ) {
print $0 . $1 . "\n";
$bodyContents = $dbh->quote($1);
}
if( $fileContents =~ m/^.*<\/head>/gi ) {
print $0 . $1 . "\n";
$headContents = $dbh->quote($1);
}
$bodyTable = $dbh->quote($bodyTable);
$headerTable = $dbh->quote($headerTable);
$dbh->do($createBodyTable) or die " error: Couldn't create body table: " . DBI->errstr;
$dbh->do($createHeadTable) or die " error: Couldn't create header table: " . DBI->errstr;
$dbh->do("INSERT INTO $headerTable ( headData, headDataOutput ) VALUES ( $headContents, $headContents )") or die " error: Couldn't connect to database: " . DBI->errstr;
$dbh->do("INSERT INTO $bodyTable ( bodyData, bodyDataOutput ) VALUES ( $bodyContents, $bodyContents )") or die " error: Couldn't connect to database: " . DBI->errstr;
$dbh->do("INSERT INTO page_names (linkFromRoot, linkTrue, page_name, table_name, navigation, location) VALUES ( $linkFromRoot, $linkTrue, $page_name, $table_name, $navigation, $location )") or die " error: Couldn't connect to database: " . DBI->errstr;
unlink("c:/perlscripts/" . md5_hex($md5Con) . "-code.php");
}
$ftp->quit;
}
Run Code Online (Sandbox Code Playgroud)
以上使用print WILL打印整个文件.但是,由于某种原因,两个正则表达式返回false.知道为什么吗?
if( $fileContents =~ m/<\s*body[^>]*>.*$/gi ) {
print $0 . $1 . "\n";
$bodyContents = $dbh->quote($1);
}
if( $fileContents =~ m/^.*<\/head>/gi ) {
print $0 . $1 . "\n";
$headContents = $dbh->quote($1);
}
Run Code Online (Sandbox Code Playgroud)
Gre*_*con 10
这包含在标准发行版附带的Perl FAQ的第5部分中.
您可以使用该Path::Class::File::slurp模块一步完成.
use Path::Class;
$all_of_it = file($filename)->slurp; # entire file in scalar
@all_lines = file($filename)->slurp; # one line per element
Run Code Online (Sandbox Code Playgroud)
处理文件中所有行的习惯Perl方法是一次一行:
open (INPUT, $file) || die "can't open $file: $!";
while (<INPUT>) {
chomp;
# do something with $_
}
close(INPUT) || die "can't close $file: $!";
Run Code Online (Sandbox Code Playgroud)
这比将整个文件作为一个行数组读入内存然后一次处理一个元素要高效得多,这通常是 - 如果不是总是 - 错误的方法.每当你看到有人这样做:
@lines = <INPUT>;
Run Code Online (Sandbox Code Playgroud)
你应该长时间地思考为什么你需要一次装满所有东西.这不是一个可扩展的解决方案.您可能还会发现使用标准Tie::File模块或DB_File模块的$DB_RECNO绑定更有趣,它允许您将数组绑定到文件,以便访问数组实际访问文件中相应行的元素.
您可以将整个文件句柄内容读入标量.
{
local(*INPUT, $/);
open (INPUT, $file) || die "can't open $file: $!";
$var = <INPUT>;
}
Run Code Online (Sandbox Code Playgroud)
这暂时取消了您的记录分隔符,并将在块退出时自动关闭该文件.如果文件已经打开,只需使用:
$var = do { local $/; <INPUT> };
Run Code Online (Sandbox Code Playgroud)
对于普通文件,您也可以使用该read功能.
read( INPUT, $var, -s INPUT );
Run Code Online (Sandbox Code Playgroud)
第三个参数测试INPUT文件句柄上数据的字节大小,并将许多字节读入缓冲区$var.