Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: exit this way

by sundialsvc4 (Abbot)
on Sep 01, 2015 at 13:57 UTC ( [id://1140667]=note: print w/replies, xml ) Need Help??


in reply to exit this way

Another strategy that I have sometimes seen used goes something like this:

#!/usr/bin/perl exit main(); sub main { ... }

Even though the Perl language does not have the concept of a main() subroutine, these authors created one, and ensured that only it would be executed.   An interesting idea ...

Replies are listed 'Best First'.
Re^2: exit this way
by Your Mother (Archbishop) on Sep 01, 2015 at 14:32 UTC

    This seems like a mistake or a bad practice in Perl. exit codes are a complete mismatch for subroutine return expectations. I suppose something like this might work but still… I say, nay. Also neigh.

    #!/usr/bin/env perl exit ! main(); sub main { $success = [ undef, "true" ]->[rand 2] }

      Boiler plate code for the build scripts in our build system looks like:

      package main; my $build = BuildGeneral->new(buildtype => 'release'); exit(!$build || $build->runFailed());

      which often causes a double take at first glance, but accurately reflects the sense of the exit value.

      Premature optimization is the root of all job security

      Hi, Your Mother. Can you please explain this snippet? It does print out the randomly assigned value of $success if I change it to:

      exit ! main(); sub main { $success = [ undef, "true" ]->[rand 2]; print "$success\n" +}
      But it appears to produce the same output when I omit the !:
      exit main(); sub main { $success = [ undef, "true" ]->[rand 2]; print "$success\n" +}
      Which appears to be the same code as The Vague One posted ...

      What is the function of the bang?

      The way forward always starts with a minimal test.

        :P The last expression of a sub/block/package is its return value; when there is no explicit return. So that little snippet just rotates returning "true" and "false" randomly; idiom: return 0 or 1 element of anon array with 2 elements, one true, one false. By adding a print you made the sub always return "true."

        exit values are approximately opposed to return/success values in most Perl. The bang (!) performs this inversion. exit == 0 means success, any other value (-1, 1 .. 255) means some flavor of fail (on *nix anyway). Why I suggest it is cognitively a bad idea for a Perl script. To test the original, try these instead (with 1140673.pl being the first scriptlet I posted)-

        moo@cow~>1140673.pl && echo "I CAN HAZ SUCCEED?" moo@cow~>1140673.pl || echo "FAIL!"

      In many other languages, main is "special". The unnamed programmers were attempting to make a facsimile of that "specialness".

      Perhaps:

      #!perl use strict; use warnings; main(); exit 99; # or other reasonable default exit value

      might be better.

        Yes, and the attempt at the idiom is a reasonable idea, I have in fact used it for service/daemon style scripts in the past, but exit 0; # undef works too is the only "correct" exit code. Everything else is an error state. Since returning false is suggestive of an error in Perl I am reticent to put that lipstick on this pig... or this pig stick on those lips... And that's the way the news goes!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1140667]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-24 03:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found