Perl:基本问题,函数功能

发布于 2024-10-04 22:56:15 字数 123 浏览 1 评论 0原文

这个函数有什么作用?

    sub MyDigit {
       return <<END;
       0030\t0039
       END
    }

What does this function do?

    sub MyDigit {
       return <<END;
       0030\t0039
       END
    }

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

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

发布评论

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

评论(5

独夜无伴 2024-10-11 22:56:15

这称为"here-document",用于分解字符串多行作为串联或列表操作的替代方法:

print "this is ",
    "one line when printed, ",
    "because print takes multiple ",
    "arguments and prints them all!\n";
print "however, you can also " .
    "concatenate strings together " .
    "and print them all as one string.\n";

print <<DOC;
But if you have a lot of text to print,
you can use a "here document" and create
a literal string that runs until the
delimiter that was declared with <<.
DOC
print "..and now we're back to regular code.\n";

您可以在手册中阅读有关此处文档的更多信息:请参阅 perldoc perlop

That's called a "here-document", and is used for breaking strings up over multiple lines as an alternative to concatenation or list operations:

print "this is ",
    "one line when printed, ",
    "because print takes multiple ",
    "arguments and prints them all!\n";
print "however, you can also " .
    "concatenate strings together " .
    "and print them all as one string.\n";

print <<DOC;
But if you have a lot of text to print,
you can use a "here document" and create
a literal string that runs until the
delimiter that was declared with <<.
DOC
print "..and now we're back to regular code.\n";

You can read more about here-documents in the manual: see perldoc perlop.

智商已欠费 2024-10-11 22:56:15

你们都错过了重点!

它使用正则表达式定义一个用户定义的属性,供在 \p{MyDigit}\P{MyDigit} 中使用。

就像这样:

  sub InKana {
      return <<'END';
  3040    309F
  30A0    30FF
  END
  }

或者,您可以根据现有属性名称来定义它:

  sub InKana {
      return <<'END';
  +utf8::InHiragana
  +utf8::InKatakana
  END
  }

您还可以使用“C<->”进行集合减法前缀。假设你只
想要实际的字符,而不仅仅是字符的块范围。
您可以像这样清除所有未定义的字符集:

  sub IsKana {
      return <<'END';
  +utf8::InHiragana
  +utf8::InKatakana
  -utf8::IsCn
  END
  }  

您还可以使用“C”前缀从补充字符集开始:

  sub IsNotKana {
      return <<'END';
  !utf8::InHiragana
  -utf8::InKatakana
  +utf8::IsCn
  END
  }

我想我一定是对的,因为我说的是excamelis。 :)

You’ve all missed the point!

It’s defining a user-defined property for use in \p{MyDigit} and \P{MyDigit} using regular expressions.

It’s like these:

  sub InKana {
      return <<'END';
  3040    309F
  30A0    30FF
  END
  }

Alternatively, you could define it in terms of existing property names:

  sub InKana {
      return <<'END';
  +utf8::InHiragana
  +utf8::InKatakana
  END
  }

You can also do set subtraction using a "C<->" prefix. Suppose you only
wanted the actual characters, not just the block ranges of characters.
You could weed out all the undefined ones like this:

  sub IsKana {
      return <<'END';
  +utf8::InHiragana
  +utf8::InKatakana
  -utf8::IsCn
  END
  }  

You can also start with a complemented character set using the "C" prefix:

  sub IsNotKana {
      return <<'END';
  !utf8::InHiragana
  -utf8::InKatakana
  +utf8::IsCn
  END
  }

I figure I must be right, since I’m speaking ex camelis. :)

我家小可爱 2024-10-11 22:56:15

它使用名为 此处文档 的内容来返回字符串“0030\t0039”

It uses something called a Here Document to return a string "0030\t0039"

方圜几里 2024-10-11 22:56:15

它返回字符串 "0030\t0039\n"\t 是制表符,\n 是要添加的换行符,因为该行以换行符结尾(显然))。

<<FOO
sometext
FOO

是所谓的heredoc,一种方便地编写多行字符串的方法(尽管这里只使用一行)。

It returns the string "0030\t0039\n" (\t being a tab and \n a newline that is being added because the line ends in a newline (obviously)).

<<FOO
sometext
FOO

Is a so-called heredoc, a way to conveniently write multi-line strings (though here it is used with only one line).

神仙妹妹 2024-10-11 22:56:15

您可以通过尝试一个简单的实验来帮助自己:

C:\Temp> cat t.pl
#!/usr/bin/perl

use strict; use warnings;

print MyDigit();

sub MyDigit {
    return <<END;
    0030\t0039
END
}

输出:

C:\Temp> t | xxd
0000000: 2020 2020 3030 3330 0930 3033 390d 0a        0030.0039..

现在,在您的情况下, END 没有排列在行的开头,因此您应该已经收到消息:

Can't find string terminator "END" anywhere before EOF at …

You can help yourself by trying a simple experiment:

C:\Temp> cat t.pl
#!/usr/bin/perl

use strict; use warnings;

print MyDigit();

sub MyDigit {
    return <<END;
    0030\t0039
END
}

Output:

C:\Temp> t | xxd
0000000: 2020 2020 3030 3330 0930 3033 390d 0a        0030.0039..

Now, in your case, the END is not lined up at the beginning of the line, so you should have gotten the message:

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