Hey guys,
I just learned how to treat functions in Perl using Laura Lemay's "Perl In 21 Days" by Sams. The question was on p.272 of that tome and exercise one states:
Write a subroutine that does nothing but print its arguments, one argument per line, with each line numbered.
Now some clarification: Lemay's attitude is that if it is an imported function or one that is part of perl itself one refers to it as a "function". If it is one that you write yourself, it's what she refers to as a "subroutine". This was just her writing style, and says that one can refer to them as either and you'll still be right from a comp sci. standpoint.
I do plan on getting merlyn's book (as has been suggested) as well as the O'Reilly one by St. Wall. But in the meantime I am getting my toes wet with Lemay's book. One step at a time, eh?
Well anyway, here is how I approached the problem. I hope you goes don't mind me asking this everytime I give myself an assignment! But I would love to see how a more experienced programmer would do things. I am still studying my earlier thread, btw which generated and INCREDIBLE response for which I am grateful. Here goes:
#!/usr/bin/perl -w
&initarg;
while ($i !~ /stop/i) { # hey! all I could think of! :)
printf("Please enter phrase number %d : ", $count+1); # here's
+where knowing some c has helped me
chomp ($i = <STDIN>);
$phr[$count] = $i;
$count++;
}
&printarg(@phr);
sub printarg {
my $count = 1;
foreach $i (@_) {
print "Phrase number $count : $i\n";
$count++;
}
}
sub initarg {
@phr = (); # the author suggests initializing variables to null
$i = ""; # done yet?
$count = 0; # number of the phrase
}
Now I know that I may have separated some things out into their own functions that maybe I shouldn't have. That's the sort of thing I would love to recieve criticism on. Also, I got some feedback on one of my earlier posts that I should use 'strict', etc. I totally agree with that viewpoint! But I haven't "book learned" these concepts yet! I'm sure I'll get to them eventually. But at my toddler's level on perlness at the moment, I think I can afford to leave certain things till later when I learn about them.
THANKS!!!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.