An example to illustrate is the Synopsis of my own Test::Version
.
use Test::More;
use Test::Version 0.04;
# test blib or lib by default
version_all_ok();
done_testing;
I don't have to include parenthesis on done_testing();
I can simply call it. However when I've tried to call version_all_ok;
( note: First attempt at Dist::Zilla::Plugin::Test::Version failed this way) I get an error. Why is this?
Update Perhaps my example is not quite as good as I've thought. The actual error I've gotten is
Bareword "version_all_ok" not allowed while "strict subs" in u开发者_StackOverflow中文版se at t/release-test-version.t line 19.
and here's the full code
#!/usr/bin/perl
BEGIN {
unless ($ENV{RELEASE_TESTING}) {
require Test::More;
Test::More::plan(skip_all => 'these tests are for release candidate testing');
}
}
use 5.006;
use strict;
use warnings;
use Test::More;
eval "use Test::Version";
plan skip_all => "Test::Version required for testing versions"
if $@;
version_all_ok; # of course line 19, and version_all_ok() works here.
done_testing;
The following should be relevant snippets pulled from Test::Version 1.0.0
for exportation.
use parent 'Exporter';
our @EXPORT = qw( version_all_ok ); ## no critic (Modules::ProhibitAutomaticExportation)
our @EXPORT_OK = qw( version_ok );
Fundamentally, because Perl needs to know that a bareword means a function call in order to parse it as a function call. There are two ways Perl might learn this interesting fact:
You might have decorated the bareword like a function call, prepending
&
or->
or appending(...)
or both. Perl will trust that you know what you're talking about and parse the bareword as a function call even if it doesn't yet know what function it will have to call.You might have declared a function with that name before Perl tries to parse the call. Ordinarily,
use
-ing a module is enough to ensure the symbols get created at the right time; you're doing something wrong inTest::Version
such that the symbol is not getting exported until after it is needed to compile the test script.
In your code, you wrap the use
inside an eval
, which effectively delays it until execution time. Consequently, the symbol version_all_ok
is not available when Perl tries to compile the call and it blows up. Forcing the eval
to compile time should suffice to make the symbol available:
BEGIN {
eval "use Test::Version";
plan skip_all => "Test::Version required for testing versions"
if $@;
}
This example shows (clearly I think) that all you need is to predeclare the function.
#!/usr/bin/env perl
use strict;
use warnings;
sub hi {
print "hi\n";
}
hi; #could be `hi();`
bye(); #could not be `bye;`
sub bye {
print "bye\n";
}
If your sensibilities require that you define your subroutines at the bottom, but you want them to be callable without parens (as though predeclared), you may use the subs
pragma:
#!/usr/bin/env perl
use strict;
use warnings;
use subs qw/hi bye/;
hi;
bye;
sub hi {
print "hi\n";
}
sub bye {
print "bye\n";
}
UPDATE:
It would appear that the subs
pragma can even alleviate problems from string evals. You might try a use subs 'version_all_ok';
near the top of your script. My proof of concept:
#!/usr/bin/env perl
use strict;
use warnings;
use subs qw/hi bye/;
eval <<'DECLARE';
sub bye {
print "bye\n";
}
DECLARE
hi;
bye;
sub hi {
print "hi\n";
}
I can't duplicate this using Test::Version 1.0.0 or 0.04. Is it possible you weren't exporting what you thought you were?
Can you double check and provide both the full script that failed, the error message, and full script that succeeded, and the perl version you are using?
Update: ok, you are loading Test::Version at runtime; that means that when version_all_ok
is encountered at compile time, there is no such subroutine. There isn't any way around this without modifying the test script in some way, such as:
my $has_test_version;
BEGIN { $has_test_version = eval "use Test::Version; 1" }
plan skip_all => "Test::Version required for testing versions" if ! $has_test_version;
It just needs to be declared before your use, still using the parentheses or ampersand should be used for distinction and clarity.
It's allowed if the function has been declared before, and it would be treated as a list operator (WARNING: it could change operator precedence!)
From Programming Perl, chapter 29 Functions:
Predefined Perl functions may be used either with or without parentheses around their arguments; the syntax summaries in this chapter omit the parentheses. If you do use parentheses, the simple but occasionally surprising rule is this: if it looks like a function, then it is a function, so precedence doesn't matter. Otherwise, it's a list operator or unary operator, and precedence does matter. Be careful, because even if you put whitespace between the keyword and its left parenthesis, that doesn't keep it from being a function
Found on p.677 (missing online at Google Books due to copyright) -- every Perl programmer should have the camel book.
精彩评论