FindBin question

FindBin question

am 13.12.2006 13:56:12 von Ronny

I've a program myutil.pl, which uses FindBin to locate other
modules, in the following way:

#!/usr/local/bin/perl
# This is file myutil.pl !
use warnings;
use strict;
use FindBin;
use lib "$FindBin::Bin";
# Support.pm is in the same directory as myutil.pl
use Support; # <---- found via FindBin magic

This worked fine and I was very happy with this. Now I have a different
program,
newprog.pl, in a different directory, which is supposed to use
myutil.pl above
under the following assumptions:
- myutil.pl should be called via a call to system("...")
- It is known that myutil.pl happens to be in a directory which is not
in
$ENV{PATH}, but in newprog.pl's @INC

(I know that the second assumption is a bit esoteric, but in this
particular case
this is how it is).

Here is the excerpt from newprog.pl which invokes myutil.pl:

#!/usr/local/bin/perl
# This is file newprog.pl !
use lib "....";
......
# Temporarily append @INC to PATH when calling myutil.pl
system('PATH=$PATH:'.join(':',@INC)." myutil.pl");

Indeed, myutil.pl gets invoked, as expected, but then, myutil.pl claims
that
it is unable to find the module Support.pm . A look at @INC reveals
that
the directory of newprog.pl is incorporated in the @INC path, but not
that
of myutil.pl.

Maybe I misunderstood something in the way FindBin works. How do
I deal with this situation? In a short way, the files are located like
this:

$HOME/foo/myutil.pl
$HOME/foo/Support.pm
$HOME/bar/newprog.pl

with the following conditions:

- newprog.pl calls myutil.pl via system().
- myutil.pl uses Support.pm.
- The use statement in myutil.pl should not mention the path $HOME/foo,
so that
I can easily move the files around to different locations.
- The use statement in newprog.pl *does* mention the path $HOME/foo (no
need to be flexible here)
- $HOME/bar/newprog.pl and $HOME/foo/myutil.pl should both be callable
from the command line

Any ideas how to do this?

Ronald

Re: FindBin question

am 14.12.2006 01:12:17 von paduille.4060.mumia.w

On 12/13/2006 06:56 AM, Ronny wrote:
> I've a program myutil.pl, which uses FindBin to locate other
> modules, in the following way:
>
> #!/usr/local/bin/perl
> # This is file myutil.pl !
> use warnings;
> use strict;
> use FindBin;
> use lib "$FindBin::Bin";
> # Support.pm is in the same directory as myutil.pl
> use Support; # <---- found via FindBin magic
>
> This worked fine and I was very happy with this. Now I have a different
> program,
> newprog.pl, in a different directory, which is supposed to use
> myutil.pl above
> under the following assumptions:
> - myutil.pl should be called via a call to system("...")
> - It is known that myutil.pl happens to be in a directory which is not
> in
> $ENV{PATH}, but in newprog.pl's @INC
>
> (I know that the second assumption is a bit esoteric, but in this
> particular case
> this is how it is).
>
> Here is the excerpt from newprog.pl which invokes myutil.pl:
>
> #!/usr/local/bin/perl
> # This is file newprog.pl !
> use lib "....";
> ......
> # Temporarily append @INC to PATH when calling myutil.pl
> system('PATH=$PATH:'.join(':',@INC)." myutil.pl");
>

Perhaps try this in newprog.pl:

$ENV{PERL5LIB} =~ s{:?$}{:/path/above/myutil/script};
$ENV{PATH} =~ s{:?$}{:/path/above/myutil/script};
system 'myutil.pl';


> Indeed, myutil.pl gets invoked, as expected, but then, myutil.pl claims
> that
> it is unable to find the module Support.pm . A look at @INC reveals
> that
> the directory of newprog.pl is incorporated in the @INC path, but not
> that
> of myutil.pl.
>
> Maybe I misunderstood something in the way FindBin works. How do
> I deal with this situation? In a short way, the files are located like
> this:
>
> $HOME/foo/myutil.pl
> $HOME/foo/Support.pm
> $HOME/bar/newprog.pl
>
> with the following conditions:
>
> - newprog.pl calls myutil.pl via system().
> - myutil.pl uses Support.pm.
> - The use statement in myutil.pl should not mention the path $HOME/foo,
> so that
> I can easily move the files around to different locations.
> - The use statement in newprog.pl *does* mention the path $HOME/foo (no
> need to be flexible here)
> - $HOME/bar/newprog.pl and $HOME/foo/myutil.pl should both be callable
> from the command line
>
> Any ideas how to do this?
>
> Ronald
>

In addition to the above I wrote, you could also create a shell script
that sets PATH and PERL5INC before invoking newprog.pl.

Re: FindBin question

am 14.12.2006 10:11:00 von Ronny

> Perhaps try this in newprog.pl:
>
> $ENV{PERL5LIB} =~ s{:?$}{:/path/above/myutil/script};
> $ENV{PATH} =~ s{:?$}{:/path/above/myutil/script};

Actually, if you look at my posting, you see that PATH *is* set so that
it
points to the myutil directory. Strangely this is not sufficient to
make
FindBin the module in this particular case.

Extending PERL5LIB would indeed solve the problem, but it would not
explain why FindBin fails to do its job in my example.

Ronald