Commits

Toby Inkster committed b2ca845

Initial version

  • Participants

Comments (0)

Files changed (11)

+use inc::Module::Package 'RDF:standard';
+

File lib/Acme/UseStrict.pm

+package Acme::UseStrict;
+
+use 5.010;
+use strict;
+use overload;
+use Object::AUTHORITY;
+
+BEGIN {
+	$Acme::UseStrict::AUTHORITY = 'cpan:TOBYINK';
+	$Acme::UseStrict::VERSION   = '0.995';
+}
+
+sub import
+{
+	my ($class, $test) = @_;
+	$test //= 'use strict';		
+	my %overload = (
+		'q' => sub
+			{
+				$^H |= strict::bits(qw/refs subs vars/)
+					if in_effect(1) && $_[1] ~~ $test;
+				return $_[1];
+			},
+		);
+	overload::constant %overload;
+	
+	$^H{+__PACKAGE__} = 1;
+}
+
+sub unimport
+{
+	$^H{+__PACKAGE__} = 0;
+}
+
+sub in_effect
+{
+	my $level    = shift // 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.
+ }
+
+You can even provide a regular expression:
+
+ use Acme::UseStrict qr/^(complain|whine|moan|grumble)$/i;
+
+Or an list of values:
+
+ use Acme::UseStrict [qw/complain whine moan grumble/];
+
+Or basically anything that works as a right-hand-side with the smart match
+operator.
+
+=head2 unimport
+
+You can disable this module for a lexical scope using:
+
+ no Acme::UseStrict;
+
+=head2 in_effect
+
+You can check if this module is enabled:
+
+ warn Acme::UseStrict::in_effect()
+   ? 'mind your language'
+   : 'curse freely';
+
+Note that this checks if B<this module> is enabled; not if strict is enabled.
+
+=head1 BUGS
+
+Please report any bugs to
+L<http://rt.cpan.org/Dist/Display.html?Queue=Acme-UseStrict>.
+
+=head1 SEE ALSO
+
+L<https://developer.mozilla.org/en/JavaScript/Strict_mode>.
+
+=head1 AUTHOR
+
+Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
+
+=head1 COPYRIGHT AND LICENCE
+
+This software is copyright (c) 2011 by Toby Inkster.
+
+This is free software; you can redistribute it and/or modify it under
+the same terms as the Perl 5 programming language system itself.
+
+=head1 DISCLAIMER OF WARRANTIES
+
+THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+

File meta/changes.ttl

+# This file acts as the project's changelog.
+
+@prefix :        <http://usefulinc.com/ns/doap#> .
+@prefix dcs:     <http://ontologi.es/doap-changeset#> .
+@prefix dc:      <http://purl.org/dc/terms/> .
+@prefix dist:    <http://purl.org/NET/cpan-uri/dist/Acme-UseStrict/> .
+@prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
+@prefix xsd:     <http://www.w3.org/2001/XMLSchema#> .
+
+dist:project :release dist:v_0-995 .
+dist:v_0-995
+	a               :Version ;
+	dc:issued       "2011-12-06"^^xsd:date ;
+	:revision       "0.995"^^xsd:string ;
+	:file-release   <http://backpan.cpan.org/authors/id/T/TO/TOBYINK/Acme-UseStrict-0.995.tar.gz> ;
+	rdfs:label      "Initial release" .
+

File meta/doap.ttl

+# This file contains general metadata about the project.
+
+@prefix :        <http://usefulinc.com/ns/doap#> .
+@prefix dc:      <http://purl.org/dc/terms/> .
+@prefix foaf:    <http://xmlns.com/foaf/0.1/> .
+@prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
+@prefix xsd:     <http://www.w3.org/2001/XMLSchema#> .
+
+<http://purl.org/NET/cpan-uri/dist/Acme-UseStrict/project>
+	a               :Project ;
+	:programming-language "Perl" ;
+	:name           "Acme-UseStrict" ;
+	:shortdesc      "use strict constantly" ;
+	:homepage       <https://metacpan.org/release/Acme-UseStrict> ;
+	:download-page  <https://metacpan.org/release/Acme-UseStrict> ;
+	:bug-database   <http://rt.cpan.org/Dist/Display.html?Queue=Acme-UseStrict> ;
+	:repository     [ a :HgRepository ; :browse <https://bitbucket.org/tobyink/p5-acme-usestrict> ] ;
+	:created        "2011-12-06"^^xsd:date ;
+	:license        <http://dev.perl.org/licenses/> ;
+	:developer      [ a foaf:Person ; foaf:name "Toby Inkster" ; foaf:mbox <mailto:tobyink@cpan.org> ] .
+
+<http://dev.perl.org/licenses/>
+	dc:title        "the same terms as the perl 5 programming language system itself" .
+

File meta/makefile.ttl

+# This file provides instructions for packaging.
+
+@prefix : <http://purl.org/NET/cpan-uri/terms#> .
+
+<http://purl.org/NET/cpan-uri/dist/Acme-UseStrict/project>
+	:perl_version_from _:main ;
+	:version_from _:main ;
+	:readme_from _:main ;
+	:test_requires "Test::More 0.61" , "Test::Exception" ;
+	:requires "Object::AUTHORITY", "strict", "overload" .
+
+_:main <http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#fileName> "lib/Acme/UseStrict.pm" .
+
+use Test::More tests => 1;
+BEGIN { use_ok('Acme::UseStrict') };
+

File t/02strictness.t

+use Test::Exception tests => 2;
+use Acme::UseStrict qr/use strict/i;
+no strict;
+
+lives_ok {
+	no Acme::UseStrict;
+	*{"foo"} = sub {};
+	foo();
+	'use strict';
+	*{"bar"} = sub {};
+	bar();
+}
+	'Lives OK';
+
+throws_ok {
+	*{"foo"} = sub {};
+	foo();
+	'USE STRICT';
+	*{"bar"} = sub {};
+	bar();
+}
+	qr/symbol ref/,
+	'Throws OK';
+use Test::More;
+eval "use Test::Pod 1.00";
+plan skip_all => "Test::Pod 1.00 required for testing POD" if $@;
+all_pod_files_ok();
+

File xt/02pod_coverage.t

+use Test::More;
+use Test::Pod::Coverage;
+
+my @modules = qw(Acme::UseStrict);
+pod_coverage_ok($_, "$_ is covered")
+	foreach @modules;
+done_testing(scalar @modules);
+

File xt/03meta_uptodate.t

+use Test::More tests => 1;
+use Test::RDF::DOAP::Version;
+doap_version_ok('Acme-UseStrict', 'Acme::UseStrict');
+
+use Test::EOL;
+all_perl_files_ok();