Hash dereferencing and object-oriented Perl

S

Stefan

Hi...

For I while now I have been coding OO Perl using the $self->{member}
syntax to grab elements out of my blessed hash reference. This works
just fine, however it recently occurred to me that since $self is always
a simple scalar variable (containing a reference), I could just use the
$$self{member} syntax instead. Personally, I find this easier to deal
with - it's one character less to type and occasionally I find it useful
to use @$self{member1, member2}, something which I still haven't figured
out how to do with ->.

So my question... is there anything wrong with using this syntax? The
only reason I ask is that I have never seen an OO Perl module which uses
it... everyone seems to use the $self->{member} version. Sorry if I'm
being and idiot and missing something blatantly obvious!

Thanks


Stefan
 
U

Uri Guttman

S> Hi... For I while now I have been coding OO Perl using the
S> $self->{member} syntax to grab elements out of my blessed hash
S> reference. This works just fine, however it recently occurred to me
S> that since $self is always a simple scalar variable (containing a
S> reference), I could just use the $$self{member} syntax
S> instead. Personally, I find this easier to deal with - it's one
S> character less to type and occasionally I find it useful to use
S> @$self{member1, member2}, something which I still haven't figured
S> out how to do with ->.

S> So my question... is there anything wrong with using this syntax?
S> The only reason I ask is that I have never seen an OO Perl module
S> which uses it... everyone seems to use the $self->{member}
S> version. Sorry if I'm being and idiot and missing something
S> blatantly obvious!

first off, the way you dereference a hash ref (or any ref) has nothing
to do with OO perl. OO perl uses refs but you can use refs anywhere you
want without any OO code at all.

the main way to dereference is to put the proper sigil in front of the
ref value like %{$href} or ${$href}{$key}. but most (not you it seems)
find the latter hard to visually parse and larry gave us the syntactic
sugar of -> thus allowing $href->{$key}. it is your choice as to which
deref style to use but the vast majority use -> when accessing single
elements from array or hash refs.

uri
 
S

Stefan

Thanks for your response. I too find ${$hashref}{$key} hard to look at,
harder than $hashref->{$key} in fact. But $$hashref{$key} to me looks
nicer than either. I guess it's just personal preference.

The only reason I mentioned OO Perl is that the hash reference you're
dealing with in a method is invariably just $self meaning it's simple
enough for the additional curly braces to be omitted - $$hashref{$key}
as opposed to ${$hashref}{$key}. I am of course aware that references
are used elsewhere too.

I wish I knew why Mozilla isn't wrapping my messages properly.
Thanks again...

Stefan
 
T

Tad McClellan

Stefan said:
For I while now I have been coding OO Perl using the $self->{member}
syntax to grab elements out of my blessed hash reference. This works
just fine, however it recently occurred to me that since $self is always
a simple scalar variable (containing a reference), I could just use the
$$self{member} syntax instead. Personally, I find this easier to deal
with -


You are likely in the minority on that point.

it's one character less to type and occasionally I find it useful
to use @$self{member1, member2}, something which I still haven't figured
out how to do with ->.


You cannot take a slice with the arrow operator.

So my question... is there anything wrong with using this syntax?


Readability (for the majority).

The
only reason I ask is that I have never seen an OO Perl module which uses
it... everyone seems to use the $self->{member} version. Sorry if I'm
being and idiot and missing something blatantly obvious!


Your tastes differ from most other's tastes. (nothing wrong with that)

Nothing wrong with using the one _you_ like, assuming it will be
only you who does the maintenance.
 
M

Mina Naguib

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi...

For I while now I have been coding OO Perl using the $self->{member}
syntax to grab elements out of my blessed hash reference. This works
just fine, however it recently occurred to me that since $self is always
a simple scalar variable (containing a reference), I could just use the
$$self{member} syntax instead. Personally, I find this easier to deal
with - it's one character less to type and occasionally I find it useful
to use @$self{member1, member2}, something which I still haven't figured
out how to do with ->.

So my question... is there anything wrong with using this syntax? The
only reason I ask is that I have never seen an OO Perl module which uses
it... everyone seems to use the $self->{member} version. Sorry if I'm
being and idiot and missing something blatantly obvious!

Your question applies to hash references in general, not just bless()ed Perl objects.

There really is no difference except for human readability. The majority of Perl coders seem to
find this:
$href->{key}

A lot more self-explanatory and visually clearer than:
$$href{key}

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/Z3dNeS99pGMif6wRAtYnAJ9W2plWXvxBbeHRg9WM+kwuZ6IT4QCg6Wf3
WobH/UXAriRMldlvE0QpB1I=
=udW4
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top