开发者

why is there a #!perl at the top of .t files?

开发者 https://www.devze.com 2023-01-30 15:50 出处:网络
I notice that there is a #!perl at the top of perl test, .t, as far as I 开发者_Go百科know you can\'t execute it as ./test.t because that would require them to be executable and use the full path. So

I notice that there is a #!perl at the top of perl test, .t, as far as I 开发者_Go百科know you can't execute it as ./test.t because that would require them to be executable and use the full path. So why do these files usually have this?


  1. To tell editors to syntax-highlight the file as Perl code.
  2. Optionally, to provide a default set of switches to perl when running this file. (See perlrun.)

(As an aside, on Unix systems, the extension is irrelevant. If the file has the execute bit set, the OS will consider the file a program, so you can run it. When you try to do that, the OS will look at the first line to determine which command (and arguments) to invoke the program with. However, this is irrelevant here as the OS won’t search $PATH for non-absolute paths – it will only search relative to your current directory, so #!perl will not work. You want an absolute path in practice.)


I would expect that, despite the fact that the files are not executable and even it were it would only execute perl if it was in your current directory, it is put there as the "file magic," identifying the file as a Perl file. The file command can read these magic numbers and show you what type each file is. It would require that the magic configuration file be set up to map this onto Perl which, on my Macintosh at least, is not typical. It does, however, also serve to clearly identify the file to human readers as well as editor programs, too. Imagine, that you didn't know it was a perl test and only saw the .t extension -- not much information there. Open it up and, viola, it's definitely a perl file.

0

精彩评论

暂无评论...
验证码 换一张
取 消