为什么 substr 直接传递到方法时工作方式不同?

发布于 2024-10-15 08:41:41 字数 907 浏览 3 评论 0原文

我有一个关于 perl 的问题,过去我不关心这个问题,但现在它困扰着我。

我有一个名为 saveItems 的方法,它从文本日志中获取值并解析输入。

所以我在方法中有这几行。

$intime  = $_[1];
$timeHr  = substr($intime, 0,2); 
$timeMin = substr($intime, 2,2);
$timeSec = substr($intime, 5,2);
$object[$_[0]]->hr($timeHr);
$object[$_[0]]->min($timeMin);
$object[$_[0]]->sec($timeSec);

$intime 是传递到此方法的时间值。 $intime 示例:0431:12

我的问题是,为什么上面的代码没有给我任何错误,但是当我尝试像这样缩短行时:

$object[$_[0]]->hr(substr($intime, 0,2));
$object[$_[0]]->min(substr($intime, 2,2));
$object[$_[0]]->sec(substr($intime, 5,2));

只有第一个有效,而其余的则给我一个字符串错误。

正如你所看到的,我对 Perl 比较陌生,但是有人能给我答案吗?

编辑

人力资源示例:

sub hr {
    my $self = shift;
    if (@_) { $self->{HR} = shift }
    return $self->{HR};
}

编辑

案例已结..阅读我的回答帖子

I've a question about perl that I used to not bother about in the past, but it's bugging me now.

I have a method call saveItems which takes in a value from a text log and parses the input.

so I have this few lines in the method.

$intime  = $_[1];
$timeHr  = substr($intime, 0,2); 
$timeMin = substr($intime, 2,2);
$timeSec = substr($intime, 5,2);
$object[$_[0]]->hr($timeHr);
$object[$_[0]]->min($timeMin);
$object[$_[0]]->sec($timeSec);

$intime being the value of the time passed into this method.
Sample of $intime: 0431:12

My question is that why does the above not give me any error but when I try to shorten the lines like so :

$object[$_[0]]->hr(substr($intime, 0,2));
$object[$_[0]]->min(substr($intime, 2,2));
$object[$_[0]]->sec(substr($intime, 5,2));

Only the first one works while the rest gives me an out of string error.

I am relatively new to perl, as you can see, but can anyone give me an answer to this?

EDIT

Sample HR:

sub hr {
    my $self = shift;
    if (@_) { $self->{HR} = shift }
    return $self->{HR};
}

EDIT

Case Closed.. Read my answer post

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

想挽留 2024-10-22 08:41:41

从上面的评论来看,在每个 substr 之后添加 .'' 解决了您的问题。其原因是 ->hr->min->sec 方法在某些情况下修改其参数方式。如果没有进一步观察,我无法确定发生了什么。

substr 函数返回一个有效左值的值。这意味着它可以被分配给。因此,当这些方法中的某些内容从 substr 分配给切片时,它会干扰其他方法。

附加空字符串可以通过破坏切片和原始字符串(存储在 $intime 中)之间的别名来解决问题。

如果您编写了 hrminsec 方法,您应该弄清楚它们为何修改参数。在每个方法调用之间添加 print "[$intime]\n"; 语句应该能够揭示问题。

From the comments above, adding .'' after each substr solved your problem. The reason for this is that the ->hr, ->min, and ->sec methods are modifying their argument in some way. Without seeing it further I can't say for certain what is happening.

The substr function returns a value that is a valid lvalue. This means that it can be assigned to. So when something in those methods assigns to the slice from substr, it is interfering with the other methods.

Appending an empty string fixes the problem by breaking the alias between the slice and the original string (stored in $intime).

If you wrote the hr, min and sec methods, you should figure out why they are modifying their arguments. Adding print "[$intime]\n"; statements between each method call should be revealing.

云归处 2024-10-22 08:41:41

您能拿出独立的可运行代码来演示该问题吗?尽管我不明白 @object 在代码中的作用,但您描述的问题与您显示的代码不太相符。

以下工作正常:

use strict;
use warnings;

package Class;

sub new { bless {} }

sub saveItems {
    my $intime = $_[1];
    $_[0]->hr(substr($intime, 0,2));
    $_[0]->min(substr($intime, 2,2));
    $_[0]->sec(substr($intime, 5,2));
}

sub hr {
    my $self = shift;
    if (@_) { $self->{HR} = shift }
    return $self->{HR};
}

sub min {
    my $self = shift;
    if (@_) { $self->{MIN} = shift }
    return $self->{MIN};
}

sub sec {
    my $self = shift;
    if (@_) { $self->{SEC} = shift }
    return $self->{SEC};
}

package main;

my $object = Class->new();
$object->saveItems( '0431:12' );
print "hr: ", $object->hr(), " min: ", $object->min(), " sec: ", $object->sec(), "\n";

Can you come up with self-contained runnable code that demonstrates the problem? The problem you describe doesn't quite match up with the code you show, though I don't understand @object's role in your code.

The following works just fine:

use strict;
use warnings;

package Class;

sub new { bless {} }

sub saveItems {
    my $intime = $_[1];
    $_[0]->hr(substr($intime, 0,2));
    $_[0]->min(substr($intime, 2,2));
    $_[0]->sec(substr($intime, 5,2));
}

sub hr {
    my $self = shift;
    if (@_) { $self->{HR} = shift }
    return $self->{HR};
}

sub min {
    my $self = shift;
    if (@_) { $self->{MIN} = shift }
    return $self->{MIN};
}

sub sec {
    my $self = shift;
    if (@_) { $self->{SEC} = shift }
    return $self->{SEC};
}

package main;

my $object = Class->new();
$object->saveItems( '0431:12' );
print "hr: ", $object->hr(), " min: ", $object->min(), " sec: ", $object->sec(), "\n";
梦开始←不甜 2024-10-22 08:41:41

这件事已经解决了。

substr的使用方式如下,都能够正常执行,没有错误。

$object[$_[0]]->hr(substr($intime, 0,2)); 
$object[$_[0]]->min(substr($intime, 2,2)); 
$object[$_[0]]->sec(substr($intime, 5,2)); 

但是,正是日志文件中的尾随空白行导致该脚本失败。

感谢@ysth要求我重现该问题,当我意识到问题实际上在于日志文件而不是脚本时。

经验教训:在提出问题之前检查代码和来源

This matter has been resolved.

The way of using substr as follows, are able to perform normally, without errors.

$object[$_[0]]->hr(substr($intime, 0,2)); 
$object[$_[0]]->min(substr($intime, 2,2)); 
$object[$_[0]]->sec(substr($intime, 5,2)); 

However, it is the log file that has trailing blank lines that got this script to fail.

Thanks to @ysth for asking me to reproduce the problem, when I realized that the problem actually lies with the log file instead of the script.

Lesson learnt: Check the codes AND the source before raising an issue

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文