Suppose I have a really cool module buried deep in the module tree. Constructing really cool objects is no fun because all the typing makes me miss Java:

use Local::Some::Real::Cool::Module; $this = Local::Some::Real::Cool::Module->new(); $that = Local::Some::Real::Cool::Module->new(); $more = Local::Some::Real::Cool::Module->new();

Sometime ago I learned I could set a string to the module name and use it instead:

use Local::Some::Real::Cool::Module; $gimme = 'Local::Some::Real::Cool::Module'; $this = $gimme->new(); $that = $gimme->new(); $more = $gimme->new();

That is better, but still kinda sucks. So I try to have the module return its own name and get it that way, but of course the return of use can't be used:

# Nope $gimme = use Local::Some::Real::Cool::Module;

Maybe the return of require can be used to grab the module name? Yes it can. But will I be sorry if I do? Any other suggestions?

#!/usr/bin/perl my $gimme = require Local::Some::Real::Cool::Module; import $gimme qw(some import stuff here); my $this = $gimme->new(); my $that = $gimme->new(); my $more = $gimme->new(); #----------------------------------------------------------- # Local/Some/Real/Cool/Module.pm #----------------------------------------------------------- package Local::Some::Real::Cool::Module; sub import { # . . . } sub new { return bless {}, shift; } 'Local::Some::Real::Cool::Module';

YuckUse


In reply to use, require, and constructors by YuckFoo

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.