Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the exact meaning of the find2perl perl shebang + eval?

Tags:

perl

eval

What exacly do the following?

#! /usr/bin/perl -w
    eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
        if 0; #$running_under_some_shell
  • the if 0 is never true, so the eval part will never executed,
  • and the eval is strange too - what is the value of $0 in this context (inside single quotes?)

Ps: taken from the result of the find2perl command

like image 651
jm666 Avatar asked Feb 24 '23 02:02

jm666


1 Answers

Best guess - as in this comment #$running_under_some_shell, it's to detect if the script is being run by some shell other than perl, e.g. bash.

the if 0 is never true, so the eval part will never executed,

Not by perl, no. By other shells such as bash it won't spot the line continuation and will just execute the eval statement. This then re-runs the script under perl. (Oddly with different options than the hashbang line.)

and the eval is strange too - what is the value of $0 in this context (inside single quotes?)

Again, this will be expanded by bash not perl: here it means the path to find2perl to pass into the perl interpreter.

like image 89
Rup Avatar answered Mar 05 '23 04:03

Rup