The difference between the "beginner way" and a presumably
more advanced technique is merely style. As long as you
continue to produce working code, your skill will improve.
With experience comes an ability to write less code to get
the same job done. Fewer lines of code means less code to debug, which can
reduce the likelihood of producing defective code.
This artifact is difficult to explain
to employers who judge performance on lines of code produced.
Alas.
Here's some tips that could make your program "better",
though what that means in practice is actually highly
subjective.
my($day,$month,$year) = (localtime)[3,4,5];
$month = $month + 1;
$year = $year + 1900;
my $date = "$month\/$day\/$year";
This four line chunk could be reduced into something like
this:
my (@time) = (localtime)[4,3,5];
my ($date) = sprintf ("%d/%02d/%02d", $time[0]+1900, $time[1]+1,
+ $time[2]);
# Or if you don't care about formatting:
my ($date) = join ('/', $time[0]+1900, $time[1]+1, $time[2]);
# Or if you want to combine with your print, which would
# avoid having to declare $date:
printf GRAVE ("# consigned to the graveyard on %d/%d/%d\n",
$date[0]+1900,
$date[1]+1,
$date[2]);
sprintf allows you to convert a list of items into a
string, with formatting. It's a little hairy to use at first,
but with a little practice it comes in quite handy.
join()
allows you to put things together on the double, but doesn't
offer much flexibility.
while(<FILE>) {
push @output, $_;
}
You will notice here that you are taking something from one
array (<FILE>) and putting it into another (@output).
Why not do it in one shot?
@output = <FILE>;
BTW, it would seem that your program inserts things into
the GRAVE double-spaced. You're not removing the "\n" from
the input stream FILE, so it's still there when you go and
write into GRAVE with "$line\n"...
An interesting read is in the back of the "
Camel Book" where
they compare Program Efficiency vs. Programmer Efficiency and
how you can choose to optimize around either. The 'Camel Book'
is virtually essential to developing a full understanding of the Perl
language, as there are few with the same depth and scope.
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.