我正在尝试分析用Perl编写的CGI文件.我知道在文件A中声明的使用/需要文件B的变量在文件B中可用,只要它是全局的.但请看一下这段代码:
sub makeoper {
%attr = (
PrintError => 0,
RaiseError => 0
);
$dbh=DBI->connect($configs{db_source},$configs{db_user},$configs{db_passw},\%attr) or die "Can not connect to database: $DBI::errstr!\n";
if ($fetched{submit} eq 'start' and !$fetched{savefr} )
{$fetched{savefr}=&get_time_fromdb;
$fetched{saveto}='';
system "mv pool/*.txt pool/arc/";
}
#some more else ifs
$dbh->disconnect or die "Database connection not made: $DBI::errstr";
}
Run Code Online (Sandbox Code Playgroud)
这个$fetched变量取自哪里?该$configsavriable,例如,来自一个配置文件.我搜索了目录中的所有文件,没有$fetched任何地方.在获取数据时它是某种隐式变量吗?如果没有,那么我应该在哪里看?
以防万一,我发布了整个代码.
#!/usr/bin/perl -w
use DBI;
#$ENV { "ORACLE_HOME" } = "/d01/conf/oracle/product/924";
sub printPage(){
&parse_form || exit;
print "Content-type: text/html\n\n";
&makeoper;
#&makeoper;
print "<html><head></head> …Run Code Online (Sandbox Code Playgroud) 我想问一下如何在生成的输出中包含百分比(%)的符号。下面是我的代码。
$Formula = ($valueiii / $valuei) * 100 ;
$Formula = sprintf("%04.2f", $Formula);
$Formula = sprintf("%-12.12s", $Formula); # File Name
push (@data2 , " $valueiv$valuei$valueiii$Formula$valuev$valueii\t\n") ;
$Formula = 0;
*Output generate was at the slop of Use(%)*
DevicePart Total Used Use(%) LIMIT(%)
A 50000 10161 20.32 60%
B 100000 1416 1.42 80%
C 125000 42258 33.81 80%
D 5000 18 0.36 60%
Run Code Online (Sandbox Code Playgroud)
我期望得到 20.32% , 1.42% , 33.81% , 0.36% 。
但是如何将 % 包含到每一列中。
我熟悉调用者函数,程序可以使用它来知道它是从哪里调用的,从哪个文件,包等。有没有一种简单的方法让程序知道调用者的嵌套深度?用例是添加一些调试行,而不必显式计算嵌套级别。
我不是在寻找堆栈跟踪。我试图找出调用函数在另一个函数范围内的嵌套深度。
例如:
sub my_debug {
my ($txt) = @_;
my ($package, $filename, $line) = caller;
# Is there a way to know how deeply nested the caller is?
my $level = ... # How to get this?
print "DEBUG: You are calling from package $package in $filename "
. "line $line nesting level $level: MSG: $txt\n";
}
sub badly_nested {
for my $i ( 1..10 ) {
# 1-level deep
for my $j ( 1 .. 10 …Run Code Online (Sandbox Code Playgroud) 以下是我的代码,请查看.
use strict;
my @people = qw{a b c d e f};
foreach (@people){
print $_,"$people[$_]\n";
}
Run Code Online (Sandbox Code Playgroud)
以下是输出,
[~/perl]$ perl test.pl
aa #why the output of $people[$_] is not same with $_?
ba
ca
da
ea
fa
Run Code Online (Sandbox Code Playgroud)
谢谢你的要求.
当我在perl中读取输入文件时,我得到了以下内容
u_pwrup_control/g_pwrup_bscan_cell[262]_u_pwrup_bscan
Run Code Online (Sandbox Code Playgroud)
现在我想使用regexp在参考文件中找到类似的行.但是当我使用下面的命令时,它不匹配.
while(<INPUT_FILE>){
$k=$_;
##opening ref file in read mode
while(<REF_FILE>)
if ($_ =~ /$k/) {
print $_;
} else {
print $k is not matching;
}
}
}
Run Code Online (Sandbox Code Playgroud)
请告诉我如何匹配[]而不用逃避.