Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Where to keep images so a module can find them

by sweetser (Acolyte)
on Sep 04, 2006 at 15:54 UTC ( #571112=perlquestion: print w/replies, xml ) Need Help??

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


I am unclear about where I should put images so a module can find them. I would like to have a directory, call it Images, right where the module lives, then have files like Images/foo.png. When I run the perl program that uses the modules, will perl be able to open "Images/foo.png", or do I first need to find out where the module is, then read "$module_dir/Images/foo.png"?

An added question: if I use Build.PL, do I have to tell it to include the images somehow?


  • Comment on Where to keep images so a module can find them

Replies are listed 'Best First'.
Re: Where to keep images so a module can find them
by Tanktalus (Canon) on Sep 04, 2006 at 16:51 UTC

    Technically, when you open a file called "Images/foo.png", perl will use the current process' current working directory (which defaults to the same as the parent process was using when your perl app was started). So, unless you happen to already be in the lib directory when you started (or manually changed since then), no, that won't work.

    What you can do, potentially, is to look for yourself in %INC and figure out everything relative to that. e.g.:

    use File::Basename; (my $pm_name = __PACKAGE__ . '.pm') =~ s[::][/]g; my $images_dir = File::Spec->catdir(dirname($INC{$pm_name}), 'Images') +;
    This is depending on the concept that you follow all normal perl conventions (one package per pm file, the pm file is in a path where ::'s become /'s (or \ on windows), and a .pm is added to the end). If you don't, well, you'll have to look at %INC yourself to figure out what to do to figure out where you are.

      I decided to use this approach since it appears darn simple. Thanks,
Re: Where to keep images so a module can find them
by ozone (Friar) on Sep 04, 2006 at 16:11 UTC
    Any relative path, such as "Images/foo.png", will use Perl's current working directory, and if the module happens to exist in the same directory, then it will find the file.

    My point is that it's probably more useful (and better practice) to put images into the user's home dir, or a system dir ('/usr/local/xyz'). Then your module can run through a list of default dirs (~/.xyx/Images, /usr/local/xyz/Images etc) until it finds the file it's looking for.

Re: Where to keep images so a module can find them
by vkon (Curate) on Sep 04, 2006 at 19:44 UTC uses "findINC" subroutine which helps with finding an image near module installation, look into it to see the logic.

    Other approach (used in many Tk modules) is to base64-encoding image data into PM file itself.

    But in general, this is indeed a bit of a problem, without sole solution, and each module uses its own way.

Re: Where to keep images so a module can find them
by ysth (Canon) on Sep 04, 2006 at 20:46 UTC
    If these are part of your module distribution, and installed when the module is installed, consider putting them in an auto subdirectory. So, if your module is named Foo::Bar and installed somewhere like:
    put your Images in
    As far as I know, there's no abstraction to make this easy, either in the install script or at runtime, except for particular cases such as XS module dynamic libraries.

    But you can take a peek through the EU::MM/M::I/M::B::Cookbook docs and see if there's anything about the former. And the code for the latter can be stolen from

    Update: I was asked "why auto" in particular, rather than somewhere else, perhaps just alongside the module in the module tree. You have to put the files somewhere, and in the module tree, at a known location relative to the .pm, in a directory already treated as special by things looking at the module directories sounds pretty good.

      As far as I know, there's no abstraction to make this easy

      File::ShareDir and Module::Install


      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

        File::ShareDir fits the bill perfectly. I did poke about a bit in CPAN but didn't find it. (Update: I wonder how well it works with only'd modules.)

        What part of Module::Install should I be looking at?

Re: Where to keep images so a module can find them
by zentara (Archbishop) on Sep 04, 2006 at 19:03 UTC
    On linux anyways, most people will create an hidden "resource directory" for your program, where you can stick your configuration files and images, etc. Like ./myscriptdir.

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://571112]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (1)
As of 2022-07-03 02:28 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (103 votes). Check out past polls.