Gavin Ford wrote: > As the list is up and going, I thought I'd ask a Perl question. :) > > I'm wondering if I'm making things over complicated for myself. > > I have a few subroutines I reuse in a few scripts, so I moved them to another > file in the same directory and link them into each script with something like: > > use FindBin '$Bin'; > require "$Bin/lib-rf-iplayer.pl"; > > I had been entering the full path on the require line, but this was a bit > rubbish as it needed to be tweaked for different machines or users. > > But this still feels like I'm going a step too far. > > Is there a simpler way to require a file relative to the script instead of > relative to the working directory? >
I use FindBin in every cgi script I make. Windows tends to execute cgi's with a different working directory to where they actually are. This can confuse people when reading and writing to files, it also doesn't tend to have the cgi scripts directory in @INC. So if you simply use modulename; you'll get and can't find error. This small snippet of code near the top of each cgi script goes a long way towards making your script more portatble across different OS's. use FindBin qw ( $RealBin ); use lib $FindBin::RealBin; ## I've found $RealBin works in more situations than $Bin. Now it'll pickup your requires and uses properly. chdir $RealBin; ## Change to the scripts directory. Now it'll pick your file read and writes properly. I'd also go with Aaron's suggestion of making it a proper perl module. Lyle _______________________________________________ BristolBathPM mailing list [email protected] http://mailman.bristolbath.org/mailman/listinfo/bristolbathpm
