English 中文(简体)
这三维功能是否sil?
原标题:Is this trivial function silly?
  • 时间:2010-06-07 16:24:59
  •  标签:
  • perl

我今天接过了一个功能,使我停下来思考。 我认为这样做有好的理由:

sub replace_string {
        my $string  = shift;
        my $regex   = shift;
        my $replace = shift;

        $string =~ s/$regex/$replace/gi;

        return $string;
}

我能看到的唯一可能价值是,它使你能够控制用替代方式使用的缺省办法,但我不认为这样做有用。 我在看到这一职能时首先的反应是“这样做了吗? 一旦我了解情况,我将假定从那时起就这样做了。 如果它发生变化,它将打破我需要它这样做的任何守则。 这意味着这一职能将永远不会改变,或改变它会打破许多法典。

现在,我想对原方案人进行跟踪,并对她进行某种感觉。 这是否是一种正当的愿望,还是我缺乏这一职能带来的某种价值?

问题回答

与这一职能有关的问题包括:

  • Opaque: replace_string doesn t tell you that you re doing a case-insensitive, global replace without escaping.
  • Non-idiomatic: $string =~ s{$this}{$that}gi is something you can learn what it means once, and its not like its some weird corner feature. replace_string everyone has to learn the details of, and its going to be different for everyone who writes it.
  • Inflexible: Want a non-global search-and-replace? Sorry. You can put in some modifiers by passing in a qr// but that s far more advanced knowledge than the s/// its hiding.
  • Insecure: A user might think that the function takes a string, not a regex. If they put in unchecked user input they are opening up a potential security hole.
  • Slower: Just to add the final insult.

优点是:

  • Literate: The function name explains what it does without having to examine the details of the regular expression (but it gives an incomplete explanation).
  • Defaults: The g and i defaults are always there (but that s non-obvious from the name).
  • Simpler Syntax: Don t have to worry about the delimiters (not that s{}{} is difficult).
  • Protection From Global Side Effects: Regex matches set a salad of global variables ($1, $+, etc...) but they re automatically locally scoped to the function. They won t interfere if you re making use of them for another regex.

资本ulation缩略微高。

print replace_string("some/path", "/", ":");

是的,你不得不用不同的限定词取代或躲避。

仅替换<代码>/ 当时,我猜测,有人用一种语言来在Perl上写了这封信,而使用普通话则需要额外的辛迪加,而那是/更舒适的ding。 如果一例将它归类为Perl婴孩:沉sil不清, to不住老练的军士,但不是坏——但不足以要求殴打,不管怎么说;)

如果我真的难以想象,我几乎会看到这样的功能可能有用的情况:向一组星群运用一套模式,使用户能够就这些术语提出意见,为呼吁提供CODE参考。

我在看到这是一位新的Perl方案家时所作的第一次反应是,不要忘记玩物,经常表达,并创造出他/她可以轻易记住的职能,而不要学习yn。

除了上文提到的那些理由之外,我可以看到的唯一原因(新方案家不想记住reg子),是他们有可能使用某些没有 highlighting子的民主选举学会,但对于他们书写的职能确实存在。 不是最好的原因,而是可行的。





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签