我有以下代码:
$headers;
some_sub( %$headers );
Run Code Online (Sandbox Code Playgroud)
当我打电话时,some_sub我得到一个错误:
不能使用未定义的值作为HASH参考...
但类似的代码不会产生错误:
$headers->{ x };
Run Code Online (Sandbox Code Playgroud)
为什么自动复制在第一个示例中的工作方式与在第二个示例中的工作方式不同?
UPD
我注意到@ThisSuitIsBlackNot.我真的问:
为什么我的$ h; $ h - > {foo}的作品和我的$ h; %$ h没有
UPD
真正的代码:
my $email = Email::Simple->create(()
,header => [()
,To => $address
,From => $cnf->{ from }
,Subject => $subject
,'Content-Type' => 'text/html; charset="utf8"'
,%$headers
]
,body => $body
);
Run Code Online (Sandbox Code Playgroud) 我正在使用Perl编写一个小脚本,我很困惑哪个逻辑运算符必须用于比较字符串
示例代码:
if (($app eq "appname1")OR($app eq "appname2")OR($app eq "appname3"))
Run Code Online (Sandbox Code Playgroud)
我必须使用OR(或)||
perl -E 'say for map s/(æ|ø|å)/ {qw(æ ae ø oe å aa)}->{$1}/ger, qw(rød gul blå)'
perl -E 'say for map s/(æ|ø|å)/"".{qw(æ ae ø oe å aa)}->{$1}/ger, qw(rød gul blå)'
Run Code Online (Sandbox Code Playgroud)
上面的第一行给了我,syntax error at -e line 1, near "}->"但第二行给了我roed,gul与blaa预期的一样。这是编译器的弱点,还是有我看不见的某些原因?我测试并在版本5.10、5.22和5.26中获得了此行为。
我有两个目录,每个目录都有一个文件:
$ ls -l "test dir["
-rw-r--r-- 1 root media 0B 11 Dec 16:53 .ignoreme
Run Code Online (Sandbox Code Playgroud)
和
$ ls -l "test [dir]"
-rw-r--r-- 1 root media 0B 11 Dec 16:34 .ignoreme
Run Code Online (Sandbox Code Playgroud)
我正在使用 Perl 来测试“.ignoreme”文件是否存在:
$ perl -e '$d="test dir["; print -e glob qq("$d"/.ignore*); print "\n";'
1
Run Code Online (Sandbox Code Playgroud)
哪个有效,以及
$ perl -e '$d="test [dir]"; print -e glob qq("$d"/.ignore*); print "\n";'
Run Code Online (Sandbox Code Playgroud)
哪个没有。
我猜这与这[]对有关,但我不确定相互作用。有人可以请教我。我也很感谢修复以匹配任何$d包含[].
我必须调试一个旧的 perl 脚本,它显然是以一种我不理解的方式设置 perl 的版本......
: # use perl
eval 'exec perl -S $0 ${1+"$@"}'
if 0;
Run Code Online (Sandbox Code Playgroud)
perl -h说这-S意味着“使用 PATH 环境变量查找程序文件”。 $0是当前程序。我读过这$@意味着“来自最后一个eval命令的 Perl 语法错误消息”。但是他们为什么要加 1 呢?这一切是如何结合在一起的?它在做什么?
我必须调试的部分内容与它选择了我不想要的旧版本 perl 的事实有关。对于其他一切,我们使用#!/usr/bin/env perl它,我怀疑,可能会做同样的事情。我还怀疑我的解决方案可能在于修复$PATH(或防止弄乱它的代码弄乱它)。但我想通过更好地了解它现在如何选择版本来解决这个问题。
谢谢你的帮助 !
我尝试通过向前声明子例程来隐藏基类的方法,例如:
sub xid;
Run Code Online (Sandbox Code Playgroud)
如果我调用这个方法,我会得到预期的错误:
sub xid;
Run Code Online (Sandbox Code Playgroud)
但$tuple->can( 'xid' )仍然返回真值:
DBG> $tuple->xid
ERROR: Undefined subroutine &HyperMouse::Schema::Result::SaldoAnal::xid called at (eval 2346)[/home/kes/perl5/perlbrew/perls/perl-5.24.1/lib/site_perl/5.24.1/DB/Hooks.pm:422] line 8, <STDIN> line 13.
Run Code Online (Sandbox Code Playgroud)
如何隐藏xid方法,因此$tuple->can( 'xid' )返回 false 而不是:
&HyperMouse::Schema::Result::SaldoAnal::xid
Run Code Online (Sandbox Code Playgroud) 我想使用bash而不是sh当我使用该open功能时。例如:
sub run_cmd {
my ($cmd) = @_;
my $fcmd;
print("Run $cmd\n");
open($fcmd, "$cmd |");
while ( my $line = <$fcmd> ) {
print "-> $line";
}
close $fcmd;
}
eval{run_cmd('ps -p $$')};
Run Code Online (Sandbox Code Playgroud)
在这里,输出是:
Run ps -p $$
-> PID TTY TIME CMD
-> 189493 pts/6 00:00:00 sh
Run Code Online (Sandbox Code Playgroud)
我们可以看到sh是默认使用的。
我有一些限制,我需要 (1) 使用 open 函数和 (2) 调用bash而不是sh. 我试图简单地bash在我的命令的开头添加,但它不起作用:
Run ps -p $$
/bin/ps: /bin/ps: cannot execute binary …Run Code Online (Sandbox Code Playgroud) 我在 Perl 中有一个程序,它应该计算元素在数组中出现的次数,如果元素出现的次数是奇数,则打印出该元素的值。
这是我的代码。
#!/usr/bin/perl
use strict;
use warnings;
sub FindOddCount($)
{
my @arraynumber = @_;
my $Even = 0;
my $i = 0;
my $j = 0;
my $array_length = scalar(@_);
for ($i = 0; $i <= $array_length; $i++)
{
my $IntCount = 0;
for ($j = 0; $j <= $array_length; $j++)
{
if ($arraynumber[$i] == $arraynumber[$j])
{
$IntCount++;
print($j);
}
}
$Even = $IntCount % 2;
if ($Even != 0)
{
return $arraynumber[$i];
}
}
if ($Even == …Run Code Online (Sandbox Code Playgroud) 在我的代码库中,我有几个变量“存在于”main命名空间中,我使用的各种模块可以期望总是在 main 中找到(例如,$main::author是对用户散列的引用,$main::dbh是打开的数据库句柄,$main::loader是核心实用程序类的对象,并$main::FORM具有已处理的QUERY_STRING)。例如当程序启动时:
$main::author = &getAuthor;
$main::loader = SAFARI::Loader->new;
use SAFARI::View;
my $view = SAFARI::View->new;
$view->output;
Run Code Online (Sandbox Code Playgroud)
然后当我进入时SAFARI::View::output,我可以调用这些核心变量,例如:
# Display user name:
$output .= 'Hello, ' . $main::author->{'fullName'} . '! Welcome!';
Run Code Online (Sandbox Code Playgroud)
问题:当代码在线程环境中运行时,一个线程可能需要不同的$loader对象或$author与另一个线程不同的登录。更紧迫的是,当然,每个线程都有一个不同的数据库句柄。
我知道我可以在创建对象时传递核心信息,例如View通过添加它需要的参数。但这意味着目前只能引用main命名空间中这些项目的每种类型的对象都必须有一个冗长的参数列表。我试图想出最有效和“安全”的方法来解决这个问题。
我已经考虑创建散列引用具有每个线程内它的所有不同的位,例如$common,其具有$common->{'FORM'},$common->{'loader'},$common->{'author'},等等,然后通过那些作为单个参数到每个对象:
my $common = #Logic to set up this combination of bits for this particular thread.
my $view …Run Code Online (Sandbox Code Playgroud) 我需要使用 .txt 文件,并按文件名中存储的名称和日期进行过滤。
目前我实现了以下目标:
my $dir = "t-files\/";
chdir($dir);
foreach $files (glob('*.txt')) {
($sname) = split(/_/, $files);
#($sdate) = "still under work"
print "\nSwitch Name: $sname - Date: still under work";
}
Run Code Online (Sandbox Code Playgroud)
文件示例名称:"s-ar-ar55g-1_20140911-09.txt" | "s-ar-ar55g-1_20141027-09.txt" |等
使用此脚本我有以下输出:
D:\_perl>test_01.pl
Switch Name: s-ar-ar55g-1 - Date: still under work
Switch Name: s-ar-ar55g-1 - Date: still under work
Switch Name: s-ar-ar55g-1 - Date: still under work
Switch Name: s-ar-ar55g-1 - Date: still under work
Switch Name: s-ar-ar55g-1 - Date: still under work …Run Code Online (Sandbox Code Playgroud)