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

How would I make somehting print 2002 too 1903 so it would be
2002
2001
2000
all the way down to 1903, and does anyone know how to add a 2003 when the year 2003 comes trailing around.

Replies are listed 'Best First'.
•Re: Looping
by merlyn (Sage) on Jun 25, 2002 at 19:16 UTC
Re: Looping
by robobunny (Friar) on Jun 25, 2002 at 19:56 UTC
    well these responses are all fine if your computer can tell time, but what if it can't? and what if the clock is wrong? you don't want to leave a critical application like this to chance.
    open(MAIL, "|/usr/lib/sendmail timekeeper@superaccurateclock.com"); print MAIL <<END; From: Number Printer <numprint\@printnum.com> Subject: What Year is it? Please enter the year below and reply. Year: END close(MAIL); # ok, now watch our mail spool for the answer open(SPOOL, "tail -f /var/mail/spool/numprint |"); while(<SPOOL>) { if(/Year: (\d+)/) { $year = $1; last; } } close(SPOOL); # now we know for sure what year it is, we can use any # of the other suggestions to print the numbers
      How 1999. Why not be buzz-word compliant and use SOAP?

      use SOAP::Lite; $year = SOAP::Lite -> uri('http://www.yearknower.com/Year') -> proxy('http://soap.yearnknower.com/year.cgi') -> get_this_year -> result;

      -sam

      PS: Am I the only one that finds the OO calling style of the SOAP::Lite POD (shown above) bizarre? Is there any known precident or did the author invent it?

        Really good! I like your approach! And I think that robobunny got the point: if getting the right time is critical, you can't rely on yourself.

        Another solution would be to synchronize your server by means of the NTP protocol, which is the best way IMHO. I studied the principles of the protocol to implement a synchronization subnet for my job, and I was greatly surprised of how much statistical studies they put in; that makes NTP a higly-reliable protocol!

        Even if you don't want to invest a little to install a NTP server on your machine, you could take a look at the list of stratum 2 NTP servers and check if at least one of them offers the service by means of the daytime protocol too. To check, you can telnet to the tcp port 13 (i.e.: do a telnet host 13) and check if you get an output like:

        bronto@cooper:/tmp$ telnet myTimeServer 13 Trying 192.168.1.234... Connected to myTimeServer. Escape character is '^]'. Wed Jun 26 11:01:08 2002 Connection closed by foreign host.

        If so, you can get the current year using just Perl and Net::Telnet!

        Ciao!
        --bronto

        PS: if reading a document in italian language doesn't bother you, I can send a PDF doc I wrote about NTP and our synchronization subnet to anyone interested in. Just let me know!

        # Another Perl edition of a song:
        # The End, by The Beatles
        END {
          $you->take($love) eq $you->made($love) ;
        }

Re: Looping
by particle (Vicar) on Jun 25, 2002 at 19:28 UTC
    #!/usr/bin/perl use strict; use warnings; my $year = (localtime(time))[5]; print 1900+$year--,$/ while $year >= 3;

    ~Particle *accelerates*

Re: Looping
by screamingeagle (Curate) on Jun 25, 2002 at 19:17 UTC
    this should work :
    use strict; my $val = sprintf("%4d", (localtime(time))[5]+1900); while ($val >= 1903) { print "\nval : $val"; --$val; }
Re: Looping
by rbc (Curate) on Jun 25, 2002 at 19:47 UTC
    for ( reverse 1903..(1900 + (localtime)[5]) ) { print "$_\n"; }