Re: how would you work with this material
[email protected] (Tad McClellan) Fri, 27 Dec 2002 12:29:31 -0600
| Newsgroups | perl.trainers |
|---|---|
| Message-ID | <[email protected]> |
On Thu, Dec 26, 2002 at 10:21:39PM -0800, Michael R. Wolf wrote:
>
> What do you think of material that has this material?
That it is not making effective use of the time available.
Perl is a large language. There can never be "enough" time, so
you have to be hard-hearted about what you'll spend the time on...
Also, it presents error-prone ways of doing things when they
can easily be done more safely. ie. it seems to prefer "tricks"
rather than solid Software Engineering.
> How would you present it to students in an "Intro to Perl Programming"
> course?
"I don't recommend doing this, but you might see it in other people's
code, so I'll cover it quickly so you'll understand it if you see it."
> What would you do about it if you were employed by the company that
> wrote it?
Whine.
> What about if you contracted to them?
Replace it with something more valuable, or at least be
brisk and caveat it.
> Changing the $#array also changes the length of the array
>
> Similar to using the pop function
TMTOWTDI is nice, but a "clear" way is better than an "obsure" way.
I'd teach the clear way and save the "obscure way" time
for something else.
> @teams = ("redsox","yankees", "bluejays","rockies","giants","mariners");
> print "Last element int he Array: $#teams\n";
Errr, the "last element in the array" is "mariners", not "5" :-)
> print "\@teams has " .($#teams + 1). " elements\n";
print "\@teams has " . @teams . " elements\n";
will do the same thing, and avoid the possibility of the
famous "off by one" class of mistakes.
> This funciton is useful for loopping through the array
But not as useful as foreach.
> for($i=0; $i < $#teams+1; $i++)
If you maintain the index yourself, you have the possibility
of making a mistake. If you use foreach instead, perl will do
the indexing for you.
Machines don't make mistakes, people do, so it's better to
leave it to a machine when possible.
> To grow or shrink the array, you simply change the value of the last
> index in the array
That is the obscure way. The clear way it to just put in into
the array (push/splice), and let perl grow it for you.
(unless you anticipate the array becoming "very large", but
beware of premature optimization...
)
> Output:
>
> Elements 11
> 1, 2, 3, , 300, , , , , ,
Errr, where are all of the "uninitialized" warning messages? <grin>
> - Get better performance behind the scenes by creating the array and
> growing it to the required length first instead of building it
> element by element.
Beware, as above.
> It is possible to empty an array by setting its length to a negative
> number
> @#array = -1;
^
^
I'll ignore the typo.
That's the obscure way.
@array = (); # the clear way
Is "job security" the primary objective of the class? <grin again>
> --
> Michael R. Wolf
> All mammals learn by playing!
Some lessons are better learned later, when you time to kill.
--
Tad McClellan SGML consulting
[email protected] Perl programming
Fort Worth, Texas