In many perl scripts (especially in famous CPAN distros) I find the following piece of code:
eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
if 0; # not running under some shell
I was wondering what it is for?
Thanks.
Some systems don't recognize the #!/usr/bin/perl
line at the start of scripts, and so trying to invoke a Perl program by name on such a system will simply pass the script to the shell. In order to combat this, portable Perl programs begin with a line that, when interpreted by a standard POSIX shell, causes the script to be passed to perl(1)
instead. The if 0
causes the line to be ignored when run by Perl itself, and placing it on a separate line causes shells to treat it as a separate command, running just the eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
as soon as it's read.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With