Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: [implementation specific to windows] writing a proper batch file for terminal start-up

by swl (Curate)
on Oct 16, 2019 at 03:45 UTC ( #11107530=note: print w/replies, xml ) Need Help??


in reply to [implementation specific to windows] writing a proper batch file for terminal start-up

Are you using the portable edition of Strawberry perl? If so then you might be able to simplify things by calling your code after running the portable shell (which looks like the original source for your script, BTW - https://github.com/StrawberryPerl/Perl-Dist-Strawberry/blob/master/share/portable/portableshell.bat). Doing that ensures that the Strawberry perl is the first on the path, as are its utilities, and so should avoid conflicts between perl versions.

WRT setting PERL5LIB etc to blank, this is probably to avoid conflicts between perl versions. If you know your PERL5LIB variable is always for this version of Strawberry perl then you could comment it out. But then when you upgrade or change your perl, you will hit new conflicts.

If you have your code set up as a standard directory structure, e.g. with the script in ./bin and any modules under ./lib, then you can use libraries like rlib to add the relative paths in your calling script. That avoids setting any environment variables, global or within a batch script.

e.g. for a file in /somepath/bin/script.pl:

use strict; use warnings; use rlib; # add /somepath/lib to @INC # do stuff

WRT a single common place for new modules, that way would lead to more conflicts between the differing perl versions, mostly for XS modules, so is best avoided.

  • Comment on Re: [implementation specific to windows] writing a proper batch file for terminal start-up
  • Download Code

Replies are listed 'Best First'.
Re^2: [implementation specific to windows] writing a proper batch file for terminal start-up
by Aldebaran (Chaplain) on Oct 17, 2019 at 16:33 UTC
    If you have your code set up as a standard directory structure, e.g. with the script in ./bin and any modules under ./lib, then you can use libraries like rlib to add the relative paths in your calling script.

    Ok, this is helpful and worth exploring. I must say, however, that I'm unable to follow what rlib is doing. Let's look at the source:

    package rlib; use strict; use vars qw($VERSION @ISA); use lib (); use File::Basename qw(dirname); use File::Spec; $VERSION = "0.02"; @ISA = qw(lib); sub _dirs { my($pkg,$file) = (caller(1))[0,1]; my @rel = @_ ? @_ : qw(../lib lib); my $dir; # if called from package main then assume we were called # by a script not a module if($pkg eq 'main') { require FindBin; # hide "used only once" warning $dir = ($FindBin::Bin,$FindBin::Bin)[0]; } else { require Cwd; $dir = Cwd::abs_path(dirname($file)); } # If we were called by a package then traverse upwards # to root of lib while($pkg =~ /::/g) { $dir = dirname($dir); } if($^O eq 'VMS') { require VMS::Filespec; @rel = map { VMS::Filespec::unixify($_) } @rel; } map { File::Spec->catdir($dir,$_) } @rel; } sub import { shift->SUPER::import( _dirs(@_) ); } sub unimport { shift->SUPER::unimport( _dirs(@_) ); } 1; __END__

    I fail to see how _dirs is populated. Can someone talk through how this works? In particular, I do not understand the syntax of the following lines:

    my($pkg,$file) = (caller(1))[0,1]; my @rel = @_ ? @_ : qw(../lib lib);

    and

    while($pkg =~ /::/g) {

    How might I test this? Thx for your comments,

      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

      I fail to see how _dirs is populated. Can someone talk through how this works? In particular, I do not understand the syntax of the following lines:

      :) Basic debugging checklist

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11107530]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2019-12-06 08:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (154 votes). Check out past polls.

    Notices?