CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Accessing an object's hash

 



Juza
New User

Oct 1, 2008, 4:05 AM

Post #1 of 7 (1018 views)
Accessing an object's hash Can't Post

Sorry for my stupid question (I come from Java and I'm quite new to Perl), but I have some problems accessing an hash.
This is my code sample:

package UserData;

use strict;
use Readonly;


my $session;
my %user_data;

sub new
{
my ($class, $session_param) = @_;

my $new_object = {};
bless $new_object;

$new_object->{session} = $session_param;

return $new_object;
}

sub setup_userdata
{
my $this = shift;

$this->{user_data} = (
"company" => $this->{session}->{SESSION}{COMPANY},
"salutation" => $this->{session}->{SESSION}{TITLE},
"lastname" => $this->{session}->{SESSION}{FAMILYNAME},
"firstname" => $this->{session}->{SESSION}{FIRSTNAME},
"nethzname" => $this->{session}->{SESSION}{USERNAME},
"telprivate" => $this->get_tel_private(),
"teloffice" => $this->get_tel_office(),
"mobileprivate" => $this->{session}->{SESSION}{MOBILE_CERTIFIED},
"email" => $this->{session}->{SESSION}{EMAIL});
}

sub show_page
{
....
<tr>
<td>
Firma:
</td>
<td>
&nbsp;
</td>
<td colspan=\"3\">
".${user_data}{company}."
</td>
</tr>
....
}

How do I access the elements of my user_data hash from inside this module?

I tried with:
- $user_data{company}
- ${user_data}{company}
- $this->{user_data}{company}

Please, can you help me?

Thanx
Juza


shawnhcorey
Enthusiast


Oct 1, 2008, 4:27 AM

Post #2 of 7 (1016 views)
Re: [Juza] Accessing an object's hash [In reply to] Can't Post


In Reply To
How do I access the elements of my user_data hash from inside this module?

I tried with:
- $user_data{company}
- ${user_data}{company}
- $this->{user_data}{company}



Code
sub show_page { 
my $this = shift @_;

print "$this->{user_data}{company}\n";
}


Perl does not automatically give names to objects. You have to do this yourself. The first argument of a method is always a reference to the object.

Also, helper subs, that is, subs that do not expect to have an object ref as their first argument, start with an underscore. This is by convention only, nothing in Perl enforces this.

Code
sub _helper { 
...
}


Finally, you can see what's inside any variable with Data::Dumper.

Code
use Data::Dumper; 
print Dumper $this;


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


Juza
New User

Oct 1, 2008, 4:40 AM

Post #3 of 7 (1014 views)
Re: [shawnhcorey] Accessing an object's hash [In reply to] Can't Post

Thanx for the quick answer, but now I get this error msg:
Can't use string ("xxxxxxxx") as a HASH ref while "strict refs" in use at ...

Juza


shawnhcorey
Enthusiast


Oct 1, 2008, 5:21 AM

Post #4 of 7 (1009 views)
Re: [Juza] Accessing an object's hash [In reply to] Can't Post


In Reply To
Thanx for the quick answer, but now I get this error msg:
Can't use string ("xxxxxxxx") as a HASH ref while "strict refs" in use at ...

Juza


The structure of your object is not what you expect it to be. Use Data::Dumper to look at it.

You may want to add these statements when working with Data::Dumper.

# These two make it easier to read:
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent = 1;

# This one limits the depth of the printout.
# Zero means unlimited depth.
$Data::Dumper::Maxdepth = 0;

See `perldoc Data::Dumper` for details.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


Juza
New User

Oct 1, 2008, 5:29 AM

Post #5 of 7 (1008 views)
Re: [shawnhcorey] Accessing an object's hash [In reply to] Can't Post

Mmm, it seems so; but why?

I declare the variable my %user_data as a global var.
Then in setup_userdata I initialize it with
$this->{user_data} = (....)
And then I try to get the data stored in it ...

I expect user_data to be a hash...


shawnhcorey
Enthusiast


Oct 1, 2008, 5:47 AM

Post #6 of 7 (1006 views)
Re: [Juza] Accessing an object's hash [In reply to] Can't Post


In Reply To
Mmm, it seems so; but why?

I declare the variable my %user_data as a global var.
Then in setup_userdata I initialize it with
$this->{user_data} = (....)
And then I try to get the data stored in it ...

I expect user_data to be a hash...


%user_data is a lexical-scoped file variable. It is only accessible within the file. It is not the object. It is a class variable. Any object can access it.

Please dump the object just before the error occurs. It will show you what's inside it.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


Juza
New User

Oct 1, 2008, 5:58 AM

Post #7 of 7 (1005 views)
Re: [shawnhcorey] Accessing an object's hash [In reply to] Can't Post

Mmm, in the meantime I found the error and as usual in such cases is a veeeeery stupid one:

in setup_userdata I used ( instead {

$this->{user_data} = ("
instead of
$this->{user_data} = {"

Unsure

Thanx for the help anyway Wink

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives