Skip to content

Commit

Permalink
Implement a use_warnings flag for the Mason compiler
Browse files Browse the repository at this point in the history
This allows you to catch runtime warnings in all Mason templates by
enabling a global use_warnings switch, similar to the use_strict
switch. Unlike use_strict this is off by default, so it's fully
backwards compatible.

I have a huge codebase where I'd like to enable warnings by default, and
not have to turn them on one-by-one for each template, which would
be *really* painful.
  • Loading branch information
avar committed Oct 2, 2013
1 parent 426f5d7 commit 690b158
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/HTML/Mason/Compiler/ToObject.pm
Expand Up @@ -46,6 +46,10 @@ BEGIN
{ parse => 'boolean', type => SCALAR, default => 1,
descr => "Whether to turn on Perl's 'strict' pragma in components" },

use_warnings =>
{ parse => 'boolean', type => SCALAR, default => 0,
descr => "Whether to turn on Perl's 'warnings' pragma in components" },

define_args_hash =>
{ parse => 'string', type => SCALAR, default => 'auto',
regex => qr/^(?:always|auto|never)$/,
Expand All @@ -67,6 +71,7 @@ use HTML::Mason::MethodMaker
preamble
subcomp_class
use_strict
use_warnings
)
],
);
Expand Down Expand Up @@ -300,6 +305,7 @@ sub _make_main_header

return join '', ( "package $pkg;\n",
$self->use_strict ? "use strict;\n" : "no strict;\n",
$self->use_warnings ? "use warnings;\n" : "",
sprintf( "use vars qw(\%s);\n",
join ' ', '$m', $self->allow_globals ),
$self->_blocks('once'),
Expand Down Expand Up @@ -690,6 +696,11 @@ C<$m> in postamble code.
True or false, default is true. Indicates whether or not a given
component should C<use strict>.
=item use_warnings
True or false, default is false. Indicates whether or not a given
component should C<use warnings>.
=item named_component_subs
When compiling a component, use uniquely named subroutines for the a
Expand Down
21 changes: 20 additions & 1 deletion lib/HTML/Mason/Params.pod
Expand Up @@ -1458,4 +1458,23 @@ may be more appropriate for in-depth debugging sessions.
=back

True or false, default is true. Indicates whether or not a given
component should C<use strict>.
component should C<use strict>.

=head2 use_warnings

=over 4

=item * Perl name: use_warnings

=item * Apache name: MasonUseWarnings

=item * Type in httpd.conf: boolean

=item * Default: 1

=item * Belongs to: C<HTML::Mason::Compiler::ToObject>

=back

True or false, default is false. Indicates whether or not a given
component should C<use warnings>.
30 changes: 30 additions & 0 deletions t/06-compiler.t
Expand Up @@ -1131,6 +1131,36 @@ EOF
expect_warnings => qr/Use of uninitialized value.+in addition/,
);

#------------------------------------------------------------

$group->add_test( name => 'warnings_do_not_need_explicit_enabling_on_use_warnings',
interp_params => { use_warnings => 1 },
description => "Make sure that warnings _are_ generated on use_warnings for other bad use of uninit",
component => <<'EOF',
% my $x;
use_warnings is <% $x + 2 %>
EOF
expect => <<'EOF',
use_warnings is 2
EOF
expect_warnings => qr/Use of uninitialized value.+in addition/,
);

#------------------------------------------------------------

$group->add_test( name => 'warnings_do_not_need_explicit_enabling_without_autoflush_on_use_warnings',
description => "Make sure that warnings _are_ generated on use_warnings for other bad use of uninit when enable_autoflush is off",
interp_params => { enable_autoflush => 0, use_warnings => 1 },
component => <<'EOF',
% my $x;
use_warnings is <% $x + 2 %>
EOF
expect => <<'EOF',
use_warnings is 2
EOF
expect_warnings => qr/Use of uninitialized value.+in addition/,
);

#------------------------------------------------------------

$group->add_test( name => 'unbalanced_content_block_error',
Expand Down

0 comments on commit 690b158

Please sign in to comment.