For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > January 2006 > Embedding private .pm files into a script









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Embedding private .pm files into a script
Haakon Riiser

2006-01-29, 6:58 pm

I keep my own little library of Perl routines in a directory
separate from the stuff that came with Perl itself and CPAN,
and I've put this directory in PERL5LIB. This is sufficient for
programs that only I use, but when friends and co-workers ask
for a copy of one of my scripts, it would be nice if I could just
give them a single file that contains everything they need.

I have looked at PAR, but what it does is overkill. I don't need
it to bundle every single dependancy to create a huge stand-alone
executable. Only the modules from my personal library needs to
be embedded into the resulting script. I see that pp has the -X
option to exclude a named module, but what I want is to exclude
everything except the stuff stored in a certain directory tree.
As far as I can tell, PAR can't do this.

Is there an easy way to accomplish what I want? I could embed
the required modules by hand, but that's too much work. :-)

--
Haakon
A. Sinan Unur

2006-01-29, 6:58 pm

Haakon Riiser <hakonrk@fys.uio.no> wrote in
news:slrndtq6tv.ro5.hakonrk@fox.venod.com:

> I keep my own little library of Perl routines in a directory
> separate from the stuff that came with Perl itself and CPAN,
> and I've put this directory in PERL5LIB. This is sufficient for
> programs that only I use, but when friends and co-workers ask
> for a copy of one of my scripts, it would be nice if I could just
> give them a single file that contains everything they need.


See

perldoc -q lib
perldoc lib
perldoc FindBin

NAME
FindBin - Locate directory of original perl script

SYNOPSIS
use FindBin;
use lib "$FindBin::Bin/../lib";

or

use FindBin qw($Bin);
use lib "$Bin/../lib";

So, put your scripts in a directory, and put your packages in a
subdirectory of that. Then zip everything up. Give them the zip file.

Sinan

--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html

Haakon Riiser

2006-01-29, 6:59 pm

[A. Sinan Unur]

> See
>
> perldoc -q lib
> perldoc lib
> perldoc FindBin
>
> NAME
> FindBin - Locate directory of original perl script
>
> SYNOPSIS
> use FindBin;
> use lib "$FindBin::Bin/../lib";
>
> or
>
> use FindBin qw($Bin);
> use lib "$Bin/../lib";
>
> So, put your scripts in a directory, and put your packages in a
> subdirectory of that. Then zip everything up. Give them the zip file.


Thanks for the tip, but I'd prefer not having to reorganize my
directories.

I think I'll just write a simple script to convert all 'use'
declarations that refer to my private modules by a simple

package Private::Foo;
<contents of Private/Foo.pm>
package main;

This works as long as I don't include my modules in a fancy way
(such as loading them on demand, etc.)

--
Haakon
Ala Qumsieh

2006-01-30, 3:56 am

Haakon Riiser wrote:
> [A. Sinan Unur]
>
>
>
>
> Thanks for the tip, but I'd prefer not having to reorganize my
> directories.


What's wrong with a simple:

use lib '/path/to/your/module/dir';

?

--Ala

Haakon Riiser

2006-01-30, 7:59 am

[Ala Qumsieh]

> What's wrong with a simple:
>
> use lib '/path/to/your/module/dir';
>
> ?


I can think of at least three things:

(1) I don't want the script to search in a specific location.
I could use relative paths, but that would still be a problem if
someone decides to move the executable to another bin directory.

(2) If someone asks for a script 'foo', I want to be able to give
that to him without having to manually determine the module
dependencies and then copying all of the required files.

(3) I don't want other people to have to deal with the clutter
of separate modules. A single script should be enough for these
trivial things I'm talking about here.

But nevermind -- simply substituting the 'use' statement with the
contents of the respective module is good enough in this case, and
automating that task is simple.

--
Haakon
Anno Siegel

2006-01-30, 6:59 pm

Haakon Riiser <hakonrk@fys.uio.no> wrote in comp.lang.perl.misc:
> [A. Sinan Unur]
>
>
> Thanks for the tip, but I'd prefer not having to reorganize my
> directories.
>
> I think I'll just write a simple script to convert all 'use'
> declarations that refer to my private modules by a simple
>
> package Private::Foo;
> <contents of Private/Foo.pm>
> package main;
>
> This works as long as I don't include my modules in a fancy way
> (such as loading them on demand, etc.)


The "package"-statements are presumably already part of Private::Foo.
However, the contents should go into a block of their own so that
file-scoped lexicals in Private/Foo.pm don't leak out into your
lexical space. Also, if you want to put the module after code that
uses it, that block must be a BEGIN block. Thirdly, in case the module
has an ->import method, it should be called with the parameters that
would otherwise go in the "use" statement.

So, to replace the statement

use Private::Foo qw( fie foe fum);

one should use two BEGIN blocks:

BEGIN {
<contents of Private/Foo.pm>
}
BEGIN {
Private::Foo->import( qw( fie foe fum);
}

That should cover most cases.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com