allow indirection between less and its hints stash name
[p5sagit/p5-mst-13.2.git] / lib / less.pm
CommitLineData
a0d0e21e 1package less;
6d39ae0a 2use strict;
3use warnings;
f06db76b 4
6d39ae0a 5our $VERSION = '0.02';
6
7sub _pack_tags {
8 return join ' ', @_;
9}
10
11sub _unpack_tags {
12 return grep { defined and length }
13 map { split ' ' }
14 grep {defined} @_;
15}
16
a725df95 17sub stash_name { $_[0] }
18
6d39ae0a 19sub of {
20 my $class = shift @_;
21
22 # If no one wants the result, don't bother computing it.
23 return unless defined wantarray;
24
25 my $hinthash = ( caller 0 )[10];
26 my %tags;
a725df95 27 @tags{ _unpack_tags( $hinthash->{ $class->stash_name } ) } = ();
6d39ae0a 28
29 if (@_) {
30 exists $tags{$_} and return !!1 for @_;
31 return;
32 }
33 else {
34 return keys %tags;
35 }
36}
37
38sub import {
39 my $class = shift @_;
40
41 @_ = 'please' if not @_;
42 my %tags;
a725df95 43 @tags{ _unpack_tags( @_, $^H{ $class->stash_name } ) } = ();
6d39ae0a 44
45 $^H{$class} = _pack_tags( keys %tags );
46 return;
47}
48
49sub unimport {
50 my $class = shift @_;
51
52 if (@_) {
53 my %tags;
54 @tags{ _unpack_tags( $^H{$class} ) } = ();
55 delete @tags{ _unpack_tags(@_) };
56 my $new = _pack_tags( keys %tags );
57
58 if ( not length $new ) {
a725df95 59 delete $^H{ $class->stash_name };
6d39ae0a 60 }
61 else {
a725df95 62 $^H{ $class->stash_name } = $new;
6d39ae0a 63 }
64 }
65 else {
a725df95 66 delete $^H{ $class->stash_name };
6d39ae0a 67 }
68
69 return;
70}
71
aa96fdb0 721;
73
6d39ae0a 74__END__
b75c8c73 75
f06db76b 76=head1 NAME
77
6d39ae0a 78less - perl pragma to request less of something
cb1a09d0 79
80=head1 SYNOPSIS
81
6d39ae0a 82 use less 'CPU';
f06db76b 83
84=head1 DESCRIPTION
85
6d39ae0a 86This is a user-pragma. If you're very lucky some code you're using
87will know that you asked for less CPU usage or ram or fat or... we
88just can't know. Consult your documentation on everything you're
89currently using.
90
91For general suggestions, try requesting C<CPU> or C<memory>.
f06db76b 92
93 use less 'memory';
94 use less 'CPU';
95 use less 'fat';
96
6d39ae0a 97If you ask for nothing in particular, you'll be asking for C<less
98'please'>.
99
100 use less 'please';
101
102=head1 FOR MODULE AUTHORS
103
104L<less> has been in the core as a "joke" module for ages now and it
105hasn't had any real way to communicating any information to
106anything. Thanks to Nicholas Clark we have user pragmas (see
107L<perlpragma>) and now C<less> can do something.
108
109You can probably expect your users to be able to guess that they can
110request less CPU or memory or just "less" overall.
111
112If the user didn't specify anything, it's interpreted as having used
113the C<please> tag. It's up to you to make this useful.
114
115 # equivalent
116 use less;
117 use less 'please';
118
119=head2 C<< BOOLEAN = less->of( FEATURE ) >>
120
121The class method C<< less->of( NAME ) >> returns a boolean to tell you
122whether your user requested less of something.
123
124 if ( less->of( 'CPU' ) ) {
125 ...
126 }
127 elsif ( less->of( 'memory' ) ) {
128
129 }
130
131=head2 C<< FEATURES = less->of() >>
132
133If you don't ask for any feature, you get the list of features that
134the user requested you to be nice to. This has the nice side effect
135that if you don't respect anything in particular then you can just ask
136for it and use it like a boolean.
137
138 if ( less->of ) {
139 ...
140 }
141 else {
142 ...
143 }
144
145=head1 CAVEATS
146
147=over
148
149=item This probably does nothing.
150
151=item This works only on 5.10+
152
153At least it's backwards compatible in not doing much.
154
155=back
f06db76b 156
157=cut