Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do some functions in Perl have to be called with parens and others don't?

Tags:

perl

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 use 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 );
like image 327
xenoterracide Avatar asked Jul 22 '11 03:07

xenoterracide


3 Answers

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:

  1. 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.

  2. 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 in Test::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 $@;
}
like image 67
darch Avatar answered Sep 22 '22 05:09

darch


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";
}
like image 36
Joel Berger Avatar answered Sep 19 '22 05:09

Joel Berger


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;
like image 41
ysth Avatar answered Sep 22 '22 05:09

ysth