2011年10月26日 星期三

Perl 使用constant注意不能expand常數

As with all use directives, defining a constant happens at compile time. Thus, it's probably not correct to put a constant declaration inside of a conditional statement (like if ($foo) { use constant ... } ).
Constants defined using this module cannot be interpolated into strings like variables. However, concatenation works just fine:
  1. print "Pi equals PI...\n"; # WRONG: does not expand "PI"
  2. print "Pi equals ".PI."...\n"; # right

Perl 中文教學 (朝陽科技大學)


 Perl 教學: 啟始


這是Nik Silver於School of Computer StudiesUniversity of Leeds教授Perl程式的內容,原始設計為一天的教學實習內容。 我將其翻譯並作小幅的修改使內容能符合朝陽科技大學學生個人網頁設計的目的。
教學內容架構如下:

2011年10月21日 星期五

PERL :$\控制output輸出加於行尾的文字; $/控制input輸入結束判斷符號

{


local $/ = undef;
#平常輸入Enter就將input資料結束,若設定成undef則可以進行多行輸入
open FH,"testfile.txt";
while(<FH>){
  print $_."[END]";#將資料讀成一行,穿吃給第一個iteration的$_
}
close FH;
}

#輸出結果:
hello world 
I am stayhigh 
Want Be an Hacker in Perl 

line break

here end
[END]

Perl open file ,filehandle technique


while ($line = <CHECKBOOK>) {
  print $line;
}

Opening files

Opening a file in perl in straightforward:open FILE, "filename.txt" or die $!;The command above will associate the FILE filehandle with the file filename.txt. You can use the filehandle to read from the file. If the file doesn't exist - or you cannot read it for any other reason - then the script will die with the appropriate error message stored in the $! variable.
What if you wanted to modify the file instead of just reading from it? Then you'd have to specify the appropriate mode using the three-argument form of open.
open FILEHANDLE, MODE, EXPRThe available modes are the following:
modeoperandcreatetruncate
read<
write>
append>>

Each of the above modes can also be prefixed with the + character to allow for simultaneous reading and writing.
modeoperandcreatetruncate
read/write+<
read/write+>
read/append+>>

Callability of procedures


A more useful concept is the converse of the static scope of the name of a procedure, which we refer to as the callability of the procedure. Callability of a procedure determines the procedures that it can call.


#NOTE:此處說明Pascal的函數呼叫是否可行
Algorithm to draw the static tree of a Pascal program:
1.  Draw the main() program as the root node of the tree.
2.  Identify all the procedures/functions directly defined in main()Draw them as child nodes of main() node.
3.  Repeatedly carry out step 2 for all the leaf nodes of the tree, until there are no more nested procedures in the program.
In the Pascal program presented earlier, the procedures read()compute() and print() are defined directly inmain(), and are its children. The procedure prompt() is defined within read(), and is its child. The procedures initialize() and transform() are defined within compute()and are its children. The procedure defaults() is defined within initialize(), and is its child. The static tree of the program is shown below.


清楚講解 變數scope的網站

http://phobos.ramapo.edu/~amruth/grants/problets/courseware/scope/



static ancestors,static parents
static non-local referencing environment :
of a procedure p() is the set of all the unique variables declared in the static ancestors of p(), that have not been re-declared in the procedure p().


dynamic ancestors
The dynamic non-local referencing environment :
of a procedure p() is the set of all the unique variables declared in the dynamic ancestors of p(), that have not been re-declared in the procedure p().