weigand has asked for the wisdom of the Perl Monks concerning the following question:

I use linux. In one script ("/home/weigand/tmp.pl"), I do this:

#!/bin/env /home/weigand/perl_wrapper.pl print "Hello world from perl.\n";

My "/home/weigand/perl_wrapper.pl" script is just a perl script:

#!/usr/bin/perl print "In perl wrapper.\n"; ...

Question: While in the perl_wrapper.pl script, is there any way to get the name of the file "/home/weigand/tmp.pl" in this case?

The reason why I want to know is that I want the perl_wrapper.pl script to execute "perl /home/weigand/tmp.pl". It's a wrapper script for perl, so its job is to determine which perl binary to use and then execute the original script (/home/weigand/tmp.pl in this case) with the right perl binary.

Yes I know there are other ways of writing wrapper scripts. I'd like to stick with this particular question rather than solicit alternatives.

Yes, I know you can make symbolic links at /usr/local/bin to point to different perl binaries for different architectures. That's not what I want.

Any ideas?

I guess I could find the process ID of the parent (which is the /bin/env process I suppose). And then look at its open file list or something. Not sure exactly how to do that or if that's really my best shot.

Replies are listed 'Best First'.
Re: Getting the name of the file passed as STDIN from /bin/env?
by ikegami (Patriarch) on Mar 16, 2011 at 19:57 UTC

    The OS will call

    exec('/bin/env', '/home/weigand/perl_wrapper.pl', '/home/weigand/tmp.p +l')

    env will call

    exec('/home/weigand/perl_wrapper.pl', '/home/weigand/tmp.pl')

    The OS will call

    exec('/usr/bin/perl', '/home/weigand/perl_wrapper.pl', '/home/weigand/ +tmp.pl')

    So $ARGV[0].

    The funny part: It's /usr/bin/env on my system. To be portable, you'd have to use env to find env :)

      You're right! Haha. For some reason I thought it wasn't passed in $ARGV[0]. I think I was looking at $0 instead. Thank you thank you!