Plankton has asked for the wisdom of the Perl Monks concerning the following question:

I must be missing something here ...

die "Not enough disk space.\n" unless check_disk_space;
... causes error ...
Bareword "check_disk_space" not allowed while "strict subs" in use at +./thing line 60. Execution of ./thing aborted due to compilation errors.
... but ...
die "Not enough disk space.\n" if check_disk_space;
... does not.

Why?

Replies are listed 'Best First'.
Re: unless weirdness
by wfsp (Abbot) on May 08, 2009 at 05:50 UTC
    Works ok here.
    #!/usr/bin/perl use strict; use warnings; sub check_disk_space{0;} die "Not enough disk space.\n" unless check_disk_space;
    Not enough disk space.
    I think we need to see a bit more of your test script.
      My script is more like this ...
      #!/usr/bin/perl use strict; use warnings; die "Not enough disk space.\n" unless check_disk_space; sub check_disk_space{0;}
        In that case you must call your sub as follows :
        check_disk_space()
        From perldoc perlsub:
        A subroutine may be called using an explicit & prefix. The & is optional in modern Perl, as are parentheses if the subroutine has been predeclared.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        Doing it that way both if and unless will fail. If you don't declare the sub up front you need to call the sub with parens: check_disk_space(). There is some weirdness though. With if the error is
        Bareword "check_disk_space" not allowed while "strict subs" in use...
        and with unless
        Bareword "check_disk_space" not allowed while "strict subs" in use... Bareword "check_disk_space" not allowed while "strict subs" in use...
        Not sure why it comes up twice.

        I prefer to have all my subs at the end of the script and always call them with the parens. I use parens with perl built ins too if the args have any level of complexity. YMMV.

Re: unless weirdness
by JavaFan (Canon) on May 08, 2009 at 07:25 UTC
    Are you running 5.6.2 (or something even older)? I do see the same effects on 5.6.2. For 5.8.9 and 5.10.0, the following program
    use strict; use warnings; die "Not enough disk space.\n" unless check_disk_space; sub check_disk_space{0;} __END__
    gives me Bareword "check_disk_space" not allowed while "strict subs" twice when trying to run the program; if I replace the unless with if I only get it once. Still a difference, but no longer between compiling/not compiling.
Re: unless weirdness
by ikegami (Patriarch) on May 08, 2009 at 06:29 UTC

    I believe you are mistaken. I tried with 5.6.0, 5.6.1, 5.8.0, 5.8.8, 5.8.9 and 5.10.0, but I don't get what you get.

    >perl -we"use strict; 1 if func" Bareword "func" not allowed while "strict subs" in use at -e line 1. Execution of -e aborted due to compilation errors. >perl -we"use strict; 1 unless func" Bareword "func" not allowed while "strict subs" in use at -e line 1. Bareword "func" not allowed while "strict subs" in use at -e line 1. Execution of -e aborted due to compilation errors.

    In both case, the fix is to predeclare the sub (sub func;) or use parens in the call (func()). You can't omit parens on a sub call to sub that hasn't been declared.

Re: unless weirdness
by vinoth.ree (Monsignor) on May 08, 2009 at 06:17 UTC

    This problem I faced previously have a look on my node Function calling

    Vinoth,G