1

I'm reading a mojo example from: https://mojolicious.org/ Below is the code:

use Mojolicious::Lite -signatures;

# Render template "index.html.ep" from the DATA section
get '/' => sub ($c) {
  $c->render(template => 'index');
};

......

My question is how to get this piece of code compiled by perl? Usually I'll code subcalls as:

get '/' => sub {
      my $c = shift;
      $c->render(template => 'index');
    };

This could be compiled well by strawberry perl. While the example code from mojo got errors:

D:\Source\test_code\mojotest>perl mojo3.pl daemon
Illegal character in prototype for ? : $c at mojo3.pl line 4.
Illegal character in prototype for ? : $c at mojo3.pl line 9.
Illegal character in prototype for ? : $c, $msg at mojo3.pl line 10.

Could anyone tell why?

simbabque
  • 53,749
  • 8
  • 73
  • 136
Hao
  • 418
  • 3
  • 8
  • 2
    What's the output of `perl -MMojolicious::Lite -le'print for $], $Mojolicious::Lite::VERSION'`? (Replace the single quotes with double quotes if using the `cmd` shell.) – ikegami May 22 '18 at 08:11
  • You are probably using a very new version of Mojolicious and the latest documentation, but your Perl is too old. – simbabque May 22 '18 at 10:19
  • I'm using latest strawberry, v5.26. – Hao May 22 '18 at 12:59
  • Could it be a problem about "prototypes", a subroutine that is defined as "sub fun($arg) { $arg....}". Is there any specifical settings in perl for it? – Hao May 22 '18 at 13:07
  • There's a new feature in recent Perls called _signatures_, which replaces the _prototypes_. Prototypes in Perl are `sub foo ($$$) { ... }` and tell Perl what kind (not type) of argument to expect. They are usually discouraged. Signatures allow you to do `sub foo ($bar, $baz) { ... }` instead of `sub foo { my ($bar, $baz) = @_; ... }`. The `-signatures` on the `use Mojo::Lite` should turn the feature on for you, but maybe your Mojo version is older than that feature? Which one is it? If @ikegami's code didn't help, do `perl -MMojolicious::Lite\ 99` with a space after the backslash ``\``. – simbabque May 22 '18 at 13:24
  • `Mojolicious::Lite -signatures` just enables the `signatures` feature introduced in Perl v5.20. To see if your perl can use this feature, see if this code compiles: `perl -Mexperimental=signatures -e "sub foo($x) { }"` – mob May 22 '18 at 13:31

0 Answers0