in reply to unless weirdness

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.

Replies are listed 'Best First'.
Re^2: unless weirdness
by Plankton (Vicar) on May 08, 2009 at 06:02 UTC
    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

        That quote is misleading. "&" has side-effects. It's purpose isn't to get around declaring a sub.
        Why doesn't that apply to ...
        die "Not enough disk space.\n" if check_disk_space;
        ... ?
      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.