9

I have a simple Perl script that prints out @INC as the following:

#!/usr/bin/perl
print $_, "\n" for @INC;

I execute the script in 2 different ways with ./test.pl and perl test.pl, the output as the following:

[neevek@~/bin]$ ./test.pl 
/Library/Perl/5.12/darwin-thread-multi-2level
/Library/Perl/5.12
/Network/Library/Perl/5.12/darwin-thread-multi-2level
/Network/Library/Perl/5.12
/Library/Perl/Updates/5.12.3
/System/Library/Perl/5.12/darwin-thread-multi-2level
/System/Library/Perl/5.12
/System/Library/Perl/Extras/5.12/darwin-thread-multi-2level
/System/Library/Perl/Extras/5.12
.   
[neevek@~/bin]$ perl test.pl 
/opt/local/lib/perl5/site_perl/5.12.3/darwin-multi-2level
/opt/local/lib/perl5/site_perl/5.12.3
/opt/local/lib/perl5/vendor_perl/5.12.3/darwin-multi-2level
/opt/local/lib/perl5/vendor_perl/5.12.3
/opt/local/lib/perl5/5.12.3/darwin-multi-2level
/opt/local/lib/perl5/5.12.3
/opt/local/lib/perl5/site_perl
/opt/local/lib/perl5/vendor_perl
.   

My question is: what's behind the scenes for executing a perl script with ./script.pl and perl script.pl? what causes the script to output different @INC?

tchrist
  • 78,834
  • 30
  • 123
  • 180
neevek
  • 11,760
  • 8
  • 55
  • 73
  • 6
    Different `perl` binaries? What does `which perl` say? – cHao Mar 01 '12 at 13:11
  • `which perl` says `/opt/local/bin/perl`, but I used `/usr/bin/perl` for the shabang. you all are correct, thank you...but I could I have missed that... – neevek Mar 01 '12 at 13:17
  • 1
    Unix lesson #1 (that I give to all newbies in the team). NEVER EVER EVER rely on current directory or PATH. ALWAYS use explicit fully qualified path. – DVK Mar 02 '12 at 03:41
  • I’d say that relying on environment makes sense at least in some cases, for example it makes it easy to switch Perls with [Perlbrew](http://www.perlbrew.pl/). – zoul Mar 02 '12 at 09:06

2 Answers2

19

The script is executing perl from /usr/bin through the shebang line, but launching the script from the command line uses a different perl binary, from /opt/somewhere (see which perl for the path). You can use #!/usr/bin/env perl to make both options behave the same.

zoul
  • 102,279
  • 44
  • 260
  • 354
10

You have two perl installations on your system.

When you execute ./test.pl, it's /usr/bin/perl that is chosen, as written on the first line of your script.

When you execute perl test.pl, it's the first perl found in the PATH environment variable that is chosen. Type which perl to discover where it is.

Konerak
  • 39,272
  • 12
  • 98
  • 118
Stamm
  • 947
  • 5
  • 17