P
Paul
Hello there. Please forgive this newbie question but I don't really
know Perl as I have only been using it for a few days.
I need to understand how a particular script works and am having
difficulty with one particular subroutine.
First, here is a sample of the input file (IN_FILE):
---
OVERVIEW
----------------------------
This is where notes and stuff goes.
And maybe some more notes here too.
MATERIALS
----------------------------
This is where more notes and stuff goes.
etc...
---
Second, here is a section of the Perl code that I am having
difficulties with:
---
sub parsefile
{
1: $overview=0; @overview = ();
2: # open file..
3: while ($line = <IN_FILE>)
4: {
5: if ($line =~ /^\U\w*/)
6: {
7: if ($line =~ /^OVERVIEW/)
8: {
9: $line = <IN_FILE>; next if $line !~ /--------+/;
10: if ($overview== 1) {error("More than one Overview section")}
11: $overview++;
12: $bin = \@overview;
13: next;
14: }
15: }
16: $line =~ s/\"/\'/g;
17: push (@$bin,$line);
18: }
19: if (!$overview) {error("Missing a Overview section")}
20: # etc...
}
---
So here's what I've figured out so far:
- line 3 starts a loop that goes through each line in the input file
- line 5 I had difficulties with, but I think it means "if the line
starts with an uppercase word char" then do this block
- line 7 says "if the line starts with 'OVERVIEW'" then do this block
(Here's where I start to lose it. I think I might be having
difficulties with the 'block' concept.. but let me continue..)
- line 9 starts by saying 'get the next line in the input file.
*Then* it says "next if" the line doesn't contain a bunch of dashes.
QUESTION 1: Where does "next" go? Does it go to the start of line 9
(i.e. the beginning of the current block) or to line 3?
- line 10 is an error check and calls some other method. (I'm okay
with this line.)
- line 11 increments the $overview count. ok.
- line 12 --> I HAVE NO IDEA!
QUESTION 2: What does line 12 do? Does it assign something to a
variable? If so, I don't get it. at all.
- line 13 - "next" to where? Line 3?
- line 16 does a double-quote substitution. ok.
- line 17 pushes the content of the $line into .. what *is* that
variable? where did it come from? (It's not used anywhere else in
the script!)
QUESTION 3: What is this variable in line 17?
Finally, somewhere along the line I think the @overview array is
assigned the content of each of the lines between the heading rows.
But I'm not sure how that's done. I'm pretty sure it happens in the
block above, but there's some kind of voodoo magic working that is
keeping me from seeing it.
Can some kind person please help me understand how this code works? I
have spent several hours, gone through 2 O'Reilly books and asked
everyone within yelling distance of my desk but so far I haven't been
able to completely understand this.
TIA.
know Perl as I have only been using it for a few days.
I need to understand how a particular script works and am having
difficulty with one particular subroutine.
First, here is a sample of the input file (IN_FILE):
---
OVERVIEW
----------------------------
This is where notes and stuff goes.
And maybe some more notes here too.
MATERIALS
----------------------------
This is where more notes and stuff goes.
etc...
---
Second, here is a section of the Perl code that I am having
difficulties with:
---
sub parsefile
{
1: $overview=0; @overview = ();
2: # open file..
3: while ($line = <IN_FILE>)
4: {
5: if ($line =~ /^\U\w*/)
6: {
7: if ($line =~ /^OVERVIEW/)
8: {
9: $line = <IN_FILE>; next if $line !~ /--------+/;
10: if ($overview== 1) {error("More than one Overview section")}
11: $overview++;
12: $bin = \@overview;
13: next;
14: }
15: }
16: $line =~ s/\"/\'/g;
17: push (@$bin,$line);
18: }
19: if (!$overview) {error("Missing a Overview section")}
20: # etc...
}
---
So here's what I've figured out so far:
- line 3 starts a loop that goes through each line in the input file
- line 5 I had difficulties with, but I think it means "if the line
starts with an uppercase word char" then do this block
- line 7 says "if the line starts with 'OVERVIEW'" then do this block
(Here's where I start to lose it. I think I might be having
difficulties with the 'block' concept.. but let me continue..)
- line 9 starts by saying 'get the next line in the input file.
*Then* it says "next if" the line doesn't contain a bunch of dashes.
QUESTION 1: Where does "next" go? Does it go to the start of line 9
(i.e. the beginning of the current block) or to line 3?
- line 10 is an error check and calls some other method. (I'm okay
with this line.)
- line 11 increments the $overview count. ok.
- line 12 --> I HAVE NO IDEA!
QUESTION 2: What does line 12 do? Does it assign something to a
variable? If so, I don't get it. at all.
- line 13 - "next" to where? Line 3?
- line 16 does a double-quote substitution. ok.
- line 17 pushes the content of the $line into .. what *is* that
variable? where did it come from? (It's not used anywhere else in
the script!)
QUESTION 3: What is this variable in line 17?
Finally, somewhere along the line I think the @overview array is
assigned the content of each of the lines between the heading rows.
But I'm not sure how that's done. I'm pretty sure it happens in the
block above, but there's some kind of voodoo magic working that is
keeping me from seeing it.
Can some kind person please help me understand how this code works? I
have spent several hours, gone through 2 O'Reilly books and asked
everyone within yelling distance of my desk but so far I haven't been
able to completely understand this.
TIA.