Which text book is this? Where are you doing your course? It clearly isn't very good. I once observed an online training course for biologists to learn some basic Perl in which some of my colleagues took part. The guy who did it said that he advises against all that "strict, warnings and 'my'" stuff because he gets annoyed by all those error messages. Then he got himself into big trouble with a typo in a variable name which of course would have been caught by all those "annoying messages"! Took him ages to find the problem....
In short (and as already mentioned here): use strict (and warnings) and keep your variables scoped with 'my'. A good subroutine should have a clear input and output and have as few hidden side-effects as possible. Then it is clear to anybody what it does and it is easy to maintain the code. To achieve that you explicitly pass it the input variables, then access them with local variables as shown above ( something like my ($var1,$var2) = @_;) and then return one or more values explicitly. Sometimes a sub doesn't have an obvious return value and in that case you often return "1" or "0" to indicate success or failure of whatever the sub did.
Using global variables and making some changes to them somewhere in the sub means that it is not clear at all what it does and the hole point of making the sub (put some code in a blackbox), is lost.
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.