I think that it is not a modules task to know on which machine it is run. There are Modules in the perl core which do different things depending on the operation system they are running on for the sake of portability, e.g. File::Spec. Instead in your case I'd place the burden on the shoulders of the module user, i.e. the code which uses the module. To set something in the module's namespace, write a import subroutine which does just that. You decide whether that variable is visible (and/or changeable) from the outside (our, or package global), or if it is a private variable to the module which can be set only once in the import routine.

package Foo; my $dir; # could be 'our' to be visible from outside # portablitity... far from complete if ($^O eq 'MSWin32') { $dir = 'C:\Users'; } else { $dir = '/home'; } sub import { my $package = shift; # just the package name if (@_) { if (@_ % 2) { die "odd number of parameters in ".__PACKAGE__"::import(), + aborted"; } my %args = @_; if ($args{dir}) { $dir = $args{dir}; } } } ... # more module code 1;

Code using this module would say use Foo dir => '/tmp_mnt/home' if it was running, say, on SunOS-4.1.3 and the home directories were mounted via NIS, NFS and automounter. I digress... but you get the picture ;-)

Since use statements lead to compiling and executing the used module which the current script is parsed, any intialization of arguments to e.g. use Foo dir => $somedir must happen before use Foo is called, which means that this code must be IMMEDIATE (in forth terms). This is done wrapping the initializing code into a BEGIN block prior to use Foo. The contents of this block will be parsed and executed immediately.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

In reply to Re: Best practice for setting module "environment" variables by shmem
in thread Best practice for setting module "environment" variables by nysus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.