如果在匿名子程序中调用shift,会发生什么情况?

斯宾塞·巴格利(Spencer Bagley)

首先,如果这个问题不适当地表示歉意;我实际上并不了解很多Perl。

我正在尝试调试一些现有代码,这些代码应该将名为WeBWorK的在线作业系统中的成绩发送给LMS。我遇到了一个奇怪的错误,我认为某些东西没有正确初始化,或者不是正确的类。我怀疑问题可能出在这里:

sub go {
    my $self = shift;
    my $r = $self->r;
    my $ce = $r->ce;

    # If grades are begin passed back to the lti then we peroidically
    # update all of the grades because things can get out of sync if
    # instructors add or modify sets.
    if ($ce->{LTIGradeMode}) {

      my $grader = WeBWorK::Authen::LTIAdvanced::SubmitGrade->new($r);

      my $post_connection_action = sub {
        my $grader = shift;

        # catch exceptions generated during the sending process
        my $result_message = eval { $grader->mass_update() };
        if ($@) {
          # add the die message to the result message
          $result_message .= "An error occurred while trying to update grades via LTI.\n"
        . "The error message is:\n\n$@\n\n";
          # and also write it to the apache log
          $r->log->error("An error occurred while trying to update grades via LTI: $@\n");
        }
      };
      if (MP2) {
        $r->connection->pool->cleanup_register($post_connection_action, $grader);
      } else {
        $r->post_connection($post_connection_action, $grader);
      }
    }
... # a bunch of other stuff happens in the "go" sub

我有点怀疑问题出在$grader变量上。特别是,我不知道my $grader = shift;匿名子内部有什么作用。就像,如果子对象具有名称,则将shift传递给子对象的第一个参数会更清楚但是由于它是匿名的,所以我不知道它的论点是什么。

此外,我不太确定为什么根本需要该行。就像我从谷歌搜索中了解到,匿名子程序的作用是将周围环境中的所有变量都保留在范围内。那么,为什么我们首先需要$grader在匿名子内部重新定义

感谢您帮助Perl新手!:)

池上

在这方面,无用潜艇没什么特别的。

my $cr = sub {
   my $arg = shift;
   say $arg;
};

$cr->("foo");   # Prints "foo"
$cr->("bar");   # Prints "bar"

在你的情况,你通过$post_connection_action,并$gradercleanup_registerpost_connection与预期,这将导致调用&$post_connection_action$grader作为第一个参数。无论期望是正确与否取决于执行cleanup_registerpost_connection,而我什么都不知道。


请注意,此处提供了另一种解决方案。sub评估运算符时,Sub可以访问范围内的词汇

my $prefix = "> ";
my $cr = sub {
   my $arg = shift;
   say "$prefix$arg";   # Captures $prefix from sub{} scope.
};

$cr->("foo");           # Prints "> foo"

即使在子程序被调用时否则捕获的词汇将不再存在的情况下,上述情况也是正确的。

my $cr;
{
   my $prefix = "> ";
   $cr = sub {
      my $arg = shift;
      say "$prefix$arg";   # Captures $prefix from sub{} scope.
   };
}                          # $prefix would normally stop existing here.

$cr->("foo");              # Prints "> foo"

这意味着您不需要$grader作为参数传递它可以简单地被捕获只需省去my $grader = shift;(不要传递$gradercleanup_registerpost_connection)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如果在取消点上调用信号处理程序,会发生什么情况?

如果在ajax调用期间显示警报窗口,会发生什么情况?

如果在事务执行过程中终止JVM进程,会发生什么情况?

如果在更新过程中安排关机,会发生什么情况?

如果在关闭连接之前未调用transaction.Rollback / Commit,会发生什么情况?

如果在使用PTHREAD_PROCESS_SHARED时不调用pthread_mutex_destroy,会发生什么情况

如果在ThreadPool线程正在写入文件时关闭应用程序会发生什么情况?

在C程序中,如果在两个具有不同实现的不同库中定义了相同功能,会发生什么情况?

如果在反引号中为命令表达式分配了变量,会发生什么情况

如果在NET Core库中添加PackageReference net46,会发生什么情况?

如果在执行过程中修改了SQL Server作业步骤,会发生什么情况

如果在地址中未将字对齐时将char *地址转换为int *,会发生什么情况?

如果在SQL中现有字段名后没有放置逗号,会发生什么情况

如果在推送过程中忽略./.git/objects/pack/下的.pack文件,会发生什么情况?

如果在 writeFileSync 操作期间中止 Node 脚本,文件会发生什么情况?

如果在我的java文件中使用了很多@SuppressWarnings,会发生什么情况?

如果在JVM仍在使用Jar时更新了Jar,会发生什么情况?

如果在已经初始化的变量上使用`var`会发生什么情况

如果在crontab文件中使用@reboot reboot,会发生什么情况?

如果在finalize()期间引发异常会发生什么情况

如果在没有JMSPriority标头的情况下发送JMS消息会发生什么情况

如果在需要整数的切换情况下输入字符会发生什么情况

如果在同一集合上两次调用同一迭代器,会发生什么情况?

如果在其他线程仍在运行时调用exit(0),会发生什么情况?

如果在laravel5中没有DB :: commit()的情况下调用DB :: beginTransaction,会发生什么?

如果在log4j的getlogger(“ ...”)方法上找不到指定的日志文件,会发生什么情况

如果在关闭套接字之前不发送IP_DROP_MEMBERSHIP,会发生什么情况?

如果在lambda表达式的函数接口中添加不同的参数类型,会发生什么情况?

如果在恶霸算法中进行选举后唤醒失败的流程,会发生什么情况