莫约利奇如何修改perl语法

发布于 2025-02-05 18:04:41 字数 671 浏览 2 评论 0原文

阅读mojolicious minions文档我找到了以下代码:

use v5.28;
use Mojolicious::Lite;
use experimental qw( signatures );
use Time::HiRes qw( time );

plugin Minion => {
    SQLite => 'sqlite:' . app->home->child('minion.db'),
};

# ...
app->start;

如何 找到以下代码:他们是否创建了一个新的语法插件minion => {...}?我从未在经典的佩尔书中见过。它是带有哈希参数的函数调用:“小牛仔”是钥匙,而hashref {...}是一个值?

他们还调用app-> start - app是返回祝福的hashref的功能吗?但是,如果这是一个哈希,为什么“开始”不会封闭在牙套中?这个语法对我来说很奇怪。

Reading Mojolicious minions documentation I found the following code:

use v5.28;
use Mojolicious::Lite;
use experimental qw( signatures );
use Time::HiRes qw( time );

plugin Minion => {
    SQLite => 'sqlite:' . app->home->child('minion.db'),
};

# ...
app->start;

How did they create a new syntax plugin Minion => {...}? I've never seen it in classic Perl books. Is it a function call with a hash parameter: "Minion" being a key and a hashref {...} is a value?

Also they call app->start - app is a function returning a blessed hashref? But if it was a hash why "start" is not enclosed into braces? This syntax looks strange to me.

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

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

发布评论

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

评论(1

夏末染殇 2025-02-12 18:04:42

app是返回$ app的函数,它是mojolicious :: lite = lite = hash

app-> start is same as app()->start

https://github.com/mojolicious/mojo/blob/24d1d1987dbfbe27eaaa37dd5db692d2deb8d07f/lib/Mojolicious/Plugin/Config.pm#L12-L14

"sub app; local *app = sub { \$app }; use Mojo::Base -strict; $content";
                             |
                             ^ not a reference but escape
                             due to eval() of double quoted string

Reproduction

perl -MMojolicious::Lite -E 'no warnings; sub foo; local *foo = sub { app }; say for foo, app'

output

Mojolicious::Lite=HASH(0xe72080)
Mojolicious::Lite=HASH(0xe72080)

plugin< /code>是一个常规功能,

perl -MData::Dumper -wE 'sub plugin { print Dumper \@_ } plugin Minion => { SQLite => "sqlite:" }'
$VAR1 = [
          'Minion',
          {
            'SQLite' => 'sqlite:'
          }
        ];

您可以添加parens,然后将fat Comma放置为通常的功能调用,

plugin("Minion", { SQLite => "sqlite:" });

app is a function which returns $app which is an instance of Mojolicious::Lite=HASH

app->start is same as app()->start

https://github.com/mojolicious/mojo/blob/24d1d1987dbfbe27eaaa37dd5db692d2deb8d07f/lib/Mojolicious/Plugin/Config.pm#L12-L14

"sub app; local *app = sub { \$app }; use Mojo::Base -strict; $content";
                             |
                             ^ not a reference but escape
                             due to eval() of double quoted string

Reproduction

perl -MMojolicious::Lite -E 'no warnings; sub foo; local *foo = sub { app }; say for foo, app'

output

Mojolicious::Lite=HASH(0xe72080)
Mojolicious::Lite=HASH(0xe72080)

plugin is a regular function

perl -MData::Dumper -wE 'sub plugin { print Dumper \@_ } plugin Minion => { SQLite => "sqlite:" }'
$VAR1 = [
          'Minion',
          {
            'SQLite' => 'sqlite:'
          }
        ];

You can add parens, and drop fat comma to look like the usual function call,

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