使用三元运算符赋值?

发布于 2024-12-27 17:23:07 字数 513 浏览 0 评论 0原文

我使用的是 Perl 5.8,需要分配一个默认值。我最终这样做了:

if ($model->test) {
    $review = "1"
} else {
    $review = ''
}

$model->test 的值将是 "1" 或未定义。如果 $model->test 中有内容,请将 $review 设置为 "1",否则将其设置为 ''< /代码>。

因为它不是 Perl 5.10,所以我无法使用新的时髦的定义或运算符。我的第一反应是像这样使用三元运算符......

defined($model->test) ? $review = "1" : $review = '';

但这也不起作用。

有谁知道如何更有效地分配它? 珍妮

I am on Perl 5.8 and am needing to assign a default value. I ended up doing this:

if ($model->test) {
    $review = "1"
} else {
    $review = ''
}

The value of $model->test is going to be either "1" or undefined. If there's something in $model->test, set $review to "1" otherwise set it equal to ''.

Because it's not Perl 5.10 I can't use the new swanky defined-or operator. My first reaction was to use the ternary operator like this...

defined($model->test) ? $review = "1" : $review = '';

but that didn't work either.

Does anyone have an idea how to assign this more efficiently?
Janie

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

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

发布评论

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

评论(7

北方的巷 2025-01-03 17:23:07

我通常将其写为:

$review = ( defined($model->test) ? 1 : '' );

其中括号是为了让其他阅读代码的人清楚起见。

I'd usually write this as:

$review = ( defined($model->test) ? 1 : '' );

where the parentheses are for clarity for other people reading the code.

心碎无痕… 2025-01-03 17:23:07

你有一个优先级问题。你所拥有的与 You can make it work with parens 相同

( defined($model->test) ? $review="1" : $review ) = '';

my $review; defined($model->test) ? ( $review='1' ) : ( $review='' );

但将作业移出会干净得多。

my $review = defined($model->test) ? '1' : '';

继续下去,检查该值是否已定义根本没有意义。鉴于仅有的两个可能值是 undef 和字符串 1,一个简单的真值测试就可以了。

my $review = $model->test ? '1' : '';

事实上,既然你希望真实值不变,你可以简单地使用以下

my $review = $model->test || '';

最后,你真的需要将 undef 更改为空字符串吗?如果没有,您可以简单地使用以下命令:

my $review = $model->test;

You have a precedence problem. What you have is the same as

( defined($model->test) ? $review="1" : $review ) = '';

You could make it work with parens.

my $review; defined($model->test) ? ( $review='1' ) : ( $review='' );

But it's much cleaner to move the assignment out.

my $review = defined($model->test) ? '1' : '';

Moving along, there's no point in checking if the value is defined at all. Seeing as the only two possible values are undef and the string 1, a simple truth test would do.

my $review = $model->test ? '1' : '';

In fact, since you want true values unchanged, you could simply use the following

my $review = $model->test || '';

Finally, do you really need to change undef into to an empty string? If not, you can simply use the following:

my $review = $model->test;
老街孤人 2025-01-03 17:23:07

$model->test 将是 "1" 或未定义。如果 $model->test 中有内容,请将 $review 设置为 "1",否则设置为 ''

然后就用这个:

$review = $model->test || "";

$model->test is going to be either "1" or undefined. If there's something in $model->test, set $review to "1" otherwise set it ''

Then just use this:

$review = $model->test || "";
遇到 2025-01-03 17:23:07

除了条件运算符之外,我还经常喜欢使用 do,它返回最后计算的表达式的值:

my $review = do {
     if( ... ) { 'foo' }
  elsif( ... ) { 'bar' }
  elsif( ... ) { 'baz' }
  else         { 'defaut' }
  };

Besides the conditional operator, I often like to use do, which returns the value from the last evaluated expression:

my $review = do {
     if( ... ) { 'foo' }
  elsif( ... ) { 'bar' }
  elsif( ... ) { 'baz' }
  else         { 'defaut' }
  };
天冷不及心凉 2025-01-03 17:23:07

我假设 $model->test 应该返回 true 或 false 值。

除非明确指出 false 值为 undef,否则可以重写该方法以开始返回一些其他 false 值。
这会破坏仅检查值是否已定义的任何内容。
(我认为该方法返回 undef 而不是规范的 false 值是一个错误。)

因此设置 $review 的最佳方法是测试返回的真实性价值;不是它的定义。

my $review = $model->test ? 1 : '';

我想指出的是,这仍然存在一个错误。
如果您希望能够将该值用作数字,则如果该值为假,它将发出警告。

要解决此问题,您应该返回 !1 (规范错误值),它将返回一个字符串 '' 值,但也具有数值 0

my $review = $model->test ? 1 : !1;

。它可以简化为:

my $review = !! $model->test; # invert it twice

如果您只想在值为 false 时更改该值,则可以使用 or 运算符 ||

my $review = $model->test || !1;

如果您真的只想知道它是否已定义,为什么不使用 已定义

my $review = defined $model->test;

如果您只想在未定义时更改该值,并且您有 Perl 5.10 或更高版本,则可以使用 定义或运算符 (//)

my $review = $model->test // !1;

在较旧的 Perl 上,这需要多个语句

my $review = $model->test;
$review = !1 unless defined $review;

I assume that $model->test is supposed to return a true or false value.

Unless it specifically states that the false value is undef, the method could be rewritten to start returning some other false value instead.
Which would break anything that only checks if the value is defined.
( I think it is a bug that the method returns undef instead of the canonical false value. )

So the best way to set $review is to test the truthfulness of the returned value; not it's definedness.

my $review = $model->test ? 1 : '';

I would like to point out that this still has a bug in it.
If you want to be able to use the value as a number, it will emit warnings if it was false.

To fix that you should return !1 (canonical false value), which will return a value that is the string '', but also has the numerical value of 0.

my $review = $model->test ? 1 : !1;

Notice that it could be simplified to just:

my $review = !! $model->test; # invert it twice

If you only want to change the value only when it is false, you could use the or operator ||.

my $review = $model->test || !1;

If you really only want to know if it is defined, or not why don't you just use defined.

my $review = defined $model->test;

If you want to change the value only when it is undefined, and you have Perl 5.10 or newer you could use the defined-or operator (//).

my $review = $model->test // !1;

On an older Perl, that would require more than one statement.

my $review = $model->test;
$review = !1 unless defined $review;
一身软味 2025-01-03 17:23:07

首先,“这也不起作用”并不是您能告诉我们的最有帮助的事情。重要的是要确切地知道它为什么不起作用:它做了什么,您期望什么,以及它们有何不同?

但问题在于

defined($model->test) ? $review="1" : $review='';

运算符优先级。条件运算符 ? : 比赋值运算符 = 绑定得更紧密,因此上面等价于:

(defined($model->test) ? $review="1" : $review) = '';

因此,如果定义了 $model->test ,则执行等价操作 您可以用括号

$review = "1" = '';

解决这个问题:

defined($model->test) ? ($review="1") : ($review='');

但实际上,您为什么要这样做?当您想要使用结果时,条件(三元)运算符非常有用。如果结果将被丢弃,就像这里一样,那么使用 if/else 语句会更清晰(而且,正如您所见,更不容易出错):

if (defined($model->test) {
    $review = "1";
}
else {
    $review = "";
}

或者,如果您坚持将其写在一行上:

if (defined($model->test) { $review = "1"; } else { $review = ""; }

如果您确实想使用条件表达式,可以这样做:

$review = defined($model->test) ? "1" : "";

这可能是一种合理的方法。

BUT:

define 运算符本身会产生 "1" (true) 或 "" (false)。所以整个事情可以简化为:

$review = defined($model->test);

First of all, "That didn't work either" is not the most helpful thing you could tell us. It's important to know exactly how it didn't work: what did it do, what did you expect, and how to they differ?

But the problem with

defined($model->test) ? $review="1" : $review='';

is operator precedence. The conditional operator ? : binds more tightly than the assignment operator =, so the above is equivalent to:

(defined($model->test) ? $review="1" : $review) = '';

So if $model->test is defined, it does the equivalent of

$review = "1" = '';

You can fix that problem with parentheses:

defined($model->test) ? ($review="1") : ($review='');

But really, why would you want to? The conditional (ternary) operator is useful when you want to use the result. If the result is going to be discarded, as it is here, it's clearer (and, as you've seen, less error-prone) to use an if/else statement:

if (defined($model->test) {
    $review = "1";
}
else {
    $review = "";
}

or, if you insist on writing it on one line:

if (defined($model->test) { $review = "1"; } else { $review = ""; }

If you really want to use a conditional expression, you can do this:

$review = defined($model->test) ? "1" : "";

which is probably a reasonable way to do it.

BUT :

The defined operator itself yields either "1" (true) or "" (false). so the whole thing can be reduced to:

$review = defined($model->test);
緦唸λ蓇 2025-01-03 17:23:07
my $result = defined $model->test ? '1' : '';
my $result = defined $model->test ? '1' : '';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文