1 / 14

COMP519: Web Programming Autumn 2007

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

amil
Télécharger la présentation

COMP519: Web Programming Autumn 2007

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP519: Web ProgrammingAutumn 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 = $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 • عبارات ریاضی در 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 returnit • $a++;# Return $a and then increment it • --$a;# Decrement $a and then return it • $a--;# Return $a and then decrement it • دقت کنید که عبارت $a = $bباعث می شود که ابتدا یک کپی از $bدرست شود و سپس به $a اختصاص داده می شود. لذا تغییرات بعدی $bروی $aتاثیری ندارد. • برای دیدن بقیه عملگر ها دستور man perlop را اجرا کنید.

  7. Interpolation #!/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”; • دقت کنید دستور print '$a and $b';دقیقا خود رشته $a and $b را چاپ می کند. • برای چاپ عبارت apples and pears از دستور print "$a and $b";استفاده کنید. • علامت " باعث می شود که متغییرها و عباراتی مثل \nو\t تفسیر شوند. 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 • در جدول زیر تعدادی از توابع که برای کار کردن با رسته ها استفاده می شوند آورده شده اند:

  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 برای اضافه کردن به آخر آرایه از push استفاده کنید. • @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 برای حذف کردن از آخر آرایه از pop استفاده کنید. @food = ("apples", "pears", "eels"); $grub = pop(@food);# Now $grub = "eels“ shift و unshiftبصورت مشابه کار می کنند اما روی ابتدای لیست عمل می کنند.

  13. Array Assignments انتساب آرایه به یک متغییر: $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. می توان دستورات انتساب چندگانه داشت: ($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)

  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 apples pears eels 3

More Related