Main Site Contents Up Previous Next

Including Resources


Resource Directory

Cava Packager provides a simple method of accessing files and resources from your scripts in the perl environment that will work unchanged in your packaged executables.

You can achieve this by using a single resource directory in your development environment for all your resource files. For example, you could store all of the images that a script requires in a single directory.

You would then use the Cava::Packager module at the start of your script to set the location of this resource directory:

use Cava::Packager;
Cava::Packager::SetResourcePath('c:/myscripts/resourcedir');

Later in your script you could then access a file within the resource path using the Cava::Packager::GetResource function:

my $filepath = Cava::Packager::GetResource('helpfiles/myapp.chm');

Cava Packager also provides an importable short alias for this function.

use Cava::Packager qw( CRF );
my $filepath = CRF('helpfiles/myapp.chm');

Note that subdirectories of the resource directory are allowed: so in the above example, the absolute path of the file we have loaded would be C:\myscripts\resourcedir\helpfiles\myapp.chm.

When you package the script using Cava Packager, all that you need to do is select the 'Resources' node in the project tree and include your resource directory in the project. When the package is 'built' all the files in the resource directory will be included with your package. The resource directory is included recursively so subdirectories will work as expected.


User Folder

Cava Packager provides a second method of accessing files and resources from your scripts in the perl environment that will work unchanged in your packaged executables.

A folder named 'user' is created alongside your executable directory. The content for this is taken from the folder defined in the 'User Folder' project tree node. Commonly, this folder is used to store configuration files or other content that should be available to the user. Unlike resource files, this folder is never compressed or virtualized when packaged.

So that your script may run unchanged in both packaged and non packaged environments, a function is provided in the Cava::Packager utilities that allows you to set a 'user folder' for use by your script when not packaged. This function is ignored when packaged.

use Cava::Packager;
Cava::Packager::SetUserPath('c:/myscripts/userdir');

Later in your script you could then access a file within the user folder using the Cava::Packager::GetUserFile function:

my $filepath = Cava::Packager::GetUserFile('config/myapp.xml');

You can also access the directory itself with:

my $userpath = Cava::Packager::GetUserPath();

Note that subdirectories of the user folder are allowed. The user folder is included recursively so subdirectories will work as expected.

Cava Packager will attempt to preserve the permissions of files in your user folder when packaged.

Mapped Files

As an alternative or addition, Cava Packager provides the function Cava::Packager::MapFile. This allows you to record the location of files as your development code runs and then have those files packaged and accessible using the same virtual filepath in your distributed application.

use Cava::Packager;
Cava::Packager::SetProjectPath('/user/myname/pathto/cavaproject');
my $filepath = Cava::Packager::MapFile($mylocalpathtofile);

Cava Packager also provides an importable short alias for this function.

use Cava::Packager qw( CMF );
my $filepath = CMF($mylocalpathtofile);

It is necessary to call Cava::Packager::SetProjectPath once in your code so that the Cava::Packager module can save gathered information to the correct place.

At packaging time Mapped Files are packaged together with your other resources.

Compressed Resources

You can choose to compress your resources if you wish so that they are extracted at run time using the appropriate project build options.





Contents Up Previous Next


Cava Packager Copyright © 2006-2012 Mark Dootson