Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add -signatures flag to Mojo::Base and Mojolicious::Lite
  • Loading branch information
kraih committed Oct 31, 2017
1 parent 6ade763 commit 9b1ea3c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

7.51 2017-11-01
7.51 2017-10-31
- Added -signatures flag to Mojo::Base and Mojolicious::Lite.
- Added support for new HTTP status code.

7.50 2017-10-30
Expand Down
4 changes: 1 addition & 3 deletions README.md
Expand Up @@ -70,9 +70,7 @@ app->start;
applications.

```perl
use Mojolicious::Lite;
use 5.20.0;
use experimental 'signatures';
use Mojolicious::Lite -signatures;

# Render template "index.html.ep" from the DATA section
get '/' => {template => 'index'};
Expand Down
31 changes: 25 additions & 6 deletions lib/Mojo/Base.pm
Expand Up @@ -68,31 +68,39 @@ sub attr {

sub import {
my $class = shift;
return unless my $flag = shift;
return unless my @flags = @_;

# Base
if ($flag eq '-base') { $flag = $class }
if ($flags[0] eq '-base') { $flags[0] = $class }

# Strict
elsif ($flag eq '-strict') { $flag = undef }
elsif ($flags[0] eq '-strict') { $flags[0] = undef }

# Module
elsif ((my $file = $flag) && !$flag->can('new')) {
elsif ((my $file = $flags[0]) && !$flags[0]->can('new')) {
$file =~ s!::|'!/!g;
require "$file.pm";
}

# ISA
if ($flag) {
if ($flags[0]) {
my $caller = caller;
no strict 'refs';
push @{"${caller}::ISA"}, $flag;
push @{"${caller}::ISA"}, $flags[0];
_monkey_patch $caller, 'has', sub { attr($caller, @_) };
}

# Mojo modules are strict!
$_->import for qw(strict warnings utf8);
feature->import(':5.10');

# Signatures (Perl 5.20+)
if (($flags[1] || '') eq '-signatures') {
Carp::croak 'Subroutine signatures require Perl 5.20+' if $] < 5.020;
require experimental;
@_ = ('warnings', 'signatures');
goto &experimental::import;
}
}

sub new {
Expand Down Expand Up @@ -188,6 +196,17 @@ All three forms save a lot of typing.
push @ISA, 'SomeBaseClass';
sub has { Mojo::Base::attr(__PACKAGE__, @_) }
On Perl 5.20+ you can also append a C<-signatures> flag to all three forms and
enable support for L<subroutine signatures|perlsub/"Signatures">.
# Also enable signatures
use Mojo::Base -strict, -signatures;
use Mojo::Base -base, -signatures;
use Mojo::Base 'SomeBaseClass', -signatures;
This will also disable experimental warnings on versions of Perl where this
feature was still experimental.
=head1 FUNCTIONS
L<Mojo::Base> implements the following functions, which can be imported with
Expand Down
15 changes: 14 additions & 1 deletion lib/Mojolicious/Lite.pm
Expand Up @@ -50,7 +50,8 @@ sub import {
Mojo::UserAgent::Server->app($app) unless Mojo::UserAgent::Server->app;

# Lite apps are strict!
Mojo::Base->import(-strict);
unshift @_, 'Mojo::Base', -strict;
goto &Mojo::Base::import;
}

1;
Expand Down Expand Up @@ -81,6 +82,18 @@ Mojolicious::Lite - Micro real-time web framework
L<Mojolicious::Lite> is a tiny domain specific language built around
L<Mojolicious>, made up of only about a dozen Perl functions.
On Perl 5.20+ you can also use a C<-signatures> flag to enable support for
L<subroutine signatures|perlsub/"Signatures">.
use Mojolicious::Lite -signatures;
get '/:foo' => sub ($c) {
my $foo = $c->param('foo');
$c->render(text => "Hello from $foo.");
};
app->start;
See L<Mojolicious::Guides::Tutorial> for more!
=head1 GROWING
Expand Down

0 comments on commit 9b1ea3c

Please sign in to comment.