artist has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks
I am trying to store and retrieve the values via storage object. My main program use 3 packages:
1. Storage
2. Source
3. Destination

Now within Source.pm, I like to store some data using an object($storage) of Storage class and would like to access the same object in Destination.pm to retrive the value.

I have multiple instances of Source and Destination object and Storage class stores historical values as well which Destination objects uses any time. Where do I declare the $storage object? I try to use 'our' That doesn't seem to present $storage in the other packages.
I am trying to achieve something like

use Storage; our $storage = new Storage(); use Source; use Destination; my $input = 'source.txt'; while (<IN>) { $source = new Source($_); $destination = new Destination(); $source->storeData(); $destination->useData(); }
The storeData and useData should have access to the single $storage object all the time. I would rather not pass data around via objects. As multiple classes can access the same data using the storage object as desire.

Thanks,
Seeking art of storage
Artist

Replies are listed 'Best First'.
Re: Storage Object
by jkahn (Friar) on Sep 11, 2002 at 20:54 UTC
    You might consider one of the following:
    1. Put the Storage object in a package variable within the Source and Destination classes.

      (in Source and Destination):

      our $storage; # or: # our Storage $storage; # I think this works to type it

      and in your main code:

      use Storage; my $storage = new Storage(); use Source; use Destination; $Source::storage = $storage; $Destination::storage = $storage; my $input = 'source.txt'; while (<IN>) { $source = new Source($_); $destination = new Destination(); $source->storeData(); $destination->useData(); }
    2. Pass the Storage object into the constructor of each of the Source and Destination objects:

      in your Source and Destination classes, add code to handle setting a Storage object as a parameter to new().
      then in your main code:

      use Storage; my $storage = new Storage(); use Source; use Destination; my $input = 'source.txt'; while (<IN>) { $source = new Source($_, storage => $storage); $destination = new Destination(storage => $storage); $source->storeData(); $destination->useData(); }
    Personally, I prefer the latter solution, since that allows you in the future to have different Storage objects for different Source and Destination objects.
      Thank Jkahn
      use Storage; my $storage = new Storage(); use Source; use Destination; $Source::storage = $storage; $Destination::storage = $storage;
      Works for now. I don't know how elegant it is though. It's good for me since I just need a single storage object for the entire file rather than seperate objects. Let's say my storage object is pointing to a database.
      2nd solution is considerd good for different storage objects

      art of storage
      of
      an artist.

Re: Storage Object
by broquaint (Abbot) on Sep 11, 2002 at 21:25 UTC
    I try to use 'our' That doesn't seem to present $storage in the other packages
    The reason for this is that $storage is declared into the main:: package, so isn't a global. If you still want to use as is with your classes you could always access it by it's full path $main::storage, but that's not a great solution.

    I think your best options are either to pass it to the constructor each time or put it in the Source package as a package variable and have the Destination package access it from their e.g

    $Source::storage = Storage->new(); # in package Destination do_stuff_to($Source::storage);
    Another option would be to have Destination inherit from Source
    package Destination; @ISA = qw(Source); ... # main code $Source::storage = Storage->new(); # Destination can now use $storage, hurrah
    Er, nope, that's wrong. Why? Because child classes only inherit methods, and even then it's only through the method lookup done by objects.
    HTH

    _________
    broquaint