如何使用块作为“或” 子句而不是简单的骰子?

发布于 2024-07-16 08:21:35 字数 479 浏览 6 评论 0原文

我想检查 Net::FTP Perl 模块中的操作结果而不是死掉。

通常你会这样做:

$ftp->put($my_file)
  or die "Couldn't upload file";

但我想做点别的事情,而不是仅仅死在这个脚本中,所以我尝试了:

$ftp->put($my_file)
  or {
      log("Couldn't upload $my_file");
      return(-1);
  }

log("$my_file uploaded");

但 Perl 抱怨编译错误说:

syntax error at toto.pl line nnn, near "log"

这是我的代码片段中的第二个日志。

任何建议都非常感激。

干杯,

I want to check the results of an operation in the Net::FTP Perl module rather than die.

Typically you would do:

$ftp->put($my_file)
  or die "Couldn't upload file";

But I want to do something else instead of just dying in this script so I tried:

$ftp->put($my_file)
  or {
      log("Couldn't upload $my_file");
      return(-1);
  }

log("$my_file uploaded");

But Perl complains of compilation errors saying:

syntax error at toto.pl line nnn, near "log"

which is the second log in my code fragment.

Any suggestions greatly appreciated.

cheers,

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

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

发布评论

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

评论(4

枯寂 2024-07-23 08:21:35

do 就是你的正在寻找:

$ftp->put($my_file)
  or do {
      log("Couldn't upload $my_file");
      return(-1);
  };

log("$my_file uploaded");

但是可能是更好的风格:

unless( $ftp->put( $my_file )) { # OR if ( !$ftp->put...
      log("Couldn't upload $my_file");
      return(-1);
}

如果您只想返回错误条件,那么您可以die 并使用 < code>eval 在调用函数中。

use English qw<$EVAL_ERROR>; # Thus, $@ <-> $EVAL_ERROR

eval { 
    put_a_file( $ftp, $file_name );
    handle_file_put();
};

if ( $EVAL_ERROR ) { 
    log( $EVAL_ERROR );
    handle_file_not_put();
}

然后调用

sub put_a_file { 
    my ( $ftp, $my_file ) = @_;
    $ftp->put( $my_file ) or die "Couldn't upload $my_file!";
    log( "$my_file uploaded" );

}

do is what you're looking for:

$ftp->put($my_file)
  or do {
      log("Couldn't upload $my_file");
      return(-1);
  };

log("$my_file uploaded");

But this is probably better style:

unless( $ftp->put( $my_file )) { # OR if ( !$ftp->put...
      log("Couldn't upload $my_file");
      return(-1);
}

If you just want to return an error condition, then you can die and use eval in the calling func.

use English qw<$EVAL_ERROR>; # Thus, $@ <-> $EVAL_ERROR

eval { 
    put_a_file( $ftp, $file_name );
    handle_file_put();
};

if ( $EVAL_ERROR ) { 
    log( $EVAL_ERROR );
    handle_file_not_put();
}

and then call

sub put_a_file { 
    my ( $ftp, $my_file ) = @_;
    $ftp->put( $my_file ) or die "Couldn't upload $my_file!";
    log( "$my_file uploaded" );

}

终弃我 2024-07-23 08:21:35

或执行{}; 总是让我头疼。 使用“or”语法(我承认在单行中使用了很多)与“if”(我更喜欢在多行中使用)是否有充分的理由?

那么,是否有理由使用或不使用其中一种方法而不是另一种呢?

foo()
  or do {
    log($error);
    return($error);
  };
log($success);

if (!foo()) {
  log($error);
  return($error);
}
log($success);

or do{}; always makes my head hurt. Is there a good reason to use "or" syntax (which I admit using a lot for one liners) vs "if" (which I prefer for multi liners)?

So, is there a reason to use or not use one of these methods in preference of the other?

foo()
  or do {
    log($error);
    return($error);
  };
log($success);

if (!foo()) {
  log($error);
  return($error);
}
log($success);
纵性 2024-07-23 08:21:35

使用do

这是小代码片段:


sub test {
    my $val = shift;
    if($val != 2) {
        return undef;
    }
    return 1;
}

test(3) || do {
            print "another value was sent";
};

use do.

here is small code snippet:


sub test {
    my $val = shift;
    if($val != 2) {
        return undef;
    }
    return 1;
}

test(3) || do {
            print "another value was sent";
};
沐歌 2024-07-23 08:21:35

我很难理解为什么这需要包含在一个 do 中。 是否有理由认为这还不够?

my $ftp_ok = $ftp->put( $my_file )
  or log("Couldn't upload $my_file" ) and return -1;

log("$my_file uploaded") if $ftp_ok;

这假设 put 函数并不总是在成功时返回 undef。

I'm having a hard time understanding why this needs to be wrapped up in a do. Is there a reason that this isn't sufficient?

my $ftp_ok = $ftp->put( $my_file )
  or log("Couldn't upload $my_file" ) and return -1;

log("$my_file uploaded") if $ftp_ok;

This assumes that the put function doesn't always return undef on success.

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