What "ladder sub {...}" means in perl?
Asked Answered
I

1

7

I'm reading tweetylicious source from github to study Mojolicious framework:

However I'm confused by below piece of code ladder sub .... What does it mean in Perl? It looks like not a regular Perl grammar.

Btw, I'm with Strawberry Perl 5.

# The rest of the routes are specific to logged in users, so we
# add a ladder to make sure (instead of making sure inside each route)
ladder sub {
    my $self = shift;
    return 1 if $self->session('name');
    $self->redirect_to('/login') and return;
};
Impulsion answered 22/5, 2018 at 3:11 Comment(0)
U
9

It is a call to a subroutine called ladder that expects a code reference as its first argument. It is equivalent to

$tmpfunc = sub {
    my $self = shift;
    return 1 if $self->session('name');
    $self->redirect_to('/login') and return;
};
ladder($tmpfunc);
Utterance answered 22/5, 2018 at 3:14 Comment(5)
Thanks for answering. But how I fix the compilation error? I tried change it to the example style, but then I blocked by cgi.pm:has description => 'Start application with CGI'; has usage => sub { shift->extract_usage };Impulsion
@Impulsion the code you're looking at is from 2011. The Mojolicious project is quite active and the API changes frequently. I've searched through the github repo and I can only find two references to ladder, when it was added in 2010. It has since been removed, but the changelog doesn't reveal when. I think you should look for more modern examples.Kenosis
@Kenosis Got. Thanks for your time.Impulsion
@Impulsion I went on IRC and asked in #mojolicious. The creator of the project says it probably went out in 2010 before version 1.0. So I would say it's safe to assume we shouldn't be using it today. The modern equivalent is called under, btw.Kenosis
@Impulsion thank you. We're all just people here. I hope you're having fun learning Perl although you're experiencing a bit of a setback right now! :)Kenosis

© 2022 - 2024 — McMap. All rights reserved.