Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP519: Web Programming Autumn 2007 Perl Tutorial: The very beginning A basic Perl Program The first line Comments and statements Simple printing Running.

Similar presentations


Presentation on theme: "COMP519: Web Programming Autumn 2007 Perl Tutorial: The very beginning A basic Perl Program The first line Comments and statements Simple printing Running."— Presentation transcript:

1 COMP519: Web Programming Autumn 2007 Perl Tutorial: The very beginning A basic Perl Program The first line Comments and statements Simple printing Running the program Scalar Variables Operations and assignment Interpolation Exercise Array variables Array assignments Displaying arrays Exercise

2 Perl زبان برنامه نویسی Perl در سال 1980 توسط لاری وال ابداع شد. قرار است Perl شکاف موجود بین زبانهای سطح بالا و سطح پایین را پر نماید. Perl مخصوصا جهت پردازش رشته ها و داده ای متنی بسیار مناسب است. جهت پردازش متون استفاده از عبارات منظم (Regular expressions) دارای انعطاف زیادی است. می توان از Perl جهت برنامه نویسی CGI استفاده کرد.

3 A Basic Program هر برنامه زبان Perl با خط #!/usr/local/bin/perlشروع می گردد ( یا #!/usr/bin/perl بسته به تنظیمات سرور محلی). به این کار shebang می گویند. برای اضافه کردن توضیحات تک خطی از # استفاده می شود. هر عبارت زبان Perl با ; خاتمه می یابد. تابع print جهت چاپ کردن خروجی استفاده می شود. #!/usr/local/bin/perl # basic01.pl COMP519 # Program to do the obvious # print 'Hello world.'; # Print a message در اینجا یک برنامه ساده را جهت شروع مشاهده می کنید:

4 Running the Program برای اجرای Perl از Linux استفاده کنید. از یک ویرایشگر متن استفاده نمایید. فایل را اجرایی کنید.  دستور chmod u+x progname را اجرا کنید. برای اجرای برنامه دستور perl progname یا./progname را در خط فرمان تایپ کنید. برای فعال کردن اخطارها از دستور perl -w progname استفاده کنید. برای فعال کردن debugger از دستور perl -d progname استفاده کنید. bash-2.05b$ perl basic01.pl Hello world.bash-2.05b$

5 Scalar Variables: Declaration متغییر با علامت $ شروع می شوند و می توانند از رشته ها و اعداد نگهداری کنند. نوع یک متغییر وابسته به داده ای است که نگهداری میکند و در حین اجرا قابل تغییر است. $priority = 9; $priority = 'high'; $priority = “high”; Perl اعداد را بصورت رشته نیز می پذیرد. $priority = '9'; $default = '0009'; اسم متغییر ها می تواند شامل اعداد و حروف و _ باشد اما نباید با عدد شروع شود. Perl به بزرگی و کوچکی حروف حساس است. پس $a و $A متفاوت هستند. $_ یک متغییر مخصوص است که بعدا در مورد آن بیشتر بحث خواهیم کرد.

6 Operations and Assignment عبارات ریاضی در Perl : $a = 1 + 2; # Add 1 and 2 and store in $a $a = 3 - 4; # Subtract 4 from 3 and store in $a $a = 5 * 6; # Multiply 5 and 6 $a = 7 / 8; # Divide 7 by 8 to give 0.875 $a = 9 ** 10; # Nine to the power of 10 $a = 5 % 2; # Remainder of 5 divided by 2 ++$a; # Increment $a and then return it $a++; # Return $a and then increment it --$a; # Decrement $a and then return it $a--; # Return $a and then decrement it رشته ها در Perl : $a = $b. $c; # Concatenate $b and $c $a = $b x $c; # $b repeated $c times انتساب در Perl : $a = $b; # Assign $b to $a $a += $b; # Add $b to $a $a -= $b; # Subtract $b from $a $a.= $b; # Append $b onto $a دقت کنید که عبارت $a = $b باعث می شود که ابتدا یک کپی از $b درست شود و سپس به $a اختصاص داده می شود. لذا تغییرات بعدی $b روی $a تاثیری ندارد. برای دیدن بقیه عملگر ها دستور man perlop را اجرا کنید.

7 Interpolation دقت کنید دستور print '$a and $b'; دقیقا خود رشته $a and $b را چاپ می کند. برای چاپ عبارت apples and pears از دستور print "$a and $b"; استفاده کنید. علامت " باعث می شود که متغییرها و عباراتی مثل \n و \t تفسیر شوند. #!/usr/local/bin/perl # basic02.pl COMP519 $a = 'apples'; $b = 'pears'; print $a.' and '.$b; #!/usr/local/bin/perl # basic03.pl COMP519 $a = 'apples'; $b = 'pears'; print “$a and $b”; bash-2.05b$ perl basic02.pl apples and pearsbash-2.05b$ bash-2.05b$ perl basic02.pl apples and pearsbash-2.05b$

8 Exercise در این برنامه عبارت Hello world. به یک متغییر انتساب و سپس به همراه خط جدید چاپ می شود. #!/usr/local/bin/perl # basic04.pl COMP519 $message = 'Hello world.'; print “$message \n”; # Print a message bash-2.05b$ perl basic04.pl Hello world. bash-2.05b$

9 Example #!/usr/local/bin/perl # basic05.pl COMP519 print '007',' has been portrayed by at least ', 004, ' actors. '; print "\n"; print 7+3, ' ', 7*3, ' ', 7/3, ' ', 7%3, ' ', 7**3, ' '; print "\n"; $x = 7; print $x; print "\n"; print ' Doesn\'t resolve variables like $x and backslashes \n. '; print "\n"; print " Does resolve $x and backslash\n"; $y = "A line containing $x and ending with line feed.\n"; print $y; $y = "Con". "cat". "enation!\n"; print $y; bash-2.05b$ perl basic05.pl 007 has been portrayed by at least 4 actors. 10 21 2.33333333333333 1 343 7 Doesn't resolve variables like $x and backslashes \n. Does resolve 7 and backslash A line containing 7 and ending with line feed. Concatenation! bash-2.05b$

10 String Functions اگر cube اسم یک تابع باشد آنرا به دو صورت می شود صدا زد: cube(x) یا cube x در جدول زیر تعدادی از توابع که برای کار کردن با رسته ها استفاده می شوند آورده شده اند : NameParametersResult chomp(a string)the string w/terminating newline characters removed length(a string)the number of characters in the string lc(a string)the string with uppercase letters converted to lower uc(a string)the string with lowercase letters converted to upper hex(a string)the decimal value of the hexadecimal number in the string join(a char. and a list of strings) the strings concatenated together with the character inserted between them q(a string)places single quotes around the string qq(a string)places double quotes around the string

11 Array Variables اسم هر آرایه از متغییرها با @ شروع می شود. دقت کنید که در کد زیر به جای @ از $ استفاده کرده ایم چون می خواستیم که به یک عنصر از آرایه دسترسی پیدا کنیم. Perl فقط از آرایه های تک بعدی پشتیبانی می کند. (برای آرایه های چند بعدی از ارجاع استفاده می شود) @food = ("apples", "pears", "eels"); # a three element list @music = ("whistle", "flute"); # a two element list $food[2] # returns eels. @music = ("whistle", "flute"); @moremusic = ("organ", @music, "harp"); # combines both @moremusic = ("organ", "whistle", "flute", "harp"); # is the same @moremusic = qq(organ whistle flute harp); # is the same

12 Push and Pop Functions @food = ("apples", "pears", "eels"); push(@food, "eggs"); # pushes eggs onto the end of @food push(@food, "eggs", "lard"); # pushes two items push(@food, ("eggs", "lard")); # pushes two items push(@food, @morefood); # pushes the items of @morefood $length= push(@food, @morefood);# returns the length of the new list برای اضافه کردن به آخر آرایه از push استفاده کنید. برای حذف کردن از آخر آرایه از pop استفاده کنید. @food = ("apples", "pears", "eels"); $grub = pop(@food); # Now $grub = "eels“ shift و unshift بصورت مشابه کار می کنند اما روی ابتدای لیست عمل می کنند.

13 Array Assignments ($a, $b) = ($c, $d); # Same as $a=$c; $b=$d; ($a, $b) = @food; # $a and $b are the first two items of @food ($a, @somefood) = @food; # $a is the first item of @food and # @somefood is a list of the others (@somefood, $a) = @food; # @somefood is @food and # $a is undefined, that form is best avoided ($a, $b) = ($b, $a); # swaps the values of $a and $b (since # Perl creates the whole list before the # assignment statement) $f= $#food; # assigns the index of the last element of @food array $f = @food; # assigns the length of @food $f = "@food"; # turns the list into a string with a space between each element. # This space can be replaced by any other string # by changing the value of the special (built-in) $" variable. می توان دستورات انتساب چندگانه داشت: انتساب آرایه به یک متغییر:

14 Displaying Arrays #!/usr/local/bin/perl # basic06.pl COMP519 @food = ("apples", "pears", "eels"); print @food; # By itself print "\n"; print "@food"; # Embedded in double quotes print "\n"; $f=$food; print "$f \n"; # In a scalar context bash-2.05b$ perl basic06.pl applespearseels 3


Download ppt "COMP519: Web Programming Autumn 2007 Perl Tutorial: The very beginning A basic Perl Program The first line Comments and statements Simple printing Running."

Similar presentations


Ads by Google