Category Archives: php perl ruby

bad interpreter: No such file or directory

在运行perl脚本的时候, 明明在文件头加了#!/usr/bin/perl, 且这个路径也是对的, 可./test.pl运行之后, 还是会有

./test.pl: /usr/bin/perl^M: bad interpreter: No such file or directory 的错误

上网找到了原因, 原来是文件的编码不对, 解决方法如下(转:http://blog.csdn.net/luckqt/archive/2008/12/25/3605138.aspx):

sh脚本异常:bad interpreter: No such file or directory

Linux中执行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory

分析:这是不同系统编码格式引起:在windows系统中编辑的.sh文件可能有不可见字符,所以在Linux系统下执行会报以上异常信息。
解决:1)在windows下转换:
利用一些编辑器如UltraEdit或EditPlus等工具先将脚本编码转换,再放到Linux中执行。转换方式如下(UltraEdit):File–>Conversions–>DOS->UNIX即可。
2)也可在Linux中转换:
首先要确保文件有可执行权限
#sh>chmod a+x filename

然后修改文件格式
#sh>vi filename

利用如下命令查看文件格式
:set ff 或 :set fileformat

可以看到如下信息
fileformat=dos 或 fileformat=unix

利用如下命令修改文件格式
:set ff=unix 或 :set fileformat=unix

:wq (存盘退出)

最后再执行文件
#sh>./filename

perl 符号

( 创建简单数组
[ 创建匿名数组
{ 创建hash数组(关联数组)
$ 声明变量
@ 声明数组
% 声明hash数组(关联数组)
\ 获得指针(或引用或变量地址)
$$从指针(或引用或变量地址)得到变量
@$从指针(或引用或变量地址)得到数组
%$从指针(或引用或变量地址)得到hash数组
->从指针(或引用或变量地址)得到变量的值
::引用其它包的变量(‘ 单引号也可以)
&调用函数(在调用之后定义的函数)

perl多维数组

Perl语言应该记住的最重要的一点是:Perl中的数组和哈希表始终是一维的。因此,数组和哈希表只保存标量值,不直接存贮数组或其它的复杂数据结构。数组的成员要么是数(或字符串)要么是引用

  • 中括号可以创建匿名数组的引用,所以创建多维数组可以这样来创建:如

$line = [‘solid’ , ‘black’ , [‘1′,’2′,’3’] , [‘4′,’5′,’6’]];

$line->[0] = solid
$line->[1] = black
$line->[2][0] = 1
$line->[3][1] = 5

  • 大括号可以创建匿名 hash表的引用,所以创建多维数组还可以这样来创建

$map = {“1” =>
 {
   “1-0” => “1-0”,
   “1-1” => “1-1”
  },
  “2” =>
  {
    “2-0” => “2-0”,
   “2-1” => “2-1”
  } 
};

该hash表可以这样来使用

$map->{“2”}->{“2-1”}

perl my和local

在perl子函数中,可以直接引用和修改全局变量,如:

$var = “global”;

sub foo() {
print $var;
$var .= ” extend”;
}

foo();
print(“\n”);
print($var);

输出:

global
global extend
可以看出,子函数可以引用和修改全局变量,当你不确定有哪些全局变量,且定义了与全局变量同名的变量时,这样就造成了冲突,这时可以通过perl里面的my和local关键字来解决,它们俩都是用来定义局部变量的,但有区别,local定义的局部变量的生命周期可以延续到当前子函数所调用的其它子函数里,而my则没有这个特性,举例说明:

$var = “global”;

sub foo() {
local $var = “assigned by foo sub”;
foo2();
}
sub foo2() {
print($var);
print(“\n”);
}

foo();
print($var);

输出:

assigned by foo sub
global
————————————————————————

$var = “global”;

sub foo() {
my $var = “assigned by foo sub”;
foo2();
}
sub foo2() {
print($var);
print(“\n”);
}

foo();
print($var);

输出:

global
global

20 Great PHP Libraries You Need to Know

You’re in the middle of a project, and need to get everything done as soon as possible. To keep the deadline and still have a life, you would better to collect this awesome library on your fingertip. By knowing this library capability, then you’ll know when to use any of them.

Charting Library

You can create simple graph or chart using GD library on PHP, but to help you create more complex chart, then you’ll need this awesome library.

  1. pChart – a PHP class to build charts.
  2. Libchart – Simple PHP chart drawing library.
  3. JpGraph – Object-oriented graph creating library for PHP.
  4. Open Flash Chart – Flash based charting library.

RSS Parser Library

Parsing a RSS is not a fun thing to do, so you would better put this library and get everything done.

  1. MagpieRSS – RSS for PHP.
  2. SimplePie – Super-fast, easy-to-use, RSS and Atom feed parsing in PHP.

Thumbnail Generator

Just another way to create thumbnail.

  1. phpThumb – The PHP thumbnail creator.

Payment

Dealing with an e-commerce site? Need payment solution? Don’t worry. Let this library help you.

  1. PHP Payment Library – PHP Payment Library for Paypal, Authorize.net and 2Checkout (2CO)

OpenID

Just in case you need to implement OpenID to your project.

  1. PHP-OpenID – OpenID implementation in PHP.

Database Abstraction/ORM

Writing SQL Query isn’t really exciting. These library let you smile more.

  1. ADOdb – Database abstraction library for PHP.
  2. Doctrine – Object relational mapper (ORM) for PHP 5.2.3+ that sits on top of a powerful database abstraction layer (DBAL).
  3. Propel – Object-Relational Mapping framework for PHP5
  4. Outlet – an Open source object-to-relational mapping tool for PHP.

PDF Generator

  1. FPDF – PHP class which allows to generate PDF files with pure PHP.

Excel

Need to generate your report in Excel file. Well, you better have this library on your side.

  1. php-excel – Very simple library for generating excel documents from php on-the-fly.
  2. PHP Excel Reader – Parse and retrieve information from XLS files.

E-Mail

Don’t like PHP mail function? Put this library on your project, and you have more features when dealing with email.

  1. Swift Mailer – Free Feature-rich PHP Mailer.
  2. PHPMailer – Powerful email transport class with a big features and small footprint.

Unit Testing

For Test-driven development lover, these library will help you get your heart at home.

  1. SimpleTest – PHP unit test and web test framework.
  2. PHPUnit – Member of the xUnit family of testing frameworks and provides both a framework that makes the writing of tests easy as well as the functionality to easily run the tests and analyse their results.

Quote:http://komunitasweb.com/2009/03/20-great-php-library-you-need-to-know/