Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: The costs of packages

by Abigail-II (Bishop)
on Sep 16, 2003 at 08:45 UTC ( #291763=note: print w/replies, xml ) Need Help??


in reply to The costs of packages

Why have minimal packages at all? Would something like this work for you?
# # This is the file My/Types.pm # package UNIVERSAL; use strict; use warnings; sub create { my $class = $_ [0]; print "Creating class $class\n"; eval <<"--"; package $class; sub create { my \$class = shift; print "Now inside \${class}::create\n"; bless [] => \$class; } -- no strict 'refs'; my $sub = "${class}::create"; goto &$sub; } 1; __END__ #!/usr/bin/perl use strict; use warnings; use My::Types; my $typeA1 = create TypeA; my $typeA2 = create TypeA; my $typeB1 = create TypeB; __END__ Creating class TypeA Now inside TypeA::create Now inside TypeA::create Creating class TypeB Now inside TypeB::create

Abigail

Replies are listed 'Best First'.
Re2: The costs of packages
by dragonchild (Archbishop) on Sep 16, 2003 at 16:24 UTC
    That's an extremely cool approach! It's actually quite obvious, once it's demonstrated. (As all neat and simple things usually are ...)

    Now, are there maintainability issues here? Why would you put create() in UNIVERSAL instead of exporting it, ala use My::Types qw(create);?

    Also, I would assume that, since you're eval'ing the package, you could have it do inheritance and the like, right? It would be interesting to see a non-trivial example of this in action ...

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      Now, are there maintainability issues here? Why would you put create() in UNIVERSAL instead of exporting it, ala use My::Types qw(create);?

      If you export the create sub, it would mean that each time you call create, the create function in My/Types.pm is called, because it's the create function in the current package. With the UNIVERSAL trick, the create function in My/Types.pm is only called once for each class - a second call to create with the same package name as argument is handled by the create package directly. The example program I gave shows this. This is because UNIVERSAL is searched *last*, while the current package is searched *first*, and that's the crucial difference between exporting and using UNIVERSAL.

      Also, I would assume that, since you're eval'ing the package, you could have it do inheritance and the like, right?

      Uhm, no. As I understood, the problem BrowserUK was having is that his program typically would use a few classes, but those classes would be picked from potentially hundreds. And the memory of those hundreds "skeletons" was a concern. The "eval" trick (which might as well have been a 'require') makes that only packages that are used consume memory. I didn't get the impressions BrowserUKs original approach considered inherited constructors, and I certainly didn't considered it either.

      Abigail

        Inheritance is not a requirement nor even desirable for my application. The types I am talking about are not really classes per se, just perl convenient wrappers around C-structures that will be passed to OS calls.

        On a slightly related question while your around. Is there any reason beyond your personal preference for using create rather than new?

        I've tried it both ways and it does exactly what I was achieving with my scheme, but without the overhead, which is great--thanks again--but I'm wondering if you know of some trap waiting for me if I use "new" as the constructor name?


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
        If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: Re: The costs of packages
by BrowserUk (Pope) on Sep 16, 2003 at 09:31 UTC

    Thanks Abigail. That works for me.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://291763]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2019-02-19 17:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I use postfix dereferencing ...









    Results (105 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!