Perl has, like many other languages global variables. So you don't *need* to pass a variable to a subroutine in order for the subroutine to "see" that variable. However, in subroutines, relying on globals to be set, and modifying them is a recipe for disaster. It may be OK in small scripts, but when your scripts grow you'll curse the decision to use globals more often than not (see tilly's post RE (tilly) 3: redeclaring variables with 'my' for a very good explanation of why "going global" without good reason is a bad idea). The general rule is: you pass variables to your subroutines, and then you localize them using my, as other monks in this thread have shown you (snax's suggestion for other reading material is where you should start). I'm not going to reproduce the perl documentation -- read up on the my operator and please don't confuse it with the local operator =)


In reply to Re: Re: subroutine syntax by arturo
in thread subroutine syntax by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.