Commits

Toby Inkster committed eb63ada

add exported functions

Comments (0)

Files changed (3)

 	use Module::Runtime qw(use_module use_package_optimistically);
 	use namespace::sweep 0.006;
 	
+	sub import {
+		shift;
+		if (@_) {
+			require Ask::Functions;
+			unshift @_, 'Ask::Functions';
+			goto \&Ask::Functions::import;
+		}
+	}
+	
 	sub detect {
 		my $class  = shift;
 		my %args   = @_==1 ? %{$_[0]} : @_;
 	my $ask = Ask->detect(traits => ['AskX::Method::Password']);
 	say "GOT: ", $ask->password;
 
+=head2 Export
+
+You can optionally export the Ask methods as functions. The functions behave
+differently from the object-oriented interface in one regard; if called with
+one parameter, it's taken to be the "text" named argument.
+
+	use Ask qw( question info );
+	
+	if (question("Are you happy?")
+	and question("Do you know it?")
+	and question("Really want to show it?")) {
+		info("Then clap your hands!");
+	}
+
+Ask uses L<Sub::Exporter::Progressive>, so exported functions may be renamed:
+
+	use Ask
+		question => { -as => 'interrogate' },
+		info     => { -as => 'notify' },
+	;
+
 =head1 ENVIRONMENT
 
 The C<PERL_ASK_BACKEND> environment variable can be used to influence the

lib/Ask/Functions.pm

+use 5.010;
+use strict;
+use warnings;
+
+{
+	package Ask::Functions;
+	
+	our $ASK;
+	
+	sub _called {
+		$ASK //= do { require Ask; Ask->detect };
+		
+		my $method = shift;
+		unshift @_, 'text' if @_ % 2;
+		return $ASK->$method(@_);
+	}
+
+	my @F;
+	BEGIN {
+		@F = qw(info warning error entry question file_selection);
+		
+		eval qq{
+			sub $_ { unshift \@_, $_; goto \\&_called };
+		} for @F;
+	}
+
+	use Sub::Exporter::Progressive -setup => { exports => \@F };
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+Ask::Functions - guts behind Ask's exported functions
+
+=head1 SYNOPSIS
+
+	use Ask 'question';
+
+=head1 DESCRIPTION
+
+This module implements the exported functions for Ask. It is kept separate
+to avoid the functions polluting the namespace of the C<Ask> package.
+
+=head1 BUGS
+
+Please report any bugs to
+L<http://rt.cpan.org/Dist/Display.html?Queue=Ask>.
+
+=head1 SEE ALSO
+
+L<Ask>.
+
+=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.
+

meta/makefile.pret

 	requires          p`Moo`;
 	requires          p`namespace::sweep`;
 	requires          p`System::Command`;
+	requires          p`Sub::Exporter::Progressive`;
 .
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.