Just a few snooty comments on style:

  1. Indent code within a subroutine's blocks to make it easier to recognize which parts are actually within the subroutine block. This should also be done for conditionals and any other type of block (for instance, closures).
  2. You should really use lexical variables in a subroutine by using my $variable = .... You are actually using package globals. Having multiple subroutines working on globals can result in unexpected behavior if you aren't aware of the problems they can cause.
  3. There is a wonderful quoting mechanism in perl called qw. It returns an array of all items between the delimiters split on space. For instance, you would set @days and @mons with:
    my @days = qw(Sun Mon Tues Wed Thurs Fri Sat); my @mons = qw(Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec);
  4. To quote from perldoc -f localtime:
    If EXPR is omitted, localtime() uses the current time (localtime(time)).
  5. You can use a list slice to set your variables thusly:
    my ($mday,$mon,$year,$wday) = (localtime)[3..6];

    This tends to result in clearer code since you don't have to search in a sea of undefs to find the variables.

  6. The expression 2000 + ($year - 100) can be reduced to 1900 + $year.
  7. You can simplify the expression $year = 1900 + $year to $year += 1900.
  8. You don't need an intermediate variable to return the stringification of your variables. Instead of:
    $date = "$days[$wday] $mons[$mon] $mday $year"; return $date;
    You could quite simply do:
    return "$days[$wday] $mons[$mon] $mday $year";

Put all of that together and you get:

sub makeDate { my @days = qw(Sun Mon Tues Wed Thurs Fri Sat); my @mons = qw(Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec); my ($mday,$mon,$year,$wday) = (localtime)[3..6]; $year += 1900; return "$days[$wday] $mons[$mon] $mday $year"; }

You may wish to read perldoc perlsub for information concerning the different ways of calling subroutines, perldoc perlstyle for recommended code formatting (just for a guideline until you can develop your own style), and perldoc perldata for more information about perl data types and how they can be accessed.


In reply to Re: Using RETURN in a sub by Anonymous Monk
in thread Using RETURN in a sub by MysticFallout

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.