Hash dereferencing and object-oriented Perl

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. #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...
    2. 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...
    3. 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...
    4. 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
    5. oriented object programming
      Greetings I have the following class: #!/usr/bin/perl package monParser; use HTML::Parser; @ISA = qw(HTML::Parser);
  3. #2

    Default 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

  4. #3

    Default 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

  5. #4

    Default 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

  6. #5

    Default Re: Hash dereferencing and object-oriented Perl

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

    Stefan wrote:
    > 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 - [url]http://enigmail.mozdev.org[/url]

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

    Mina Naguib Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139