Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What means "not running under some shell" in Perl scripts?

Tags:

perl

cpan

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.

like image 961
Беров Avatar asked Feb 04 '12 19:02

Беров


1 Answers

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.

like image 126
jwodder Avatar answered Sep 22 '22 12:09

jwodder