473,325 Members | 2,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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.

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
2 1189
KevinADC
4,059 Expert 2GB
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
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

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

Similar topics

5
by: John Smith | last post by:
Can someone point me to an example of how to implement and access the kind of object shown below? Most of the examples if found are an object that contains one other object. I need to create an...
1
by: J-miami | last post by:
I am just starting to learn Perl. I had an idea that there should be free open-source Internet Cafe management software for Linux. I searched around online but couldn't find anything. The...
2
by: Daniel Tonks | last post by:
Here's the situation: I have a Perl-based forum that I wrote myself. It currently supports user accounts, but these are stored in a flat text file (as is everything else for that matter). I'm...
9
by: Dieter Vanderelst | last post by:
Dear all, I'm currently comparing Python versus Perl to use in a project that involved a lot of text processing. I'm trying to determine what the most efficient language would be for our...
1
by: homeflash | last post by:
Before I get into deep programming. I want to find out how to run it. I have abyss web server on. I have no problem running CGI script (My perl is installed) However, I can't get any .pl file to...
1
by: vitalia | last post by:
Hello everyone, I really need help to create this syntax. I am a beginner. I was able to write just simple code but I couldn't restrict the number of digits and letters. What I need is: All...
17
by: beginner | last post by:
Hi All, This is just a very simple question about a python trick. In perl, I can write __END__ in a file and the perl interpreter will ignore everything below that line. This is very handy...
1
by: alsaz | last post by:
Hi I'm a relatively PERL beginner but would like to improve my PERL programming and have been requested to scrape phone prices from http://www.envirofone.com/default.aspx I've scrapped a few...
4
crystal2005
by: crystal2005 | last post by:
Hello guys.... I'm newbie in Perl Language. So, in here i would like to ask several questions about Perl itself and not about the coding. I have been given a task to use Perl for directory...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.