为什么"!"认为不好的形式在Perl? [英] Why is "!!" considered bad form in Perl?

查看:121
本文介绍了为什么"!"认为不好的形式在Perl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最近的一次面试过程中,我提出了一些示例Perl code其中所用的所谓秘方 !! operator 。后来,在讨论code时,面试官一问我,为什么我选择使用的,并表示,它被认为是不好的形式。他没有详细说明,为什么。

我和我的团队一直在使用该运营商多年,从来没有意识到这一点被认为是不好的形式。

请问嘭嘭运营商有副作用或其他意外的行为?为什么,或有些人可能会说,它是,认为是坏表?是否有地道的选择吗?

下面是在那里我会考虑 !! 接受和/或可取的。

的几个例子

  1. 实际code在编码演习,这是加入布尔值的例子:

     同时(我的$行=< $ F>){
        #SNIP
        存在$ {计数线}和$ {计数}行+ = !!的Chomp $线;
    }


  2. 使用一个布尔值作为哈希键(显然是一个简单的例子):

     子富{
        我($输入)= @_;
        我的答复%=(''=>中假,1 =>真);
        返回$回应{!! $输入};
    }


  3. 在按位操作使用一个布尔值,甚至包()

     子富{
        我($ A,$ B,$ C)= @_;
        我的$结果= !! $ A +($!B)<< 1 +($ !!℃)下; 2;
        返回$结果;
    }


  4. 您需要通过外部库/过程做使用的类型转换,如一个数据库,其中只考虑某些值是truthy:

     我的$某物= $ dbh-> prepare(INSERT INTO表(旗,值)VALUES(,)?)
    $ sth->执行(i_haz_cheeseburger,!! $ cheeseburger_string)



解决方案

!! 充分利用Perl中两个歧件事:<$ C $的具体值C> <!/ code>回报,并且可能的值中的一个是dualvar(含有字符串和多个标量)。使用 !! 来加剧这些行为固然精辟。虽然它的简洁可以是一个巨大的加,它也可以是一个相当大的负数。不要使用导致项目强制要求功能使用​​Perl的是在这个项目上禁止的。

有很多的替代品,以 $计数+ = !! (some_ex pression)来算的前pression的truthy值的出现。一个是三元, $计数+ =(some_ex pression)? 1:0 。这也有点模糊。有一个很好的紧凑的方式做你想要的,这是使用后,如果:

  $ X ++如果some_ex pression;

此说,你在做什么。

During a recent job interview process, I submitted some sample Perl code which used the so-called "secret" !! operator. Later, when discussing the code, one of the interviewers asked me why I chose to use that, and indicated that it was considered bad form. He didn't elaborate as to why.

My team and I have been using this operator for years, without ever realizing it was considered "bad form."

Does the "bang bang" operator have side-effects or other unexpected behavior? Why is it, or might it be, considered "bad form" by some? Is there an idiomatic alternative?

Below are a few examples where I would have considered !! acceptable and/or desirable.

  1. The actual code in the coding exercise, which is an example of adding booleans:

    while (my $line = <$F>) {
        # snip
        exists $counts{lines} and $counts{lines} += !! chomp $line;
    }
    

  2. Using a boolean value as a hash key (clearly a simplified example):

    sub foo {
        my ($input) = @_;
        my %responses = ( '' => "False", 1 => "True" );
        return $responses{ !! $input };
    }
    

  3. Using a boolean in a bitwise operation, or even pack():

    sub foo {
        my ( $a, $b, $c ) = @_;
        my $result = !!$a + (!! $b)<<1 + (!! $c)<<2;
        return $result;
    }
    

  4. You need to do a typecast for use by an external library/process, such as a database, which only considers certain values to be truthy:

    my $sth = $dbh->prepare("INSERT INTO table (flag,value) VALUES (?,?)")
    $sth->execute("i_haz_cheeseburger", !! $cheeseburger_string)
    

解决方案

Your !! takes advantage of two obscure things in Perl: The specific values that ! returns, and that one of the possible values is dualvar (a scalar containing both a string and a number). Using !! to compound these behaviors is admittedly pithy. While its pithiness can be a huge plus, it can also be a rather big minus. Don't use features that result in a project mandating that "the use of Perl is forbidden on this project."

There are lots of alternatives to $count += !! (some_expression) to count occurrences of truthy values of that expression. One is the ternary, $count += (some_expression) ? 1 : 0. That too is a bit obscure. There is a nice compact way to do what you want, which is to use the post-if:

$x++ if some_expression;

This says exactly what you are doing.

这篇关于为什么&QUOT;!&QUOT;认为不好的形式在Perl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆