pythonperlexecutable

Is there a Perl equivalent to Python's `if __name__ == '__main__'`?


Is there a way to determine if the current file is the one being executed in Perl source? In Python we do this with the following construct:

if __name__ == '__main__':
    # This file is being executed.
    raise NotImplementedError

I can hack something together using FindBin and __FILE__, but I'm hoping there's a canonical way of doing this. Thanks!


Solution

  • unless (caller) {
      print "This is the script being executed\n";
    }
    

    See caller. It returns undef in the main script. Note that that doesn't work inside a subroutine, only in top-level code.