I have some very basic questions about use strict and subroutines. I'm just getting started so pardon my lack of understanding.
I have some code:
#!/usr/local/bin/perl use strict; myname(); print "my name is $myname\n"; sub myname { my $myname = "bill"; return $myname; }
I don't understand why this doesn't work. Where do I need to declare $myname? I thought I would have access to it at my print statement since I'm returning it in the subroutine.

I change that code a little bit in my experimenting to:

#!/usr/local/bin/perl use strict; my $myname = undef; myname(); print "my name is $myname\n"; sub myname { $myname = "bill"; return $myname; }
This prints out what I expect, but I notice that it works the same if I remove the return line from the subroutine. Why is this? Do I need to return the variable? What is the best way to go about using strict with subroutines?

Since I'm already displaying my basic lack of understanding of local/global variables, when do I need to pass variables into a subroutine? I'm currently creating variables and using them later in subroutines without passing them in and having no problems. Is this the wrong way to do it?

Thanks for any help.


In reply to use strict and subroutines 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.