1. Toby Inkster
  2. p5-perl5-tobyink

Commits

Toby Inkster  committed 18ade43

perl5-tobyink

  • Participants
  • Branches default

Comments (0)

Files changed (4)

File Makefile.PL

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

File examples/stuff.pl

View file
+use perl5-tobyink;
+

File examples/trim.pl

View file
+use perl5-tobyink;
+
+normalize(my @strings = split /\n/, <<STRINGS);
+  Hello world
+Hello world   
+   Hello world   
+ Hello    world 
+STRINGS
+
+say "[$_]" for @strings;

File lib/perl5/tobyink.pm

View file
+package perl5::tobyink;
+
+use 5.010;
+use strict 1;
+no warnings 1;
+
+use IO::Handle 0;
+
+# perl5-hack
+sub imports { +__PACKAGE__ }
+
+BEGIN
+{
+	$perl5::tobyink::AUTHORITY = 'cpan:TOBYINK';
+	$perl5::tobyink::VERSION   = '1';
+	
+	@perl5::tobyink::EXPORT = qw(trim ltrim rtrim normalize);
+}
+
+{
+	our $norm = 1;
+	our $ltrim = 1;
+	our $rtrim = 1;
+	
+	sub normalize
+	{
+		if (not defined wantarray)
+		{
+			for (@_)
+			{
+				$_ =~ s{^\s+}{}g if $ltrim;
+				$_ =~ s{\s+$}{}g if $rtrim;
+				$_ =~ s{\s+}{ }g if $norm;
+			}
+			return;
+		}
+		
+		my @strings = @_;
+		for (@strings)
+		{
+			$_ =~ s{^\s+}{}g if $ltrim;
+			$_ =~ s{\s+$}{}g if $rtrim;
+			$_ =~ s{\s+}{ }g if $norm;
+		}
+		
+		if (@strings==1 and not wantarray)
+		{
+			return $strings[0];
+		}
+		
+		return @strings;
+	}
+	
+	sub trim
+	{
+		local $norm = 0;
+		if (defined wantarray)
+		{
+			return normalize(@_);
+		}
+		normalize(@_);
+		1;
+	}
+
+	sub ltrim
+	{
+		local $norm  = 0;
+		local $rtrim = 0;
+		if (defined wantarray)
+		{
+			return normalize(@_);
+		}
+		normalize(@_);
+		1;
+	}
+	
+	sub rtrim
+	{
+		local $norm  = 0;
+		local $ltrim = 0;
+		if (defined wantarray)
+		{
+			return normalize(@_);
+		}
+		normalize(@_);
+		1;
+	}
+}
+
+sub IMPORT
+{
+	strict->unimport;
+	warnings->unimport;
+
+	# stolen from common::sense...
+	# use warnings qw(FATAL closed threads internal debugging pack
+	#                 portable prototype inplace io pipe unpack malloc
+	#                 deprecated glob digit printf layer
+	#                 reserved taint closure semicolon);
+	${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\x3c\x3f\x33\x00\x0f\xf0\x0f\xc0\xf0\xfc\x33\x00";
+	
+	# use strict qw(subs vars);
+	$^H |= 0x000600;
+
+	# use feature ':5.10';
+	$^H{feature_switch} =
+	$^H{feature_say}    =
+	$^H{feature_state}  = 1;
+	
+	my $caller = caller;
+	no strict 'refs';
+	foreach my $e (@perl5::tobyink::EXPORT)
+	{
+		*{"$caller\::$e"} = \&{$e};
+	}
+}
+
+use Object::Tap 0 -package => 'UNIVERSAL';
+
+###
+### Inject these "use" lines into our caller.
+###
+use Syntax::Collector 0.003 -collect => q/
+use Carp 0 qw(carp croak confess);
+use List::Util 0 qw(first max min reduce shuffle);
+use List::MoreUtils 0 qw(uniq);
+use Path::Class 0 qw(file dir);
+use POSIX 0 qw(floor ceil);
+use Scalar::Util 0 qw(blessed isweak looks_like_number refaddr reftype weaken);
+use Syntax::Feature::Function 0 options => { -as => 'function' };
+use Syntax::Feature::Maybe 0;
+use Syntax::Feature::Perform 0;
+use Syntax::Feature::Ql 0;
+use Syntax::Feature::Qwa 0;
+use utf8::all 0;
+/;
+
+{
+	package
+	Class::Path::Dir;
+	use overload '@{}' => 'children';
+}
+
+__FILE__
+__END__
+
+=head1 NAME
+
+perl5::tobyink - an lightweight collection of syntax extensions
+
+=head1 SYNOPSIS
+
+ use perl5-tobyink;
+
+=head1 DESCRIPTION
+
+C<< use perl5-tobyink >> is roughly equivalent to:
+
+ use 5.010;
+ use Carp qw(carp croak confess);
+ use IO::Handle;
+ use List::Util 0 qw(first max min reduce shuffle);
+ use List::MoreUtils 0 qw(uniq);
+ use Object::Tap 0 -package => 'UNIVERSAL';
+ use Path::Class qw(file dir);
+ use POSIX qw(floor ceil);
+ use Scalar::Util qw(blessed isweak looks_like_number refaddr reftype weaken);
+ use common::sense;
+ use syntax 'function' => { -as => 'function' };
+ use syntax qw(maybe perform ql qwa);
+ use utf8::all;
+ *trim      = \&perl5::tobyink::trim;
+ *ltrim     = \&perl5::tobyink::ltrim;
+ *rtrim     = \&perl5::tobyink::rtrim;
+ *normalize = \&perl5::tobyink::normalize;
+
+=head1 BUGS
+
+Please report any bugs to
+L<http://rt.cpan.org/Dist/Display.html?Queue=perl5-tobyink>.
+
+=head1 SEE ALSO
+
+L<perl5>, L<common::sense>, L<Modern::Perl>.
+
+=head1 AUTHOR
+
+Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
+
+=head1 COPYRIGHT AND LICENCE
+
+This software is copyright (c) 2012 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.
+
+=begin private
+
+=item C<IMPORT>
+
+=end private