有这个原型的new方法Parse::RecDescent:
sub new ($$$)
{
# code goes here
}
Run Code Online (Sandbox Code Playgroud)
如果我创建这样的对象:
my $parser = Parse::RecDescent->new($grammar);
Run Code Online (Sandbox Code Playgroud)
它会创建一个解析器,该方法将接收2个参数"Parse :: RecDescent"和$ grammar,对吧?如果我尝试创建一个像这样的对象:
Parse::RecDescent::new("Parse::RecDescent",$grammar)
Run Code Online (Sandbox Code Playgroud)
这将失败说"没有足够的Parse :: RecDescent :: new参数",我理解这个消息.我只传递了2个参数.但是,我不明白为什么箭头版本有效.
你可以解释吗?
为什么我们在Perl中使用函数原型?有哪些不同的原型?怎么用?
例子:$$,$@,\@@他们是什么意思?
无法理解为什么函数login all返回的值与传递给它的内容不对应.
以下是我的代码片段
package This_package;
.......
# returned from function that parses post data ($reqparam)
my $thisuser = $$reqparam{"username"};
# escape '@', username is an email
$thisuser =~ s/@/\@/;
my $thisuser_pass = $$reqparam{'password'};
print $thisuser; # ok
print $thisuser_pass; # ok
my $obj = new users;
my $valid_user = $obj->login($thisuser, $thisuser_pass);
.......
package Another_package;
sub new {
my ($class) = @_;
my $self = {
_login => undef,
_create_user => undef,
....
};
bless $self, $class;
return $self;
}
sub …Run Code Online (Sandbox Code Playgroud) 这是一位新生的Perl开发者.我一直在wra my my and and and and ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
我有以下代码(只剩下相关部分),因为其余的工作):
my @arrMissingTids;
@arrMissingTids = %hshTids;
my $missingtid;
foreach $missingtid (@arrMissingTids) {
print "$missingtid\n";
}
Run Code Online (Sandbox Code Playgroud)
这很好用,返回我想要在数组中的值:
500000246,500000235,500000185,500000237,500000227,500000252
Run Code Online (Sandbox Code Playgroud)
但是,当我将它传递给子程序并将其包含在变量名中时,它不提供上面所写的列表,而只是提供数字1.代码如下:
myqry(@arrMissingTids);
sub myqry($) {
my $missingtids = @_;
$sql = "select
i.tid i_tid, i.name i_name …Run Code Online (Sandbox Code Playgroud) 这是一个Perl n00b问题,但我没有找到一个清晰易懂的在线回答.
我有这个代码:
1.sub remCH();
2.#some stuff
3.$line = remCH($line);
4.
5.sub remCH() {
6.$rem = shift;
7.$rem = chomp($rem);
8.return ($rem);
9.}
Run Code Online (Sandbox Code Playgroud)
执行此代码时总是会出现以下错误(第24行将在上面的代码中为第3行):
Too many arguments for main::rem_CRLF at ./make_csv.pl line 24, near "$line)"
Run Code Online (Sandbox Code Playgroud)
根据我的理解,这是因为函数设置为返回一些东西但是当我声明它时,它被声明为"void".
我如何声明如下函数???
sub remCH(string/integer);
Run Code Online (Sandbox Code Playgroud) 我有以下代码,其中我有一个结构($node),它是声明的标量,但似乎是使用的散列:
sub LoadData()
{
#not significant code here
my $node = {
BaseName => "deviceA",
SysDescr => "Example device",
SysObjectId => "SysObjectIdTest",
ManagementIpAddress => "BLABLABLA",
Protocol => "1",
};
$store->AddDeviceData( 1, $node->{BaseName}, $node );
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:正如我们在上面看到的那样声明的$node是散列还是标量?我的意思是,两者之间有区别吗(就行为而言)
my $hash = {
#some foo => "bar" assign here
};
Run Code Online (Sandbox Code Playgroud)
和
my %hash = (
#some foo => "bar" assign here
);
Run Code Online (Sandbox Code Playgroud)
和
my %hash = {
#some foo => "bar" assign here
}
Run Code Online (Sandbox Code Playgroud)
PS:它表现为散列引用,因为 AddDeviceData() 将最后一个参数限制为散列引用。
PSS:也许它与上下文有关;分配给标量的散列意味着分配对散列的引用而不是散列本身的内容,但我不太确定。
将运行时必需数据文件与Perl模块捆绑在一起的"正确"方法是什么,以便模块在使用之前可以读取其内容?
一个简单的例子是这个Dictionary模块,它需要在启动时读取(字,定义)对的列表.
package Reference::Dictionary;
# TODO: This is the Dictionary, which needs to be populated from
# data-file BEFORE calling Lookup!
our %Dictionary;
sub new {
my $class = shift;
return bless {}, $class;
}
sub Lookup {
my ($self,$word) = @_;
return $Dictionary{$word};
}
1;
Run Code Online (Sandbox Code Playgroud)
和一个驱动程序,Main.pl:
use Reference::Dictionary;
my $dictionary = new Reference::Dictionary;
print $dictionary->Lookup("aardvark");
Run Code Online (Sandbox Code Playgroud)
现在,我的目录结构如下所示:
root/
Main.pl
Reference/
Dictionary.pm
Dictionary.txt
Run Code Online (Sandbox Code Playgroud)
我似乎无法让Dictionary.pm在启动时加载Dictionary.txt.我已经尝试了一些方法来实现这一点,例如......
使用BEGIN块:
BEGIN {
open(FP, '<', 'Dictionary.txt') or die "Can't open: $!\n";
while (<FP>) {
chomp;
my ($word, …Run Code Online (Sandbox Code Playgroud)我想将reg-ex的单个捕获作为标量传递给子程序,我该怎样做呢?这是一个例子:
sub myfunc($)
{
my ($value)=@_;
# Do something with $value...
}
# This is the data we want to parse
my $some_var='value: 12345'; # For example
# We want to extract the value '12345' from $some_var
# and pass it to the myfunc subroutine as a scalar
# Attempt #1: This doesn't work
myfunc($some_var=~/value: (\d+)/);
# Attempt #2: This does work, but seems overly complicated
myfunc(join('',$some_var=~/value: (\d+)/));
Run Code Online (Sandbox Code Playgroud)
有没有比尝试#2更好的方法?
更新:
Oesor的回答完全给出了我想要避免调用的内容join:
myfunc(($some_var=~/value: (\d+)/)[0]);
Run Code Online (Sandbox Code Playgroud) 我是perl和使用正则表达式的新手.我需要从字符串中删除空格.
我找到了一个例子,但它对我来说非常不透明.这是对发生的事情的准确描述吗?
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
# =~ : regex on variable string
# s/ : replace match with value
# ^\s+ : one or more white space characters at beginning
# // : no characters
$string =~ s/\s+$//;
# =~ : regex on variable $string
# s/ : replace match with value
# \s+$ : one or more white space characters until end of line
# // : no characters
return $string;
}
Run Code Online (Sandbox Code Playgroud) 在 Perl 中,如何找到字符串中最后一位数字的索引?
示例:Hello123xyz最后一位数字的索引是7
perl ×10
regex ×2
arrays ×1
data-files ×1
declaration ×1
hash ×1
oop ×1
perl-module ×1
prototype ×1
scalar ×1