我对正则表达式的概念相当陌生。我了解我在 bash 脚本中使用的基本正则表达式。以下代码片段来自我正在编写的用于自动更新服务器上的 Wordpress 插件的程序。
无论如何,这个概念是这段代码是子程序的一部分,它通过目录中的 .php 文件递归,并尝试从文件,如果模式匹配,则另一个子尝试提取字符“:”后面的值以获取正确的版本号。
$searchpath=$path."/".$plugins[$i];
@files = <$searchpath/*.php>;
print "Search path is ".$searchpath."\n";
OUT: foreach $file (@files)
{
print "Checking alternate php file: ".$file."\n";
open(txt, $file);
while($line = <txt>)
{
for ($line)
{
s/^\s+//;
s/\s+$//;
}
if ( $line =~ /^Version:|^version:|^\* Version:|\sVersion:/ )
{
print "Version found in file ".$file."\n";
$varfound=1;
close(txt);
$ver=&read_extract($file);
print $ver."\n";
$pluginversion[$i]=$ver;
print "Array Num ".$i." Stored plugin name:".$plugins[$i]." Version found ".$ver." Version stored ".$pluginversion[$i]."\n";
last OUT;
}
}
}
问题是我似乎在逻辑上有错误,并且该文件实际上与 " . phpversion() . "\n"; Version stored " 匹配。php版本()。"\n" 用于搜索查询。以我有限的知识,我发现很难理解哪里出了问题,并且渴望得到一些建议。
提及的其他子项包括如下:
sub read_extract
{
my $pl_version="";
open(txt, my $file=$_[0]);
while($line = <txt>)
{
for ($line)
{
s/^\s+//;
s/\s+$//;
}
if ( $line =~ /^Version:|^version:|^\* Version:|\sVersion:/ )
{
$pl_version=&extract_version($line);
}
}
close(txt);
$pl_version;
}
sub extract_version
{
my $line=$_[0];
$string=substr($line,rindex($line, ":")+1);
for ($string)
{
s/^\s+//;
s/\s+$//;
}
$string;
}
如果完全需要我的子程序,我可以包含它。但是我的调试行显示了这一点:
Processing xcloner-backup-and-restore...Search path is /var/www/virtual/joel.co.in/vettathu.com/htdocs/wp-content/plugins/xcloner-backup-and-restore
Checking alternate php file: /var/www/virtual/joel.co.in/vettathu.com/htdocs/wp-content/plugins/xcloner-backup-and-restore/admin.cloner.html.php
Checking alternate php file: /var/www/virtual/joel.co.in/vettathu.com/htdocs/wp-content/plugins/xcloner-backup-and-restore/admin.cloner.php
Checking alternate php file: /var/www/virtual/joel.co.in/vettathu.com/htdocs/wp-content/plugins/xcloner-backup-and-restore/admin.xcloner-backupandrestore.php
Checking alternate php file: /var/www/virtual/joel.co.in/vettathu.com/htdocs/wp-content/plugins/xcloner-backup-and-restore/admin.xcloner.php
Checking alternate php file: /var/www/virtual/joel.co.in/vettathu.com/htdocs/wp-content/plugins/xcloner-backup-and-restore/cloner.config.php
Checking alternate php file: /var/www/virtual/joel.co.in/vettathu.com/htdocs/wp-content/plugins/xcloner-backup-and-restore/cloner.cron.php
Checking alternate php file: /var/www/virtual/joel.co.in/vettathu.com/htdocs/wp-content/plugins/xcloner-backup-and-restore/cloner.functions.php
Version found in file /var/www/virtual/joel.co.in/vettathu.com/htdocs/wp-content/plugins/xcloner-backup-and-restore/cloner.functions.php
" . phpversion() . "\n";
Array Num 26 Stored plugin name:xcloner-backup-and-restore Version found " . phpversion() . "\n"; Version stored " . phpversion() . "\n";
这似乎是错误所在。