Dear Perlmonks :), I have some packages in /home/avarus/perl/myPackages/*.pm and what I want to do is to use that dir as a lib directory which is generally not a problem, but the first problem exists when I do the following:
my $libDir = "/home/avarus/perl/myPackages/"; use lib "$libDir";
This code does not really sets the lib path as expected but if I hard code this path instead of using this $libDir variable it simply works. So I decided to use a hardcoded lib path for testing purposes which exposed the next problem. In the directory "/home/avarus/perl/myPackages/" there is a package called "test.pm" which defines package "Test".
use lib "/home/avarus/perl/myPackages/"; use test; # works
this example works, but...
use lib "/home/avarus/perl/myPackages/"; my $packageName = "test"; use $packageName; # does not work, segfaults
At the moment I am working around this problem with "require" and bla::blub constructs but then I don't have the functionality I want to make use of as described in perldoc perlobj. When I solved all those problems the next one is how I can create a new instance like this:
my $objName = "foo"; my $testObj = new $objName();
this of course does not work, but that's like I wanna do that :). It would be great if you can give me some hints. Thanks in advance, Andreas Schipplock.

In reply to Loading and using packages dynamically by avarus.com.ar

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.