Updated: (Added notes, original solution the same)
Updated: (Expanded solution due to peer pressure)
:-)
Don't have a Linux system up but presume chomp would do it:
$year=`date +%Y`;
chomp $year;
printf "$year";
I see choroba was faster on the post than I was, and also gave a more complete answer.
Okay, fine, I'll give in. Everyone is mentioning localtime, but my old eyes don't see the commensurate adjustment warning:
# Humanize the time
my $nowsse = time;
my ($nowsec,$nowmin,$nowhou,$nowdom,$nowmon,$nowyea,$nowdow,$nowdo
+y,$nowdst) = localtime($nowsse);
$nowyea += 1900;
$nowmon++;
With some further explanations of my TLAs:
- $nowsse: Seconds since Epoch
- $nowdom: Day of Month
- $nowdow: Day of Week
- $nowdoy: Day of Year
- $nowdst: Daylight Savings Time in effect?
Some additional thoughts:
- Any time you shell out to run an OS command you should really examine what you get back. Each OS behaves a little bit differently and you'll want to get a feel for what you get back. You may need to massage it for storage or modification (such as chomping off the newline in your example).
- Use of Perl native features is going to be more portable than a homespun OS interface; while not always the right answer, in my opinion based on a few decades of experience this should be a consideration -- even for one-off work which might later be used as a template for something less one-off-ish.
- Use of a published module (a la CPAN, etc.) is likely to be more portable than an OS-specific hack, but as always caveat emptor, so test its functionality and results before releasing it into the wild.
The original question was specifically about the newline on the OS response, so that's what my post covered. It was irresponsible of me to not mention, as so many other Monks have, the particular function you appeared to be attempting to perform was available via both native and module functionality.
But the other Monks did cover that, rather thoroughly. I love Perlmonks. :-)
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.