10 subs::auto - Read barewords as subroutine names.
27 foo; # Compile to "foo()" instead of "'foo'"
28 # or croaking on strict subs
29 foo $x; # Compile to "foo($x)" instead of "$x->foo"
30 foo 1; # Compile to "foo(1)" instead of croaking
31 foo 1, 2; # Compile to "foo(1, 2)" instead of croaking
33 foo->meth; # "'foo'->meth" if you have use'd foo somewhere,
34 # or "foo()->meth" otherwise
35 print foo 'wut'; # print to the filehandle foo if it's actually one,
36 # or "print(foo('wut'))" otherwise
37 } # ... but function calls will fail at run-time if you don't
38 # actually define foo somewhere
44 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).
46 You can pass options to C<import> as key / value pairs :
54 Specifies on which package the pragma should act.
55 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.
56 You can use the pragma several times with different package names to allow resolution of all the corresponding barewords.
58 Defaults to the current package.
62 This module is B<not> a source filter.
70 use Variable::Magic 0.31 qw/wizard cast dispell getdata/;
73 unless (Variable::Magic::VMG_UVAR) {
75 Carp::croak('uvar magic not available');
78 XSLoader::load(__PACKAGE__, $VERSION);
83 @B::Keywords::Barewords,
84 @B::Keywords::Functions,
87 delete @core{qw/my local/};
90 *_REFCNT_PLACEHOLDERS = eval 'sub () { ' . ($] < 5.011002 ? 0 : 1) . '}'
93 my $tag = wizard data => sub { \(my $data = _REFCNT_PLACEHOLDERS ? 2 : 1) };
96 my $fqn = join '::', @_;
104 if ($cb and defined(my $data = getdata(&$cb, $tag))) {
106 return if $$data > 0;
113 (undef, my $data, my $name) = @_;
115 return if $data->{guard};
116 local $data->{guard} = 1;
118 return if $name =~ /::/
119 or exists $core{$name};
121 my $op_name = $_[-1] || '';
122 return if $op_name =~ /method/;
124 my $pkg = $data->{pkg};
126 my $hints = (caller 0)[10];
127 if ($hints and $hints->{+(__PACKAGE__)}) {
128 my $pm = $name . '.pm';
129 return if exists $INC{$pm};
131 my $fqn = $pkg . '::' . $name;
132 my $cb = do { no strict 'refs'; *$fqn{CODE} };
134 if (_REFCNT_PLACEHOLDERS and defined(my $data = getdata(&$cb, $tag))) {
139 return if do { no strict 'refs'; *$fqn{IO} };
142 my ($file, $line) = (caller 0)[1, 2];
143 ($file, $line) = ('(eval 0)', 0) unless $file && $line;
144 die "Undefined subroutine &$fqn called at $file line $line\n";
158 (undef, my $data, my $name) = @_;
160 return if $data->{guard};
161 local $data->{guard} = 1;
163 _reset($data->{pkg}, $name);
168 my $wiz = wizard data => sub { +{ pkg => $_[1], guard => 0 } },
171 op_info => Variable::Magic::VMG_OP_INFO_NAME;
180 [A-Za-z_][A-Za-z0-9_]*
181 (?:::[A-Za-z_][A-Za-z0-9_]*)*
187 my ($pkg, $cur) = @_;
189 return $cur unless defined $pkg;
191 if (ref $pkg or $pkg !~ $pkg_rx) {
193 Carp::croak('Invalid package name');
197 $pkg = $cur . $pkg if $pkg eq '' or $pkg =~ /^::/;
205 Carp::croak('Optional arguments must be passed as keys/values pairs');
210 my $in = _validate_pkg $args{in}, $cur;
214 cast %{$in . '::'}, $wiz, $in;
217 $^H{+(__PACKAGE__)} = 1;
224 $^H{+(__PACKAGE__)} = 0;
231 dispell %{$_ . '::'}, $wiz for keys %pkgs;
241 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.
242 This may or may not be considered as Doing The Right Thing.
243 However, C<*{'::foo'}{CODE}> will always return the right value if you fetch it outside the pragma's scope.
244 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 ?).
246 You have to open global filehandles outside of the scope of this pragma if you want them not to be treated as function calls.
247 Or just use lexical filehandles and default ones as you should be.
249 This pragma doesn't propagate into C<eval STRING>.
255 L<Variable::Magic> with C<uvar> magic enabled (this should be assured by the required perl version).
259 L<Carp> (standard since perl 5), L<XSLoader> (since 5.006).
263 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
265 You can contact me by mail or on C<irc.perl.org> (vincent).
269 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>.
270 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
274 You can find documentation for this module with the perldoc command.
278 Tests code coverage report is available at L<http://www.profvince.com/perl/cover/subs-auto>.
280 =head1 ACKNOWLEDGEMENTS
282 Thanks to Sebastien Aperghis-Tramoni for helping to name this pragma.
284 =head1 COPYRIGHT & LICENSE
286 Copyright 2008,2009,2010,2011 Vincent Pit, all rights reserved.
288 This program is free software; you can redistribute it and/or modify it
289 under the same terms as Perl itself.
293 1; # End of subs::auto