找回密码
 立即注册
首页 业界区 业界 Ubuntu 中的编程语言(中)

Ubuntu 中的编程语言(中)

郗燕岚 2025-5-29 19:45:59
在上一篇随笔中介绍了十种编程语言。这次介绍四种编程语言:Perl、PHP、Pascal 和 Delphi。
Perl

Perl 语言在2010年6月编程语言排行榜中排名第八位。下面就是 GregorianTest.pl 程序:
  1. use Time::Piece;
  2. use Time::Local;
  3. use Time::Seconds;
  4. my $dt = localtime(timelocal(0, 0, 0, 4, 10 - 1, 1582));
  5. print $dt."\n";
  6. $dt += ONE_DAY;
  7. print $dt."\n";
复制代码
Ubuntu 操作系统中已经预装了 Perl。解释执行:
  1. ben@ben-1520:~/work$ <strong>perl -v</strong>
  2. This is perl, v5.10.1 (*) built for x86_64-linux-gnu-thread-multi
  3. Copyright 1987-2009, Larry Wall
  4. Perl may be copied only under the terms of either the Artistic License or the
  5. GNU General Public License, which may be found in the Perl 5 source kit.
  6. Complete documentation for Perl, including FAQ lists, should be found on
  7. this system using "man perl" or "perldoc perl".  If you have access to the
  8. Internet, point your browser at http://www.perl.org/, the Perl Home Page.
  9. ben@ben-1520:~/work$ <strong>perl GregorianTest.pl</strong>
  10. <font color="#ff0000">Mon Oct  4 00:00:00 1582
  11. Tue Oct  5 00:00:00 1582</font>
  12. ben@ben-1520:~/work$
复制代码
运行结果和 .NET 平台的编程语言一样。
此外,还可以使用 cpan 命令来安装 DateTime 模块:
  1. ben@ben-1520:~/work$ <strong>sudo cpan</strong>
  2. Terminal does not support AddHistory.
  3. cpan shell -- CPAN exploration and modules installation (v1.9402)
  4. Enter 'h' for help.
  5. cpan[1]> <strong>install DateTime</strong>
  6. cpan[2]> <strong>m DateTime</strong>
  7. Module id = DateTime
  8.     DESCRIPTION  A complete, easy to use date and time object
  9.     CPAN_USERID  DROLSKY (Dave Rolsky )
  10.     CPAN_VERSION 0.55
  11.     CPAN_FILE    D/DR/DROLSKY/DateTime-0.55.tar.gz
  12.     UPLOAD_DATE  2010-03-16
  13.     DSLIP_STATUS bmpOp (beta,mailing-list,perl,object-oriented,Standard-Perl)
  14.     MANPAGE      DateTime - A date and time object
  15.     INST_FILE    /usr/local/lib/perl/5.10.1/DateTime.pm
  16.     INST_VERSION 0.55
  17. cpan[3]> <strong>q</strong>
  18. Terminal does not support GetHistory.
  19. Lockfile removed.
  20. ben@ben-1520:~/work$
  21. </autarch>
复制代码
下面就是使用 DateTime 模块的 GregorianTest2.pl 程序:
  1. use DateTime;
  2. my $dt = DateTime->new(year=>1582, month=>10, day=>4);
  3. print $dt->day_abbr." ".$dt->ymd."\n";
  4. $dt->add(days=>1);
  5. print $dt->day_abbr." ".$dt->ymd."\n";
复制代码
解释执行:
  1. ben@ben-1520:~/work$ <strong>perl GregorianTest2.pl</strong>
  2. <font color="#ff0000">Mon 1582-10-04
  3. Tue 1582-10-05</font>
  4. ben@ben-1520:~/work$
复制代码
运行结果还是和仅使用 Perl 核心模块的程序一样,没有什么改善。
PHP

PHP 语言在2010年6月编程语言排行榜中排名第四位。下面就是 GregorianTest.php 程序:
  1. [/code]安装 PHP 客户端工具,可以作为交互窗口(使用 --interactive 或者 -a 参数),也可以解释执行:
  2. [code]ben@ben-1520:~/work$ <strong>sudo apt-get install php5-cli</strong>
  3. ben@ben-1520:~/work$ <strong>php -v</strong>
  4. PHP 5.3.2-1ubuntu4.2 with Suhosin-Patch (cli) (built: May 13 2010 20:03:45)
  5. Copyright (c) 1997-2009 The PHP Group
  6. Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
  7. ben@ben-1520:~/work$ <strong>php -a</strong>
  8. Interactive shell
  9. php > <strong>echo "PHP ".phpversion();</strong>
  10. PHP 5.3.2-1ubuntu4.2
  11. php > <strong>exit;</strong>
  12. ben@ben-1520:~/work$ <strong>php GregorianTest.php</strong>
  13. Asia/Chongqing
  14. Wed 2010-06-16
  15. <font color="#ff0000">Tue 1582-10-04
  16. Wed 1582-10-05</font>
  17. ben@ben-1520:~/work$
复制代码
非常奇怪,PHP 语言的 DateTime 类居然认为1582年10月4日是星期二,既不是正确的星期四,也不是把格里历外推到1582年10月15日之前而得到的星期一。如果有哪位朋友知道这是什么原因,请在评论中告诉我。谢谢!
此外,PHP 语言还有和历法相关的函数。下面就是 GregorianTest2.php 程序:
  1. [/code]解释执行:
  2. [code]ben@ben-1520:~/work$ <strong>php GregorianTest2.php</strong>
  3. Mon 0/0/0        days from 4713-01-01 B.C.: 0        Julian
  4. Tue 1/2/-4713        days from 4713-01-01 B.C.: 1        Julian
  5. <font color="#ff0000">Thu 10/4/1582        days from 4713-01-01 B.C.: 2299160        Julian
  6. Fri 10/15/1582        days from 4713-01-01 B.C.: 2299161        Gregorian</font>
  7. Fri 10/5/1582        days from 4713-01-01 B.C.: 2299161        Julian
  8. ben@ben-1520:~/work$
复制代码
注意,在 GregorianTest2.php 程序中必须由用户自己指定使用儒略历还是格里历。
PHP 语言主要应用是服务端,是 LAMP (Linux + Apache + MySQL + PHP, or Perl, or Python) 的重要组成部分,用于架设动态网站。
Pascal

Pascal 语言在2010年6月编程语言排行榜中排名第十五位。下面就是 GregorianTest.pas 程序:
  1. Program GregorianTest(output);
  2. type
  3.   StringArray = array[0..6] of string[3];
  4. var
  5.   t: TimeStamp;
  6.   a: StringArray;
  7. procedure Init;
  8. begin
  9.   a[0] := "Sun";
  10.   a[1] := "Mon";
  11.   a[2] := "Tue";
  12.   a[3] := "Wed";
  13.   a[4] := "Thu";
  14.   a[5] := "Fri";
  15.   a[6] := "Sat";
  16. end;
  17. begin
  18.   Init;
  19.   GetTimeStamp(t);
  20.   WriteLn(a[t.DayOfWeek], ' ', Date(t));
  21.   t.Year := 1582;
  22.   t.Month := 10;
  23.   t.Day := 4;
  24.   t.DayOfWeek := 4;
  25.   WriteLn(a[t.DayOfWeek], ' ', Date(t));
  26. end.
复制代码
安装 GNU Pascal,编译和运行:
  1. ben@ben-1520:~/work$ <strong>sudo apt-get install gpc</strong>
  2. ben@ben-1520:~/work$ <strong>gpc --version</strong>
  3. gpc 20070904, based on gcc-4.1.3 20080704 (prerelease) (Ubuntu 2.1-4.1.2-27ubuntu2)
  4. Copyright (C) 2006 Free Software Foundation, Inc.
  5. This is free software; see the source for copying conditions.  There is NO
  6. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  7. ben@ben-1520:~/work$ <strong>gpc -o GregorianTest GregorianTest.pas</strong>
  8. ben@ben-1520:~/work$ <strong>./GregorianTest</strong>
  9. Wed 16 Jun 2010
  10. <font color="#ff0000">Thu  4 Oct 1582</font>
  11. ben@ben-1520:~/work$
复制代码
在 GNU Pascal 中,我没有找到计算某一日期是星期几的函数,也没有找到计算某一日期的下一天的函数。程序之所以能够正确输出星期四,是由于在 GregorianTest.pas 程序的第24行对 DayOfWeek 字段进行了赋值。
Delphi

Delphi 语言在2010年6月编程语言排行榜中排名第十位。下面就是 GregorianTest2.pas 程序:
  1. Program GregorianTest2;
  2. Uses sysutils;
  3. var
  4.   t: TTimeStamp;
  5. procedure WriteLine(t: TTimeStamp);
  6. begin
  7.   Write(FormatDateTime('ddd yyyy-mm-dd', TimeStampToDateTime(t)));
  8.   Writeln('  days past 0001-01-01: ', t.Date);
  9. end;
  10. Begin
  11.   t := DateTimeToTimeStamp(EnCodeDate(1, 1, 1));
  12.   WriteLine(t);
  13.   t := DateTimeToTimeStamp(EnCodeDate(1582, 10, 4));
  14.   WriteLine(t);
  15.   t.Date := t.Date + 1;
  16.   WriteLine(t);
  17.   t := DateTimeToTimeStamp(Now);
  18.   WriteLine(t);
  19.   t.Date := t.Date + 1;
  20.   WriteLine(t);
  21. End.
复制代码
安装兼容 Delphi 的 Free Pascal,编译和运行:
  1. ben@ben-1520:~/work$ <strong>sudo apt-get install fp-compiler</strong>
  2. ben@ben-1520:~/work$ <strong>fpc GregorianTest2.pas</strong>
  3. Free Pascal Compiler version 2.4.0-2 [2010/03/06] for x86_64
  4. Copyright (c) 1993-2009 by Florian Klaempfl
  5. Target OS: Linux for x86-64
  6. Compiling GregorianTest2.pas
  7. Linking GregorianTest2
  8. /usr/bin/ld: warning: link.res contains output sections; did you forget -T?
  9. 22 lines compiled, 0.4 sec
  10. ben@ben-1520:~/work$ <strong>./GregorianTest2</strong>
  11. Sat 0001-01-01  days past 0001-01-01: 1
  12. <font color="#ff0000">Sat 1582-10-04  days past 0001-01-01: 577725
  13. Fri 1582-10-05  days past 0001-01-01: 577726</font>
  14. Wed 2010-06-16  days past 0001-01-01: 733939
  15. Thu 2010-06-17  days past 0001-01-01: 733940
  16. ben@ben-1520:~/work$
复制代码
非常奇怪,Free Pascal 的 Date/Time routines 居然认为1582年10月4日是星期六,既不是正确的星期四,也不是把格里历外推到1582年10月15日之前而得到的星期一。而且更奇怪的是,它认为1582年10月4日星期六的下一天是1582年10月5日星期五,从星期六倒退回星期五了。如果有哪位朋友知道这是什么原因,请在评论中告诉我。谢谢!
 
更多的编程语言将在下一篇随笔中介绍。
 
参考资料


  • The Perl Programming Language
  • Perl: DateTime
  • Perl: Date::Calc - Gregorian calendar date calculations
  • Perl: Time:ocal - efficiently compute time from local and GMT time 
  • PHP: Hypertext Preprocessor
  • PHP: Date and Time Related Extensions
  • Wikipedia: Pascal (programming language)
  • The GNU Pascal Manual: Date And Time Routines
  • Free Pascal – Advanced open source Pascal compiler for Pascal and Object Pascal
  • Free Pascal: Date/time routines 
  • Wikipedia: LAMP (software bundle)

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册