http://www.perlmonks.org?node_id=862800


in reply to Re^2: More Macro work...out to export a definition to next-outer level?
in thread More Macro work...out to export a definition to next-outer level?

You say that the above isn't satisfactory for 'singletons'.

Because you're using two screens of fragile and complicated code to replace two lines of code.

To invoke the code, I called a sub 'instance_vars', that was in a begin block.

No, it's not in a BEGIN block. The sub's definition is in a BEGIN block, but I already indicates how that's useless.

Aren't they global variables to each package that are initialized only once

No, they're initialised by the constructor, any number of times.

and then retain their value as global variables in each package (or accessible via package::var) from without the package?

That may be, but you access them via method calls.

I think you are showing me an alternate way of doing what I'm doing, but I don't think it's answering my question

I did. Like I said, you called it too late. You only call class_vars after you try to compile the code that uses them. All I did was made a change to call it earlier.

  • Comment on Re^3: More Macro work...out to export a definition to next-outer level?
  • Download Code

Replies are listed 'Best First'.
Re^4: More Macro work...out to export a definition to next-outer level?
by perl-diddler (Chaplain) on Oct 01, 2010 at 09:20 UTC
    I think there's a confusion in terms. I'm using class var to mean global to the whole class and each instance of the class. My other routine is an instance_var which declares variables for each instance. For my globals, I'm trying to use the package variables referenced by 'our', that are global to the package. AFAIK, they are globals that come into creation when the package does, and if there is code defining them in the package, with an initializer, it gets executed once when that package loads.

    There shouldn't be multiple initializations

    As far as how I access them: I call them through method calls, meaning I call them through a blessed pointer which I throw away with the shift; Then I store or/and return the global variable passed in from the 'foreach' in '$_' (e.g. one two three), namely the global package variables byte those names.

    Does this clear anything up?

      I think there's a confusion in terms. I'm using class var to mean global to the whole class and each instance of the class.

      The confusion is yours. You've been saying the above, but you haven't been doing the above. Like I said, your instance constructor populates the class attributes instead of the instance attributes.

      There shouldn't be multiple initializations

      Then they should be initialised outside of any function.

      Does this clear anything up?

      No. I understood fine all along.

      I think there's a confusion in terms. I'm using class var to mean global to the whole class and each instance of the class.

      The confusion is yours. You've been saying the above, but you haven't been doing the above. Like I said, your instance constructor populates the globals instead of the instance.

      If there's only ever one instance, what you have a singleton, and you've made a mess of things.
      If there can be multiple instances, your code is broken.

      There shouldn't be multiple initializations

      Then they should be initialised outside of any function.

      Does this clear anything up?

      No. I understood fine all along.

        I don't know if you are still seeing replies, since I don't know how to get notified of replies to my replies other than to go find each thread I've written in and look for them, BUT, I'll try to clarify again. In my setup, I'm doing this (not copied from code, but written for demonstration, so if everything doesn't run, that's why):
        package MyPackage; our $global=0; sub global { shift; $global=$_[0] if @_; $global; } sub instance { my $this=shift; if (@_) { $this->{instance}=$_[0]; ++$global; } $instance; } sub new { my $package=shift; bless {},$package; } } package main; my $package = new MyPackage(); #main *doesn't* know what vars are per-class or which are per-instance +. # that's an implementation detail the caller doesn't know about # so all calls are called through method calls. $package->instance(1.7); print "vars created = ",$package->global; ... # reset MyPackage counter to globally reset counter for all users # (not normal used; be available for better user control of counting.. +. $package->global(0); ...
        Is your complaint that I'm setting my global in a method call? That is intentional. In this situation, callers don't know the details of some variables -- whether they are per-class or per-instance. I am aware, that a caller could call an initializer function more than once. But in the use of the class, this would be an abnormal occurrence -- supported for development purposes, but not normally used.

        Later, if I thought it was necessary, I could change the global definition and make some 'global_readonly', that only provides a 'get' function if I thought it necessary, but for now, I'm providing both.

        I feel like we are playing 20 questions -- I keep trying to see if 'X' is what you are talking about. You keep saying 'wrong', 'its a mess'. But you don't clearly explain why its a mess. This isn't as helpful for me as your first explanation was on my prior question -- sorry.