pietyc has asked for the wisdom of the Perl Monks concerning the following question:

Hello
I am a very new to PERL. When I run the program why is it that $new_date is printed on three lines rather than one? Below is the output I get from the code further below. Thank you for any help.

-Charles
the year is 2004 the month is 03 the day is 04 2004 03 04 2004 03 03 #!/usr/bin/perl my $yr = `date -u +%Y`; #get the output of year my $mm = `date -u +%m`; #get the output of month my $dd = `date -u +%d`; #get the output of day my $yyyymmdd = `date -u +%Y%m%d`; print "the year is $yr"; print "the month is $mm"; print "the day is $dd"; $new_date = "$yr$mm$dd"; print $new_date; $yes_dd = $dd -1; $yes_dd = 0 . $yes_dd; $yes_date = $yr.$mm.$yes_dd; print "$yes_date\n";
Edit by dws to add <code> tags

Replies are listed 'Best First'.
Re: simple question
by tachyon (Chancellor) on Mar 04, 2004 at 23:27 UTC

    No one has mentioned it but you don't typically shell out to date in perl. Perl has lots of date handling built in. See localtime gmtime and lots of other stuff like Date::Manip Date::Calc Time::Local

    my $string_time = localtime(time()); my $yesterday = localtime(time()-24*60*60); my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); print "$string_time\n$yesterday\n"; printf "%02d:%02d:%02d %02d/%02d/%4d\n", $hour, $min, $sec, $mday, $mo +n+1, $year+1900;

    HTH

    cheers

    tachyon

Re: simple question
by matija (Priest) on Mar 04, 2004 at 20:51 UTC
    The output of the `date ...` contains the newline at the end. So when you print them, you print the newlines, too.

    You could chomp each variable separately, or you could chomp them all at once, like this:

    chomp ($yr,$mm,$dd,$yyyymmdd);
Re: simple question
by onegative (Scribe) on Mar 04, 2004 at 20:47 UTC
    use the chomp function to get rid of the new line characters that are hidden.
    chomp($yr); chomp($mm); chomp($dd);
Re: simple question
by esskar (Deacon) on Mar 04, 2004 at 20:50 UTC
    date adds a "\n" at the end of its outout
    my $yr = `date -u +%Y`; #get the output of year my $mm = `date -u +%m`; #get the output of month my $dd = `date -u +%d`; #get the output of day chomp $yr; chomp $mm; chomp $dd;
    hint:
    perldoc -f localtime
      just to second esskars hint:     perldoc -f localtime
      will point you to a much smoother way of determining time and date.
Re: simple question
by BUU (Prior) on Mar 04, 2004 at 20:49 UTC
    It prints on multiple lines becaue the data you get from `data -u etc` contains a new line at the end. That is why
    print "the year is $yr"; print "the month is $mm";
    appears on different lines in the output, both $yr and $mm have a new line at the end. If you want to get rid of the new line, try chomp.
Re: simple question
by ambrus (Abbot) on Mar 04, 2004 at 20:50 UTC

    This is because date prints a newline, and the backquotes catch it, so the newlines are in the string $yr and $mm.

    Date has to print a newline, as otherwise the output would get into the same line as your prompt if you call it from the shell (this is like this only on unix systems, dos prints a newline before the prompt).

    To correct it, remove the newlines from the strings with the command

    chomp ($yr, $mm, $dd);

    Update: Five identical answers to the question, wow!

Re: simple question
by bfdi533 (Friar) on Mar 04, 2004 at 22:28 UTC

    The output from date is terminated by a newline and you have that in your string.

    You should use chomp to strip off the newline like this:

    $yr = `date -u +%Y`; chomp $yr;

    Ed