in reply to Re: unless weirdness
in thread unless weirdness

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;}

Replies are listed 'Best First'.
Re^3: unless weirdness
by CountZero (Bishop) on May 08, 2009 at 06:07 UTC
    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

      That quote is misleading. "&" has side-effects. It's purpose isn't to get around declaring a sub.
        Its purpose isn't to get around declaring a sub.

        Yes and no. It's an unambiguous parser hint that the following potential bareword isn't a bareword but a subroutine call. I hate it too, but that's one of its functions.

      Why doesn't that apply to ...
      die "Not enough disk space.\n" if check_disk_space;
      ... ?
Re^3: unless weirdness
by wfsp (Abbot) on May 08, 2009 at 06:13 UTC
    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.