http://www.perlmonks.org?node_id=11107626


in reply to Re^2: [implementation specific to windows] writing a proper batch file for terminal start-up
in thread [implementation specific to windows] writing a proper batch file for terminal start-up

The caller line gets the name of the package and file that called the use rlib. If you use caller with no arguments then it tells you about the sub it is called from, but in this case the argument tells it to go one higher in the call stack. See the docs for caller for more details.

The second line is simply setting default arguments using the ternary operator. If the arguments array @_ is empty then it will add ../lib and lib to @INC, otherwise it will work with the passed arguments.

The while loop is for cases where use rlib is called from a package like Some::Package::PP. In that case it will try to find the ancestral lib dir. It assumes caller(1) will return a file name something like /somepath/lib/Some/Package/PP.pm. The =~ /::/g will return an array of how many times the package name includes ::, and will go up that many directories to find /somepath/lib/Some. This means it will eventually add /somepath/lib/Some/../lib and /somepath/lib/Some/lib to @INC (I might be off by one on the dirs, as I have not tested, but the principle is the same).

To test the while line:

# on windows - update quotes as needed perl -E"my $x = q|Some::Package::PP|; say 1 while $x =~ /::/g" #produces 1 1