4

Is there a way to validate perl mason syntax at the command line? I know for regular perl modules you can just use perl -c, but that throws errors for mason-specific syntax like docstrings and the like...

For example:

<%doc>
DOCUMENTATION SHOULD NOT GET PARSED
</%doc>

<%args>
$args
</%args>

<%perl>
my $var = $args->{var};
</%perl>

is a valid perl mason file, but running perl -c against it returns:

Semicolon seems to be missing at path/to/file.mc line 1.
syntax error at path/to/file.mc line 2, near "DOCUMENTATION S"
path/to/file.mc had compilation errors.
therealmitchconnors
  • 2,732
  • 1
  • 18
  • 36
  • 3
    For a module, you should use `perl -e'use Module;'`. `perl -c Module.pm` can cause problems. – ikegami Jan 15 '15 at 19:38
  • You may be correct on that. I am new to perl, and have never needed this sort of command before. That being said, it definitely doesn't work for mason *.mc files, which is what I need. – therealmitchconnors Jan 15 '15 at 22:51
  • Didn't mean to imply it does. Mason is not Perl, so `perl` can't execute it. – ikegami Jan 16 '15 at 13:36

1 Answers1

10

I took a look at Mason, the first thing I see is Mason::App/mason.pl

$ mason junk.mason
Invalid attribute line '$args' at ./junk.mason line 6

Neat huh? But that actually tries to run the code, so next step is look inside, and after a little looking around, i find Mason::Interp which has load (path) which is documented as

Returns the component object corresponding to an absolute component path, or undef if none exists. Dies with an error if the component fails to load because of a syntax error.

So you can adapt Mason::App to load instead of run or make your own simple version like this

#!/usr/bin/perl --
use strict; use warnings; use Mason;
my $interp = Mason->new(
    comp_root => '/path/to/comps',
    data_dir  => '/path/to/data',
    ...
);
$interp->load( shift );
optional
  • 2,061
  • 12
  • 16