LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-31-2010, 09:13 PM   #1
Feynman
Member
 
Registered: Aug 2010
Distribution: XUbuntu
Posts: 71

Rep: Reputation: 15
Unhappy How to make a hash of objects in perl


I am trying to make a hash of objects in perl (long story.) I have a package containing the main hash and all the subroutines necessary to work with it. My "new" subroutine used to add an object to the hash goes like this:

Code:
$tmpdir="/tmp";
$subdir="/AutoScript";
$subdir=$tmpdir.$subdir;

%jobs={};

sub new
{
    my $class = shift;
    my $name = shift;
    if(! -d $subdir){mkdir $subdir;}
    $jobs->{ $name } = {
        textfile => shift,
        iofile => '$subdir."/".$name',
        placeholders => {}
    };
    return $jobs->{ $name };
}
The main hash is called $jobs. I call this script from another file that uses the package with this subroutine.
The problem is that only $jobs->{ $name }->{textfile} is accessible to other subroutines. I have tried printing other objects in these subroutines and only the "textfile" object prints a line. The really strange part is that no matter what order I declare those objects, this is always the case!

I give up. PLEASE HELP! I am literally out of ideas. I have spent hours trying every syntax variation and test I can think of.

Last edited by Feynman; 08-31-2010 at 09:16 PM.
 
Old 08-31-2010, 09:27 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Looks like you want a hash of hashes,

(untested)

Code:
$tmpdir="/tmp";
$subdir="/AutoScript";
$subdir=$tmpdir.$subdir;

%jobs={};

sub new
{
    my $class = shift;
    my $name = shift;
    if(! -d $subdir){mkdir $subdir;}
    $jobs{ $name => {
        textfile => shift,
        iofile => '$subdir."/".$name',
        placeholders => {}
        }
    };
    return $jobs{ $name };
}

hth
 
Old 08-31-2010, 09:31 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Feynman View Post
I am trying to make a hash of objects in perl (long story.) I have a package containing the main hash and all the subroutines necessary to work with it. My "new" subroutine used to add an object to the hash goes like this:

Code:
$tmpdir="/tmp";
$subdir="/AutoScript";
$subdir=$tmpdir.$subdir;

%jobs={};

sub new
{
    my $class = shift;
    my $name = shift;
    if(! -d $subdir){mkdir $subdir;}
    $jobs->{ $name } = {
        textfile => shift,
        iofile => '$subdir."/".$name',
        placeholders => {}
    };
    return $jobs->{ $name };
}
The main hash is called $jobs. I call this script from another file that uses the package with this subroutine.
The problem is that only $jobs->{ $name }->{textfile} is accessible to other subroutines. I have tried printing other objects in these subroutines and only the "textfile" object prints a line. The really strange part is that no matter what order I declare those objects, this is always the case!

I give up. PLEASE HELP! I am literally out of ideas. I have spent hours trying every syntax variation and test I can think of.
  1. How do you call your 'new' ?
  2. Why do you need $class ?
  3. Do you have a package ?
  4. Do you need a package ?
  5. Do your subroutines need anything from global scope, i.e. global variables ?

Meanwhile have a look at the following:

Code:
sergei@amdam2:~/junk/hello_world_in_perl> cat -n sub.prl
     1  use strict;
     2  use warnings;
     3
     4  sub
     5    {
     6    my ($person) = @_;
     7
     8    warn "Hello, $person";
     9    };
sergei@amdam2:~/junk/hello_world_in_perl> cat -n main.pl
     1  #!/usr/bin/perl -w
     2
     3  use strict;
     4  use warnings;
     5
     6  (require './sub.prl')->('10speed705');
sergei@amdam2:~/junk/hello_world_in_perl> ./main.pl
Hello, 10speed705 at ./sub.prl line 8.
sergei@amdam2:~/junk/hello_world_in_perl>
 
Old 08-31-2010, 09:36 PM   #4
Feynman
Member
 
Registered: Aug 2010
Distribution: XUbuntu
Posts: 71

Original Poster
Rep: Reputation: 15
Come to think of it, it does look like a hash of hashes. That might suit my purposes as well. See, I started out with just objects, but that did not do everything I needed. So I tried to store the objects in a hash and ended up with this code.

As long as I can refer to these "objects" (or keys if I am using a hash) in other subroutines I should be fine.

Anyway, whatever the final product is called, this still is not working and I cannot for the life of me figure out why.
 
Old 08-31-2010, 09:39 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Feynman View Post
Come to think of it, it does look like a hash of hashes. That might suit my purposes as well. See, I started out with just objects, but that did not do everything I needed. So I tried to store the objects in a hash and ended up with this code.

As long as I can refer to these "objects" (or keys if I am using a hash) in other subroutines I should be fine.

Anyway, whatever the final product is called, this still is not working and I cannot for the life of me figure out why.
Better give us an example and describe it in plain English. I still do not understand what you mean by "object" here.
 
Old 08-31-2010, 09:46 PM   #6
Feynman
Member
 
Registered: Aug 2010
Distribution: XUbuntu
Posts: 71

Original Poster
Rep: Reputation: 15
Yes, I do have a package. I call it like this:
use inputgenerator;
inputgenerator->new("test","testtemplate");

or

use inputgenerator;
new inputgenerator("test","testtemplate");

Both fail equally well.

I am pretty sure this is not as simple as forgetting a package because this worked fine before I tried storing the objects in a hash. I just changed the "new" subroutine so it adds to a hash, and I changed how I refer to this new object (or maybe it should be called a hash in this case) in other subroutines to
($class, $name)=@_;
$jobs->{ $name }->{iofile};

I use the $class thing because that was what
http://www.tutorialspoint.com/perl/perl_oo_perl.htm showed me. I admit I do not fully understand the subtlies of the syntax, but it worked before. I would like to say I would learn all the nuts and bolts of what perl is doing, but
a) I have yet to find a description I can understand
b) Although interesting, this is probably more time than it is worth and for now I just would like to be able to understand enough to complete the tasks at hand.
 
Old 08-31-2010, 09:47 PM   #7
Feynman
Member
 
Registered: Aug 2010
Distribution: XUbuntu
Posts: 71

Original Poster
Rep: Reputation: 15
"object" is easy to describe. It's what these guys call an object:
http://www.tutorialspoint.com/perl/perl_oo_perl.htm
 
Old 08-31-2010, 09:53 PM   #8
Feynman
Member
 
Registered: Aug 2010
Distribution: XUbuntu
Posts: 71

Original Poster
Rep: Reputation: 15
If you do not think that
http://www.tutorialspoint.com/perl/perl_oo_perl.htm
is the right approach I would not be entirely surprised. Especially since it seems I should not need that "bless" method for this. I am sorry if I confused anyone by not explaining and fully understanding the meaning of "object" in this context. I am trying to find what works. This seemed like what would work, so I used it. I assumed this was a fairly common practice, so I went along using it.

Perhapse a hash of hashes is better. I just need to be able to refer to iofile, placeholders, and name from other subroutines in the same package. That is really it.

Many of my friends do a lot of java programming and they have been lecturing me on the wonders of classes and objects for years now. I thought about their description and figured this fit the description of what I wanted pretty well. Honestly, that is why I was using that approach.
 
Old 08-31-2010, 09:58 PM   #9
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Feynman View Post
"object" is easy to describe. It's what these guys call an object:
http://www.tutorialspoint.com/perl/perl_oo_perl.htm
Now that I know what you mean by object:

Quote:
Within Perl, an object is merely a reference to a data type that knows what class it belongs to. The object is stored as a reference in a scalar variable. Because a scalar only contains a reference to the object, the same scalar can hold different objects in different classes.
why do you need objects of such kinds ? Specifically, why do you need a class ?

Further on the way there is "Defining a Class" and later on "Creating and Using Objects" - is your code compliant with what the tutorial says ?
 
Old 08-31-2010, 10:00 PM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Feynman View Post
If you do not think that
http://www.tutorialspoint.com/perl/perl_oo_perl.htm
is the right approach I would not be entirely surprised.
...
I am asking again - why do you need classes and their instances ? And Java people are probably the worst advisers when it comes to Perl - the latter has quite a different spirit.
 
Old 08-31-2010, 10:04 PM   #11
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
As a simple helper, try adding 'use warnings;' to your module to show more info
 
Old 08-31-2010, 10:07 PM   #12
Feynman
Member
 
Registered: Aug 2010
Distribution: XUbuntu
Posts: 71

Original Poster
Rep: Reputation: 15
My code was compliment to what the tutorial says. It worked perfectly before I tried to store the objects in a hash. Now it does not. However, the tutorial never explained how to do such a thing, so there is not much I can go by.

I said earlier--I am not sure I need these "objects" after all. They were the only approach I thought suitable because of conversations with friends that do a lot of programming in java.

Now that I think about it, this is definatly a hash of hashes I am making.

Yes, I need a separate package. I am going to be making many files that use these subroutines and hashes so it is very very nice to have them all stored in one place.

No, no global variables at this point (that I know of). This package uses other packages, but I am not refering to variables from different packages in any package or file. At least not directly (I may make a subroutine that returns the value of some local variable).
 
Old 08-31-2010, 10:13 PM   #13
Feynman
Member
 
Registered: Aug 2010
Distribution: XUbuntu
Posts: 71

Original Poster
Rep: Reputation: 15
You ask why I need classes and objects--what I need is (forgive me I have never successfully articulated this in any compact manner)
A grouping of "things" (strings and hashes) that are referred to under a common name. For instance thisname(firststring) (or some other syntax with that conveys the same idea of something called "firststring" referred to by/through something else called "thisname") will give me a string.

Tell me if I need to describe my process in more detail. It is rather difficult to completely describe.
 
Old 08-31-2010, 10:15 PM   #14
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Feynman View Post
...
Yes, I need a separate package. I am going to be making many files that use these subroutines and hashes so it is very very nice to have them all stored in one place.
...
The statements above does not indicate you need a package, the "one place" can be just a file.
 
Old 08-31-2010, 10:17 PM   #15
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Feynman View Post
You ask why I need classes and objects--what I need is (forgive me I have never successfully articulated this in any compact manner)
A grouping of "things" (strings and hashes) that are referred to under a common name. For instance thisname(firststring) (or some other syntax with that conveys the same idea of something called "firststring" referred to by/through something else called "thisname") will give me a string.

Tell me if I need to describe my process in more detail. It is rather difficult to completely describe.
Yes, you need to describe in more detail, and "thisname(firststring)" relationship requires just one level hash:

Code:
my %some_hash =
  (
  name1 => $value1,
  name2 => $value2,
  ...
  nameN => $valueN
  );
 
  


Reply

Tags
class, hash, object, package, perl



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl hash of hashes props666999 Programming 2 09-07-2006 04:43 AM
Using hash value as key for other hash in Perl scuzzman Programming 6 02-14-2006 05:08 PM
Perl and Hash automatic PB0711 Programming 3 09-23-2005 02:14 AM
Generating hash in perl ? MikeFoo1 Programming 3 05-21-2005 06:03 PM
Hash-sort like in PERL in C++ nyk Programming 4 06-11-2004 08:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration