By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,178 Members | 987 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,178 IT Pros & Developers. It's quick & easy.

Help...Perl Beginner...

P: 4
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.

Expand|Select|Wrap|Line Numbers
  1. $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,
Jul 30 '08 #1
Share this Question
Share on Google+
2 Replies


KevinADC
Expert 2.5K+
P: 4,059
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:

Expand|Select|Wrap|Line Numbers
  1.     sub new {
  2.         my $self  = {};
  3.         $self->{NAME}   = undef;
  4.         $self->{AGE}    = undef;
  5.         $self->{PEERS}  = [];
  6.         bless($self);           # but see below
  7.         return $self;
  8.     }
  9.  
first is this line:

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1.         $self->{NAME}   = undef;
  2.         $self->{AGE}    = undef;
  3.         $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:

Expand|Select|Wrap|Line Numbers
  1. $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:

Expand|Select|Wrap|Line Numbers
  1. 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
Jul 30 '08 #2

P: 4
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,

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:

Expand|Select|Wrap|Line Numbers
  1.     sub new {
  2.         my $self  = {};
  3.         $self->{NAME}   = undef;
  4.         $self->{AGE}    = undef;
  5.         $self->{PEERS}  = [];
  6.         bless($self);           # but see below
  7.         return $self;
  8.     }
  9.  
first is this line:

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1.         $self->{NAME}   = undef;
  2.         $self->{AGE}    = undef;
  3.         $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:

Expand|Select|Wrap|Line Numbers
  1. $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:

Expand|Select|Wrap|Line Numbers
  1. 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
Jul 30 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.