Acme-UseStrict

 view release on metacpan or  search on metacpan

lib/Acme/UseStrict.pm  view on Meta::CPAN

package Acme::UseStrict;

use 5.010;
use strict;
use overload qw();
use match::smart qw(M);

BEGIN {
	$Acme::UseStrict::AUTHORITY = 'cpan:TOBYINK';
	$Acme::UseStrict::VERSION   = '1.235';
}

sub import
{
	my $class = shift;
	my ($test) = @_;
	$test //= 'use strict';
	
	my %overload = (
		q => sub {
			in_effect(1)
				and $_[1] |M| $test
				and $^H |= strict::bits(qw/refs subs vars/);
			return $_[1];
		},
	);
	overload::constant %overload;
	
	$^H{+__PACKAGE__} = 1;
}

sub unimport
{
	$^H{+__PACKAGE__} = 0;
}

sub in_effect
{
	my $level    = $_[0] // 0;
	my $hinthash = (caller($level))[10];
	return $hinthash->{+__PACKAGE__};
}

'use strict constantly';

__END__

=head1 NAME

Acme::UseStrict - use strict constantly

=head1 SYNOPSIS

  use Acme::UseStrict;
  # not in strict mode
  
  sub foo {
    "use strict";
    # in strict mode here
  }
  
  sub bar {
    no Acme::UseStrict;
    "use strict";
    # not in strict mode
  }

=head1 DESCRIPTION

ECMAScript 5.1 (i.e. Javascript) introduces a "strict mode" similar in
spirit to Perl's strict mode. Usually you enable Perl's strict mode like
this:

 use strict;

But in ECMAScript it must be a quoted string:

 "use strict";

It is received wisdom that Perl has an ugly syntax, so it naturally follows
that any change to make Perl's syntax closer to Javascript will be welcome.

This module allows you use use strict by simply including the string constant
"use strict" anywhere in a scope.

 sub do_stuff {
   warn "use strict";
   *{"do_more_stuff"} = sub { }; # dies because of strict refs.
 }

=head2 import

But what if you'd rather have a different trigger to enable strict mode?
Yes, that can be done:

 use Acme::UseStrict 'complain';
 
 sub do_stuff {
   my $foo = { complain => 'lots' };
   *{"do_more_stuff"} = sub { }; # dies because of strict refs.



( run in 1.587 second using v1.01-cache-2.11-cpan-5837b0d9d2c )