igoryonya has asked for the wisdom of the Perl Monks concerning the following question:

I tried looking on both: Linux and Windows in %ENV variable. It doesn't have any common variable names, except for PATH, that's not very useful.
Also, I've looked at results on both systems from the File::BaseDir module. It shows good results linux, but empty results on windows.
Is there some other preinstalled module, or some command, that I can use to return the standard paths, such as:
libdir
progdir
confdir
homedir
userdir
Are there some variables or standard modules, that can determain standard system dirs, depending on which os the perl program is being launched?

Update:

Ok, I've figured out what to do:
I will create a file, called config.pl, for example, with code:
#Determine 1 path, except for cwd (current working directory), #Where perl looks for libraries: my $libPath = ''; for(@INC){ unless($_ eq '.'){ $libPath = $_; last; } } #Then, install the library, that will determain all the OS specific pa +ths, #that can be used for further execution and installation of perl progr +am.
When this library will be called, perl program doesn't need to know it's, since it will be already located in one of the paths, that perl looks in.

Running this code on 2 OSes, gave me the following results, so far:

Lin: /etc/perl Win: C:/Perl/site/lib

Replies are listed 'Best First'.
Re: standard library to return system paths
by Don Coyote (Hermit) on Jun 07, 2016 at 06:12 UTC

    Hello igoryonya

    Can you clarify which paths you intend to Find? Standard paths will vary on an application by application basis.

    Standard paths for a nix could be found by readdir('/'), which is what I understand by your question. Or grep a self-created list against File::Find

    You may also need to ensure you have permission to read the directories on either system, Errors can be generated if you do not have said permissions.

    Not knowing the paths you are looking for, you could use Find::lib module, that iirc will return the absolute filepath for the currently running program, from which you can use relative links within your program rather than trying to discern and hard-code standard paths for many different machines.


    less roam; more sell.
      Like I said:

      libdir
      progdir
      confdir
      homedir
      userdir

      It varies not by application, but by OS. For example:

      libdir: win(c:\program files\common files); lin(/lib)
      progdir: win(c:\program files); lin(/bin)
      confdir: win(c:\programdata); lin(/etc)
      homedir: win(c:\users); lin(/home)
      userdir: win(c:\users\user); lin(/home/user)
      etc..., etc..., etc...

        Maybe see File::HomeDir.

        Note that on Windows, C:\Program Files is not universal - it might localized to the language that Windows was installed to.

        There is no universal idea of where libraries get installed to, even on Linuxish OSes, you might have /lib, /usr/lib and /lib64.

        Maybe you will get better answers if you can tell us what exact problem you are trying to solve.

        There may be some useful modules with your purpose in mind, here:

        Cpan search: Find::lib

        There is likely to be something there which will be able to help you, or can be modified to spec. hth

      the end
Re: standard library to return system paths
by BrowserUk (Patriarch) on Jun 07, 2016 at 06:57 UTC

    On windows, these environment variables identify most of the information you're looking for:

    ALLUSERSPROFILE=C:\ProgramData APPDATA=C:\Users\someuser\AppData\Roaming CommonProgramFiles=C:\Program Files\Common Files CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files COMPUTERNAME=MACHINENAME HOMEDRIVE=C: HOMEPATH=\Users\someuser LOCALAPPDATA=C:\Users\someuser\AppData\Local LOGONSERVER=\\MACHINENAME ProgramData=C:\ProgramData ProgramFiles=C:\Program Files ProgramFiles(x86)=C:\Program Files (x86) USERDOMAIN=MACHINENAME USERNAME=someuser USERPROFILE=C:\Users\someuser

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice. Not understood.
      Yes, but like I said, there is no common variables in both OSes. None of those variables exist in Linux.
      This is the reason, I've asked the question.
      Are there some variables or standard modules, that can determain standard system dirs, depending on which os the perl program is being launched.
Re: standard library to return system paths
by syphilis (Archbishop) on Jun 08, 2016 at 13:47 UTC
    Running this code on 2 OSes, gave me the following results, so far: Lin: /etc/perl Win: C:/Perl/site/lib


    This is really puzzling.
    Are you claiming that you've located a Windows perl whose @INC consists only of C:/Perl/site/lib (and perhaps also .) ?

    I've not yet encountered a Windows perl whose @INC included C:/Perl/site/lib but did not also include C:/Perl/lib

    I've read every post in this thread and I still struggle to understand what you are seeking.

    UPDATE: Hang on ... the code you claimed to have run is:
    for(@INC){ unless($_ eq '.'){ $libPath = $_; last; } }
    but I think you probably wanted:
    for(@INC){ unless($_ eq '.'){ $libPath .= "$_ "; } }
    Does that alteration lead to the (expected) inclusion of C:/Perl/lib on the Windows system ?

    I'm assuming that you really want the entire @INC, ignoring the "." ... in which case you might just as well
    print "@INC\n";
    and mentally ignore the dot.

    Cheers,
    Rob
      Are you claiming that you've located a Windows perl whose @INC consists only of C:/Perl/site/lib (and perhaps also .) ?
      No, I am claming, that on windows, I find such path. I've never said, that that's the only path, I find.
      I've read every post in this thread and I still struggle to understand what you are seeking.
      I don't know, how much more clearly to explain, which I did several times and in different formulations.
      How much more clearly I need to describe, that I need my programs to know system (independently of OS) paths with the least amount of code added to every program.
      They need to know system common library paths, systemwide config paths, program/bin, etc., so my code was correct.
      How do you think I would use the whole string of paths to get the final file?
      From your code, I'd get:
      "/etc/perl /usr/local/lib/perl/5.18.2 /usr/local/share/perl/5.18.2 /us +r/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.18 /usr/share/perl/5.18 +/usr/local/lib/site_perl "
      How do you think, it would be useful? How do you think, I would locate a final file with such a path?
      If I would really need such a result, there is a better and shorter way to do it:
      $libPath = join(" ", grep { $_ ne "." } @INC);
      How do you think, I would put my libraries, finding system paths in this, "found" path? How would I do, even a simple thing, such as (with the resulted path from your suggested code)?:
      &cp($file2Copy, "$libPath/$file2Copy");
      No, I only need one (possibly, first found) path to place the common file to, and nothing is expected, that's why I need a way to determine the path. If I would "expect" some path, as you say, I would just use it, since, it would be on all systems, as "expected".
      print "@INC\n";
      And how do you think, this would be useful?
Re: standard library to return system paths
by stevieb (Canon) on Jun 07, 2016 at 12:33 UTC

    Is all of this simply to know where to install your own files to on first run, and so they are found later automatically? You aren't clear on your overall goal.

      Yes, regardless of the user and an OS. The ultimate goal, so that the minimum code (one line) will be enough to insert to any program, I write on any system.