10 use Symbol qw/gensym/;
12 use Variable::Magic qw/wizard cast dispell getdata/;
16 subs::auto - Read barewords as subroutine names.
24 our $VERSION = '0.05';
30 foo; # Compile to "foo()" instead of "'foo'"
31 # or croaking on strict subs
32 foo $x; # Compile to "foo($x)" instead of "$x->foo"
33 foo 1; # Compile to "foo(1)" instead of croaking
34 foo 1, 2; # Compile to "foo(1, 2)" instead of croaking
36 foo->meth; # "'foo'->meth" if you have use'd foo somewhere,
37 # or "foo()->meth" otherwise
38 print foo 'wut'; # print to the filehandle foo if it's actually one,
39 # or "print(foo('wut'))" otherwise
40 } # ... but function calls will fail at run-time if you don't
41 # actually define foo somewhere
47 This pragma lexically enables the parsing of any bareword as a subroutine name, except those which corresponds to an entry in C<%INC> (expected to be class names) or whose symbol table entry has an IO slot (expected to be filehandles).
49 You can pass options to C<import> as key / value pairs :
57 Specifies on which package the pragma should act. Setting C<$pkg> to C<Some::Package> allows you to resolve all functions name of the type C<Some::Package::func ...> in the current scope. You can use the pragma several times with different package names to allow resolution of all the corresponding barewords. Defaults to the current package.
61 This module is B<not> a source filter.
66 unless (Variable::Magic::VMG_UVAR) {
68 Carp::croak('uvar magic not available');
74 @B::Keywords::Barewords,
75 @B::Keywords::Functions,
78 delete @core{qw/my local/};
81 *_REFCNT_PLACEHOLDERS = eval 'sub () { ' . ($] < 5.011002 ? 0 : 1) . '}'
84 my $tag = wizard data => sub { \(my $data = _REFCNT_PLACEHOLDERS ? 2 : 1) };
87 my ($pkg, $func) = @_;
89 my $fqn = join '::', @_;
96 if ($cb and defined(my $data = getdata(&$cb, $tag))) {
102 for (qw/SCALAR ARRAY HASH IO FORMAT/) {
104 *$sym = *$fqn{$_} if defined *$fqn{$_}
112 (undef, my $data, my $func) = @_;
114 return if $data->{guard} or $func =~ /::/ or exists $core{$func};
115 local $data->{guard} = 1;
117 my $hints = (caller 0)[10];
118 if ($hints and $hints->{+(__PACKAGE__)}) {
119 my $pm = $func . '.pm';
120 return if exists $INC{$pm};
122 my $fqn = $data->{pkg} . '::' . $func;
123 my $cb = do { no strict 'refs'; *$fqn{CODE} };
125 if (_REFCNT_PLACEHOLDERS and defined(my $data = getdata(&$cb, $tag))) {
130 return if do { no strict 'refs'; *$fqn{IO} };
133 my ($file, $line) = (caller 0)[1, 2];
134 ($file, $line) = ('(eval 0)', 0) unless $file && $line;
135 die "Undefined subroutine &$fqn called at $file line $line\n";
142 _reset($data->{pkg}, $func);
149 (undef, my $data, my $func) = @_;
151 return if $data->{guard};
152 local $data->{guard} = 1;
154 _reset($data->{pkg}, $func);
159 my $wiz = wizard data => sub { +{ pkg => $_[1], guard => 0 } },
170 [A-Za-z_][A-Za-z0-9_]*
171 (?:::[A-Za-z_][A-Za-z0-9_]*)*
177 my ($pkg, $cur) = @_;
179 return $cur unless defined $pkg;
181 if (ref $pkg or $pkg !~ $pkg_rx) {
183 Carp::croak('Invalid package name');
187 $pkg = $cur . $pkg if $pkg eq '' or $pkg =~ /^::/;
195 Carp::croak('Optional arguments must be passed as keys/values pairs');
199 my $cur = (caller 1)[0];
200 my $in = _validate_pkg $args{in}, $cur;
204 cast %{$in . '::'}, $wiz, $in;
207 $^H{+(__PACKAGE__)} = 1;
214 $^H{+(__PACKAGE__)} = 0;
221 dispell %{$_ . '::'}, $wiz for keys %pkgs;
231 C<*{'::foo'}{CODE}> will appear as defined in a scope where the pragma is enabled, C<foo> is used as a bareword, but is never actually defined afterwards. This may or may not be considered as Doing The Right Thing. However, C<*{'::foo'}{CODE}> will always return the right value if you fetch it outside the pragma's scope. Actually, you can make it return the right value even in the pragma's scope by reading C<*{'::foo'}{CODE}> outside (or by actually defining C<foo>, which is ultimately why you use this pragma, right ?).
233 You have to open global filehandles outside of the scope of this pragma if you want them not to be treated as function calls. Or just use lexical filehandles and default ones as you should be.
239 L<Carp> (standard since perl 5), L<Symbol> (since 5.002).
241 L<Variable::Magic> with C<uvar> magic enabled (this should be assured by the required perl version).
247 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
249 You can contact me by mail or on C<irc.perl.org> (vincent).
253 Please report any bugs or feature requests to C<bug-subs-auto at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=subs-auto>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
257 You can find documentation for this module with the perldoc command.
261 Tests code coverage report is available at L<http://www.profvince.com/perl/cover/subs-auto>.
263 =head1 ACKNOWLEDGEMENTS
265 Thanks to Sebastien Aperghis-Tramoni for helping to name this pragma.
267 =head1 COPYRIGHT & LICENSE
269 Copyright 2008 Vincent Pit, all rights reserved.
271 This program is free software; you can redistribute it and/or modify it
272 under the same terms as Perl itself.
276 1; # End of subs::auto