Ask a Question related to PERL Miscellaneous, Design and Development.
-
Stefan #1
Hash dereferencing and object-oriented Perl
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
Stefan Guest
-
#40061 [NEW]: Repeated dereferencing in object callbacks
From: a at b dot c dot de Operating system: Windows XP SP2 PHP version: 5.2.0 PHP Bug Type: Feature/Change Request Bug... -
object oriented perl programming
Hi i am a beginner in perl I am learning right now. i am very much confused with Object oriented perl programming. Can any one please suggest any... -
Object Oriented Web Design
Hi all, I am looking at using Jscript classes and COM objects written in Java to implement OOD on the web. I was wondering if anyone knows of any... -
packages object oriented
Does anyone have a good hold of how to do object oriented programming in perl? Maybe a few lines of code as examples? EDUB -
oriented object programming
Greetings I have the following class: #!/usr/bin/perl package monParser; use HTML::Parser; @ISA = qw(HTML::Parser); -
Uri Guttman #2
Re: Hash dereferencing and object-oriented Perl
>>>>> "S" == Stefan <stefan@unfunked.org> writes:
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
--
Uri Guttman ------ [email]uri@stemsystems.com[/email] -------- [url]http://www.stemsystems.com[/url]
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- [url]http://jobs.perl.org[/url]
Damian Conway Class in Boston - Sept 2003 -- [url]http://www.stemsystems.com/class[/url]
Uri Guttman Guest
-
Stefan #3
Re: Hash dereferencing and object-oriented Perl
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
Uri Guttman wrote:
>>>>>>>"S" == Stefan <stefan@unfunked.org> writes:
>
> 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
>Stefan Guest
-
Tad McClellan #4
Re: Hash dereferencing and object-oriented Perl
Stefan <stefan@unfunked.org> wrote:
> 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.
--
Tad McClellan SGML consulting
[email]tadmc@augustmail.com[/email] Perl programming
Fort Worth, Texas
Tad McClellan Guest
-
Mina Naguib #5
Re: Hash dereferencing and object-oriented Perl
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Stefan wrote:Your question applies to hash references in general, not just bless()ed Perl objects.> 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!
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 - [url]http://enigmail.mozdev.org[/url]
iD8DBQE/Z3dNeS99pGMif6wRAtYnAJ9W2plWXvxBbeHRg9WM+kwuZ6IT4Q Cg6Wf3
WobH/UXAriRMldlvE0QpB1I=
=udW4
-----END PGP SIGNATURE-----
Mina Naguib Guest



Reply With Quote

