Help...Perl Beginner...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stirfries
    New Member
    • Jul 2008
    • 4

    #1

    Help...Perl Beginner...

    Hello Perl Gurus,

    I am new to Perl programming but not new to programming in general. Perl really fascinates me with its cryptic syntax.

    I have been analyzing a Perl system to understand and make some minor changes. I couldn't understand what the following line of code does.

    Code:
    $self->{_tagStack} = [];
    I tried googling on Initialization for perl arrays and they are mostly initialized with () and not []. I am clueless and thought some Perl expert can figure this out in a second. Can someone please shed some light on this syntax?

    I do understand what $self->{_tagStack} means. If I am not wrong, _tagStack is more like a instance variable for this Perl class.

    And this piece of code is defined, in NEW() method.

    Thanks,
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    It is a little bit complex and confusing at first when you see a reference in perl because it looks familiar and unfamiliar at the same time:

    $self->{_tagStack} = [];

    That looks like a bit of OOP style programming because of the $self reference.
    You maybe dug that out of something similar to this:

    Code:
        sub new {
            my $self  = {};
            $self->{NAME}   = undef;
            $self->{AGE}    = undef;
            $self->{PEERS}  = [];
            bless($self);           # but see below
            return $self;
        }
    first is this line:

    Code:
    my $self  = {};
    it creates a reference to an anonymous hash. Thats what the curly {} brackets signify. This is often refered to as the "hash-reference-as-an-object" idiom.

    Then the next three lines define some hash keys and some initial values:

    Code:
            $self->{NAME}   = undef;
            $self->{AGE}    = undef;
            $self->{PEERS}  = [];
    the last one is a hash key that has an anonymous array (an empty array) as its value. We can assume some data will be stuck into that array later. Its essentially the same as this example:

    Code:
    $array->{PEERS} = ['foo', 'bar', 'baz'];
    $array is a reference to a hash of array, to get to the data stored in a reference you have to use dereferencing:

    Code:
    print $array->{PEERS}[0]; (prints foo)
    Thats what the arrow operator "->" is used for. What you want is to read about complex data structures with perl and maybe object oriented programming.

    Comment

    • stirfries
      New Member
      • Jul 2008
      • 4

      #3
      KevinADC

      Great....Thanks a lot...Your explanation was very very helpful. Now, I am starting to understand the intricacies of references/arrays/hashes in perl world. Thanks a lot for your insight.

      Thanks,

      Originally posted by KevinADC
      It is a little bit complex and confusing at first when you see a reference in perl because it looks familiar and unfamiliar at the same time:

      $self->{_tagStack} = [];

      That looks like a bit of OOP style programming because of the $self reference.
      You maybe dug that out of something similar to this:

      Code:
          sub new {
              my $self  = {};
              $self->{NAME}   = undef;
              $self->{AGE}    = undef;
              $self->{PEERS}  = [];
              bless($self);           # but see below
              return $self;
          }
      first is this line:

      Code:
      my $self  = {};
      it creates a reference to an anonymous hash. Thats what the curly {} brackets signify. This is often refered to as the "hash-reference-as-an-object" idiom.

      Then the next three lines define some hash keys and some initial values:

      Code:
              $self->{NAME}   = undef;
              $self->{AGE}    = undef;
              $self->{PEERS}  = [];
      the last one is a hash key that has an anonymous array (an empty array) as its value. We can assume some data will be stuck into that array later. Its essentially the same as this example:

      Code:
      $array->{PEERS} = ['foo', 'bar', 'baz'];
      $array is a reference to a hash of array, to get to the data stored in a reference you have to use dereferencing:

      Code:
      print $array->{PEERS}[0]; (prints foo)
      Thats what the arrow operator "->" is used for. What you want is to read about complex data structures with perl and maybe object oriented programming.

      http://perldoc.perl.org/perldsc.html

      Comment

      Working...