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;
};
mikhail-t
  • 3,655
  • 6
  • 31
  • 53
Hao
  • 318
  • 2
  • 8

1 Answers1

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);
mob
  • 110,546
  • 17
  • 138
  • 265
  • 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 }; – Hao May 22 '18 at 05:38
  • 1
    @Hao 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. – simbabque May 22 '18 at 10:13
  • @simbabque Got. Thanks for your time. – Hao May 22 '18 at 12:54
  • 1
    @Hao 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. – simbabque May 22 '18 at 12:57
  • @Hao 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! :) – simbabque May 22 '18 at 13:25