Yes, I can never correctly spell 'truly' (correction: I can seldom spell it). I am ashamed. What can I say: without the 'e' it just looks to me as if it's had an unfortunate accident with something sharp -- an object lesson in what happens when you run with scissors, perhaps ? It also looks almost as if it should be pronounced with a short 'u', but for that, I have to admit, two 'l's would be required -- as in 'cully'.
Of course many other words that end in 'e' don't drop it when the adverb is formed -- 'insanely', 'obscenely', 'approximately', 'effectively'. But that may be because they don't end in two vowels. So there's 'brusquely' and 'opaquely', but I guess the 'u' isn't really a vowel in this context. What about 'eerily', and of course 'duly' -- the later is clearly of a piece with 'truly'. But, lest we conclude there's a simple rule here, there's 'vaguely' and 'freely' -- admittedly the sound of the 'ue' in 'vague' is quite different from that in 'true', which may (or may not) be significant. Similarly, in the case of 'free' it may be that the 'ee' is effectively a compound vowel ?
English: what can I say ? Gosh, what a glorious mess !
Anyway, returning to what I suppose is the real matter: I think that to "really understand what Perl is doing" you do need some more formal grounding -- highlighting the stress I intended on the "really".
For instance, consider the following:
my @rows = () ;
while (my $line = <SHEET>) {
chomp($line) ;
my @columns = split /\s+/, $line ;
push @rows, \@columns ;
} ;
What do we think is going on here ?
- declare the (one dimensional) array @rows and ensure it is initialised empty.
- run a while loop using various pieces of commonly used Perl magic to step line by line through the file referred to by SHEET, declaring the working variable $line for the duration of the while loop
- chomp($line) removes the line ending from the input line (unless you're running on a *nix type system and the file came from winders, in which case it tends to remove half the line ending, SO STAY AWAKE)
- declare the (one dimensional) array @columns for the remaining duration of the loop, and assign to it the result of splitting up the input line.
- push a reference to the array @columns onto the array @rows (we understand that in Perl arrays and lists are strongly related)
The result is intended to be a two-dimensional array (or what passes for one in Perl) of cells. And, indeed that's what happens.
Now, references are sophisticated, but just taking the code at face value, one might worry:
- if @columns only exists for the duration of the enclosing block, what does the @rows array contain after the block ?
- why does this work at all, given that it appears to push the same thing, \@columns, on to @rows each time around the loop ?
So, we all know that this works because
my @columns doesn't declare an array, it declares something that contains a pointer to a data structure which contains the actual array and which lives on a heap. The
\@columns doesn't really return a reference to
@columns, it returns a copy of the pointer it contains. What's more, each time around the while loop
@columns is reset before the new
split /\s+/, $line is assigned to it, so each
\@columns is different and refers to a different data structure on the heap -- each of which persists after the block (leaving reference counting and garbage collection to one side, for the time being).
Scanning back over the above, I observe it's hard to describe what's really going on, without using some fairly heavy computer science concepts. IMO, the study of more formal language(s) is the way to gain understanding of these and how they work.
This is only one example, but I believe it illustrates a general point.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.