One use case, but certainly not the only, is when you want to write something to a log file for debugging purposes. In this case you may want to easily associate the name of the log file with the name of the originating file.
There are a few ways you can get the name of the file.
__FILE__ is a special token that returns the name of the file where it is. This works a lot like the similar in the C preprocessor. It substitutes the name of the current file.
As an example lets presume that a Perl script is written named file.pl. Inside of this script we call a subroutine called logit within a module called file.pm.
In the file.pm the logit module performs a simple print.
$ perl file.pl
file.pl # <- __FILE__ when called from file.pl
file.pm # <- __FILE__ when called from file.pm
The token will automatically update if you change the name of the file.
It returns the name of the file where it occurs.
If you place within a subroutine of a Perl Module then you will get the name of the PM, not the file where you call the subroutine. (I suppose this could equally be a pro)
$0 is a special variable in Perl. It contains the name of the program being executed. If we use the same example as above but slightly modify the file.pl we can see how this differs.
$ perl file.pl
file.pl # <- $0 when called from file.pl
file.pl # <- $0 when called from file.pm
The output from the Perl script and the package are now both the same.
The value will automatically update to the value of the command being executed.
Returns the command being executed regardless of the actual file it is located in.
If you place the within a subroutine of a Perl Module then you will get the name original command executed, not the file where you call the subroutine. (I suppose this could equally be a pro).