选择显示字体大小

linux下编程入门

介绍

使用了linux一段时间后,很多朋友都希望能为这个开放辕马世界做自己的贡献,更多的参加到这个open source的社团里。面对无数的自由软件,对于一个稍微不太普通的用户来说去解开一个tar文件,然后修改makefile去编译辕马都是很常有的。我们不满足简单的编译和使用别人的程序,我们应该亲自参加到这个开发辕马的创作之中。对于一个初学者来说,面对五花八门的开发工具,如何选择呢?
使用什么开发工具去开发linux程序呢?我们这里做个简单的介绍。这里是你成为linux hacker征途上的第一步。


编程模式

让我们开始第一步。作为一个初学者应该首先使用什么样的编程风格或者说编程模式呢?有很多的选择,例如基于函数的、基于过程的开发,还有面向对象的开发等等。要成为一个好的programmer,对于初学者,最好使用某种语言对各种编程模式都尝试一遍。

comp.lang.functional讨论组里面的faq中定义了面向函数的语言,说“"a style of programming that emphasizes the evaluation of expressions, rather than execution of commands. the expressions in these language are formed by using functions to combine basic values. a functional language is a language that supports and encourages programming in a functional style."

linux平台下面,比较常见的面向函数的开发语言有:ml、haskell、scheme等。

面向过程的语言是一种把程序要完成的动作分割成一个一个“过程(procedures)”的语言。一个典型的面向过程的开发语言的组织结构,我们可以看看流程图,就可以感觉出来。最常见的面向过程的开发语言就是pascal和c语言了。

面向对象的开发语言的核心就是用对象来表征所要执行的主体。例如要开发一个高速公路的模拟系统,他首先定义一个交通工具的类,包括所有交通工具的公共属性;然后继承这个类,可以派生出汽车类、自行车类等等。linux平台下最常见的面向对象的语言是c++、java、python、smalltalk和eiffel等。

还有混合的语言,你可以用多种编程模式去写自己代码,例如phpperl

如何选择?一般来说最好每种模式都有一个了解。最好这样开始你的编程旅程,首先给自己定一个小project。有了一个清晰的目标后,选择编程语言和编程模式就容易了。如果使用某种语言开发起来容易,那么你就先学习使用这种语言。

例如,我开始想学习更多的关于数据库web编程的时候,我决定建一个书籍数据库,包括我拥有的书的信息还有我要计划购买的书籍信息。经过我的查找比较,我认为我的想法可以用phpmysql来实现更方便。然后我就学习了phpmysql的一些知识,完成了这个小的系统。

我个人的编程体会,我至少使用了4种语言,包括:c、php、sql和perl
c语言是最灵活和方便的开发语言,在linux下使用最多的开发语言就是它了。一旦学会了c语言,我们就可以非常容易的学习和使用其他的语言了。所以我建议你学习c语言编程。c语言是一种高级语言,它编写内核级的代码和驱动程序都非常方便。linux下面有很多开发c语言的函数库和开发工具,我们都可以方便的使用它们,这也是使用c语言开发代码的好处。

php是hyper-text processor的缩写,是一种解释执行的语言,一般来说运行在web服务器端。如果你会c语言,那么最多花一天的时间就可以学会phpphp给你在web编程方面很大的方便。

sql是standard query language的缩写,就是标准的访问数据的查询语言。很多php程序中都包含了使用sql语言去访问数据库的代码。sql基本上使用有点类似英文的语法,所以学起来非常容易。如果你学习php和c,那么sql可以简单的顺便学习。
perl是一种使用比较广的教本语言。很多语法来自c和unix的shell工具。perl拥有很多的可以添加的模块,在cpan计划中。使用这些模块,开发程序编得非常容易了。

下面是一个perl程序例子:

#!/usr/bin/perl
# nmsu job grabber
# matt michie (mmichie@linux.com)
#----------------------------------------------------------------------
#copyright (c) 2000, matt michie (mmichie@linux.com) (all rights reserved.)
#
#redistribution and use in source and binary forms, with or without
#modification, are permitted provided that the following conditions are met:
#
#redistributions of source code must retain the above copyright notice,
#this list of conditions and the following disclaimer.
#
#redistributions in binary form must reproduce the above copyright notice,
#this list of conditions and the following disclaimer in the documentation
#and/or other materials provided with the distribution.
#
#the names of this programs contributors may not be used to endorse or
#promote products derived from this software without specific prior
#written permission.
#
#this software is provided by the copyright holders and contributors
#``as is'' and any express or implied warranties, including, but not
#limited to, the implied warranties of merchantability and fitness for
#a particular purpose are disclaimed. in no event shall the regents or
#contributors be liable for any direct, indirect, incidental, special,
#exemplary, or consequential damages (including, but not limited to,
#procurement of substitute goods or services; loss of use, data, or profits;
#or business interruption) however caused and on any theory of liability,
#whether in contract, strict liability, or tort (including negligence or
#otherwise) arising in any way out of the use of this software, even if
#advised of the possibility of such damage.
#----------------------------------------------------------------------
#instructions for use:
#
#you must have lwp installed to fetch the jobs. if you wish to use the
#e-mail notification you also must have sendmail installed.
#
#the script has the following command line flags:
#
#-v : print the version
#
#-t : max threshold to include the salary in statistics calculations
#
#-b (number) : boundary of salary after which program will shoot off an
# e-mail
#
#-m (email address) : tell the program to notify you with email. the arguement
# is a valid e-mail address.
#
#-q : quiet mode, don't print statistics, automatically used in email mode.
#example use:
#
#fetch.pl -b 7.50 -m mmichie@linux.com
#
#this tells the program to fetch the jobs list and only send e-mail
#notification if there are any jobs with higher pay than $7.50.
#----------------------------------------------------------------------

use lwp::simple;
use getopt::std;

$version = 'job grabber 0.01';

getopts('vqt:m:b:') die "check your command line args! ";

if ($opt_t != 0) {
$max = $opt_t;
}
else {
$max = 20; # max threshold to include salary in count
}

$min = 0; # min threshold to include salary in count

$highest = 0; # highest salary
$total = 0; # total jobs counted
$count = 0; # total jobs which fall inside min/max thresholds

$oncampus = "http://www.nmsu.edu/~pment/oncampu.htm";
#$offcampus = "http://www.nmsu.edu/~pment/offcampu.htm";

$url = $oncampus;

if ($opt_v) {
print "$version ";
exit(0);
}
if ($opt_m && !$opt_q) {
$opt_q = true;
}

&fetch_page;
&stats;

if ($opt_m && ($highest > $opt_b)) {
&email;
}
elsif (!$opt_b && $opt_m) {
$opt_q = 1;
}

sub fetch_page {
unless (defined ($page = get($url))) {
die "there was an error getting url: $url ";
}

@page = split(/ /, $page);

foreach $line (@page) {



&#36;line =~ s/<[^>]*>//g; # strip html codes
if (!&#36;opt_q && &#36;line =~ /on campus job postings as of:/) {
print "&#36;line ";
}
elsif (&#36;line =~ /salary:/) {
push @pay, (split (/:/, &#36;line))[1];
}
}
}

sub stats {
foreach &#36;elm (@pay) {
&#36;total++;
next if (&#36;elm <= &#36;min &#36;elm >= &#36;max);

if (&#36;elm > &#36;highest) {
&#36;highest = &#36;elm;
}

&#36;count++;
&#36;accum += &#36;elm;
}

if (&#36;count == 0) {
die "eiiiiiiiiiieeeeeeeeeeeeeeeeeeee divide by zero :( ";
}
else {
&#36;avg = &#36;accum / &#36;count;
}

if (!&#36;opt_q) {
print "total jobs listed: &#36;total ";
print "number of jobs counted for pay: &#36;count ";
print "highest hourly pay: &#36;&#36;highest ";
printf "average hourly pay: &#36;%.2f ", &#36;avg;
}
}

sub email {
open(sendmail, "/usr/lib/sendmail -oi -t -odq")
or die "can't fork for sendmail: &#36;! ";

print sendmail <<"eof";
from: job grabber <&#36;opt_m>
to: &#36;opt_m <&#36;opt_m>
subject: jobby

total jobs listed: &#36;total
number of jobs counted for pay: &#36;count
highest hourly pay: &#36;&#36;highest average hourly pay: &#36;&#36;avg
eof

close(sendmail) or warn "sendmail didn't close nicely";
}

从上面的代码中你可以看到,大部分的代码都是“过程”,使用与c类似的语法风格。这对于熟悉c语言的开发者来说,可以大大提高开发速度。例如使用了printf函数,这个函数就和c的pritnf语法一样。其实在perl中还有其他的与printf语法一样的函数,我们可以自由选择。

开发工具


linux开发程序一般来说有两个主要的编辑器,vi或者vim,还有emacs。这些文本编辑器如何选择?你可以都试试看,然后选择自己喜欢的。反正它们都可以让你输入文本,都可以让你生成源代码。我个人来说,喜欢vim,简洁而好用。

gcc是gnu c compiler的缩写,是gnu/linux下最好的编译器之一。这个编译器稳定,而且文档齐全,大部分的自由软件都是用他编译的。如果你使用c,那么就可以选择gcc。

gnu的debugger工具是gdb,有非常好的调试特性。不过对于初学者来说也许使用起来有点麻烦,你可以先试试ddd来调试你的代码。

还有其他的有自己ide的开发语言,但是我还是建议你用vim或者emacs来开始你的编写代码的过程。


总结

linux是开发者的天堂。开发者可以控制特定的硬件环境,还可以创建自己的工具使得自己的生活更加轻松。通常来说,学习编程的最好的方法就是读别人的代码,然后看它们是怎么做的。linux和开发辕马使得你有机会看到别人的辕马,好好去学习吧。


一些资源

http://perl.com
http://python.org
http://www.ruby-lang.org/en/
http://www.cpan.org/
http://www.php.org   


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons