Ubuntu 中的编程语言(中)

Perl

Perl 语言在2010年6月编程语言排行榜中排名第八位。下面就是 GregorianTest.pl 程序:

|---------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 | use Time::Piece; use Time::Local; use Time::Seconds; my $dt = ``localtime``(timelocal(0, 0, 0, 4, 10 - 1, 1582)); print $dt``.``"\n"``; $dt += ONE_DAY; print $dt``.``"\n"``; |

Ubuntu 操作系统中已经预装了 Perl。解释执行:

复制代码
ben@ben-1520:~/work$ perl -v
This is perl, v5.10.1 (*) built for x86_64-linux-gnu-thread-multi
Copyright 1987-2009, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
ben@ben-1520:~/work$ perl GregorianTest.pl
Mon Oct  4 00:00:00 1582
Tue Oct  5 00:00:00 1582
ben@ben-1520:~/work$ 

运行结果和 .NET 平台的编程语言一样。

此外,还可以使用 cpan 命令来安装 DateTime 模块:

复制代码
ben@ben-1520:~/work$ sudo cpan
Terminal does not support AddHistory.
cpan shell -- CPAN exploration and modules installation (v1.9402)
Enter 'h' for help.
cpan[1]> install DateTime
cpan[2]> m DateTime
Module id = DateTime
    DESCRIPTION  A complete, easy to use date and time object
    CPAN_USERID  DROLSKY (Dave Rolsky )
    CPAN_VERSION 0.55
    CPAN_FILE    D/DR/DROLSKY/DateTime-0.55.tar.gz
    UPLOAD_DATE  2010-03-16
    DSLIP_STATUS bmpOp (beta,mailing-list,perl,object-oriented,Standard-Perl)
    MANPAGE      DateTime - A date and time object
    INST_FILE    /usr/local/lib/perl/5.10.1/DateTime.pm
    INST_VERSION 0.55
cpan[3]> q
Terminal does not support GetHistory.
Lockfile removed.
ben@ben-1520:~/work$ 

下面就是使用 DateTime 模块的 GregorianTest2.pl 程序:

|-----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 | use DateTime; my $dt = DateTime->new(``year``=>1582, ``month``=>10, ``day``=>4); print $dt``->day_abbr.``" "``.``$dt``->ymd.``"\n"``; $dt``->add(``days``=>1); print $dt``->day_abbr.``" "``.``$dt``->ymd.``"\n"``; |

解释执行:

复制代码
ben@ben-1520:~/work$ perl GregorianTest2.pl
Mon 1582-10-04
Tue 1582-10-05
ben@ben-1520:~/work$ 

运行结果还是和仅使用 Perl 核心模块的程序一样,没有什么改善。

PHP

PHP 语言在2010年6月编程语言排行榜中排名第四位。下面就是 GregorianTest.php 程序:

|-------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 | <?php $date = ``new DateTime(); echo $date``->getTimezone()->getName().``"\n"``; echo $date``->format(``'D Y-m-d'``).``"\n"``; $date``->setDate(1582, 10, 4); echo $date``->format(``'D Y-m-d'``).``"\n"``; $date``->add(``new DateInterval(``'P1D'``)); echo $date``->format(``'D Y-m-d'``).``"\n"``; ?> |

安装 PHP 客户端工具,可以作为交互窗口(使用 --interactive 或者 -a 参数),也可以解释执行:

复制代码
ben@ben-1520:~/work$ sudo apt-get install php5-cli
ben@ben-1520:~/work$ php -v
PHP 5.3.2-1ubuntu4.2 with Suhosin-Patch (cli) (built: May 13 2010 20:03:45) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
ben@ben-1520:~/work$ php -a
Interactive shell
php > echo "PHP ".phpversion();
PHP 5.3.2-1ubuntu4.2
php > exit;
ben@ben-1520:~/work$ php GregorianTest.php
Asia/Chongqing
Wed 2010-06-16
Tue 1582-10-04
Wed 1582-10-05
ben@ben-1520:~/work$ 

非常奇怪,PHP 语言的 DateTime 类居然认为1582年10月4日是星期二,既不是正确的星期四,也不是把格里历外推到1582年10月15日之前而得到的星期一。如果有哪位朋友知道这是什么原因,请在评论中告诉我。谢谢!

此外,PHP 语言还有和历法相关的函数。下面就是 GregorianTest2.php 程序:

|-------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php function writeline(``$cal``, ``$jd``) { ``$dt = cal_from_jd(``$jd``, ``$cal``); ``$info = cal_info(``$cal``); ``echo $dt``[``"abbrevdayname"``].``" "``.``$dt``[``"date"``]; ``echo "\tdays from 4713-01-01 B.C.: "``.``$jd``; ``echo "\t"``.``$info``[``"calname"``]; ``echo "\n"``; } $jd = cal_to_jd(CAL_JULIAN, 10, 4, 1582); writeline(CAL_JULIAN , 0); writeline(CAL_JULIAN , 1); writeline(CAL_JULIAN , ``$jd``); writeline(CAL_GREGORIAN, ``$jd + 1); writeline(CAL_JULIAN , ``$jd + 1); ?> |

解释执行:

复制代码
ben@ben-1520:~/work$ php GregorianTest2.php
Mon 0/0/0	days from 4713-01-01 B.C.: 0	Julian
Tue 1/2/-4713	days from 4713-01-01 B.C.: 1	Julian
Thu 10/4/1582	days from 4713-01-01 B.C.: 2299160	Julian
Fri 10/15/1582	days from 4713-01-01 B.C.: 2299161	Gregorian
Fri 10/5/1582	days from 4713-01-01 B.C.: 2299161	Julian
ben@ben-1520:~/work$ 

注意,在 GregorianTest2.php 程序中必须由用户自己指定使用儒略历还是格里历。

PHP 语言主要应用是服务端,是 LAMP (Linux + Apache + MySQL + PHP, or Perl, or Python) 的重要组成部分,用于架设动态网站。

Pascal

Pascal 语言在2010年6月编程语言排行榜中排名第十五位。下面就是 GregorianTest.pas 程序:

|----------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | Program GregorianTest(output); type ``StringArray = ``array``[``0..6``] ``of string``[``3``]; var ``t: TimeStamp; ``a: StringArray; procedure Init; begin ``a[``0``] := "Sun"; ``a[``1``] := "Mon"; ``a[``2``] := "Tue"; ``a[``3``] := "Wed"; ``a[``4``] := "Thu"; ``a[``5``] := "Fri"; ``a[``6``] := "Sat"; end``; begin ``Init; ``GetTimeStamp(t); ``WriteLn``(a[t``.``DayOfWeek], ``' '``, Date(t)); ``t``.``Year := ``1582``; ``t``.``Month := ``10``; ``t``.``Day := ``4``; ``t``.``DayOfWeek := ``4``; ``WriteLn``(a[t``.``DayOfWeek], ``' '``, Date(t)); end``. |

安装 GNU Pascal,编译和运行:

复制代码
ben@ben-1520:~/work$ sudo apt-get install gpc
ben@ben-1520:~/work$ gpc --version
gpc 20070904, based on gcc-4.1.3 20080704 (prerelease) (Ubuntu 2.1-4.1.2-27ubuntu2)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
ben@ben-1520:~/work$ gpc -o GregorianTest GregorianTest.pas
ben@ben-1520:~/work$ ./GregorianTest
Wed 16 Jun 2010
Thu  4 Oct 1582
ben@ben-1520:~/work$ 

在 GNU Pascal 中,我没有找到计算某一日期是星期几的函数,也没有找到计算某一日期的下一天的函数。程序之所以能够正确输出星期四,是由于在 GregorianTest.pas 程序的第24行对 DayOfWeek 字段进行了赋值。

Delphi

Delphi 语言在2010年6月编程语言排行榜中排名第十位。下面就是 GregorianTest2.pas 程序:

|-------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | Program GregorianTest2; Uses sysutils; var ``t: TTimeStamp; procedure WriteLine(t: TTimeStamp); begin ``Write``(FormatDateTime(``'ddd yyyy-mm-dd'``, TimeStampToDateTime(t))); ``Writeln``(``' days past 0001-01-01: '``, t``.``Date); end``; Begin ``t := DateTimeToTimeStamp(EnCodeDate(``1``, ``1``, ``1``)); ``WriteLine(t); ``t := DateTimeToTimeStamp(EnCodeDate(``1582``, ``10``, ``4``)); ``WriteLine(t); ``t``.``Date := t``.``Date + ``1``; ``WriteLine(t); ``t := DateTimeToTimeStamp(Now); ``WriteLine(t); ``t``.``Date := t``.``Date + ``1``; ``WriteLine(t); End``. |

安装兼容 Delphi 的 Free Pascal,编译和运行:

复制代码
ben@ben-1520:~/work$ sudo apt-get install fp-compiler
ben@ben-1520:~/work$ fpc GregorianTest2.pas
Free Pascal Compiler version 2.4.0-2 [2010/03/06] for x86_64
Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Linux for x86-64
Compiling GregorianTest2.pas
Linking GregorianTest2
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
22 lines compiled, 0.4 sec 
ben@ben-1520:~/work$ ./GregorianTest2
Sat 0001-01-01  days past 0001-01-01: 1
Sat 1582-10-04  days past 0001-01-01: 577725
Fri 1582-10-05  days past 0001-01-01: 577726
Wed 2010-06-16  days past 0001-01-01: 733939
Thu 2010-06-17  days past 0001-01-01: 733940
ben@ben-1520:~/work$ 
相关推荐
starzy19908 小时前
深入理解Scala隐式函数:自动类型转换
开发语言·后端·scala
Eyfcom9 小时前
〖物流 SaaS 实战 02〗SaaS 租户的计费与订阅模型:从“一锤子买卖”到“按需订阅”
linux·服务器·数据库·快递系统·物流saas实战
忆挽篱笙歌12 小时前
systemV进程通信
linux·运维·服务器
无奈笑天下17 小时前
银河麒麟桌面操作系统上磁盘无法自动挂载问题处理
linux·运维·服务器
木头左17 小时前
【Linux运维】服务器磁盘清理与Docker日志治理实战:一次巡检释放70G空间
linux·docker·磁盘清理·日志治理
星恒随风18 小时前
MySQL 从零入门(七):索引原理与 B+ 树详解——从磁盘 I/O 到 InnoDB 聚簇索引
linux·服务器·数据库·笔记·mysql
多弗朗皮卡丘1 天前
网络基础:数据链路层与其他协议或技术
linux·网络·tcp/ip
dingdingfish2 天前
TLPI 第52章 读书笔记:POSIX Message Queues
linux·mq·ipc·posix·message·tlpi