docs:دليل_المبتدئين_إلى_بيرل
اختلافات
عرض الاختلافات بين النسخة المختارة و النسخة الحالية من الصفحة.
جانبي المراجعة السابقةالمراجعة السابقةالمراجعة التالية | المراجعة السابقة | ||
docs:دليل_المبتدئين_إلى_بيرل [2008/05/07 18:57] – alsadi | docs:دليل_المبتدئين_إلى_بيرل [2015/04/23 03:19] (حالي) – تحرير خارجي 127.0.0.1 | ||
---|---|---|---|
سطر 1: | سطر 1: | ||
+ | {{tag> | ||
+ | ~~ODT~~ | ||
+ | ====== دليل المبتدئين إلى بيرل ====== | ||
+ | ===== حول هذه الوثيقة ===== | ||
+ | * تأليف: Doug Sheppard | ||
+ | * http:// | ||
+ | ===== أولاً بعض التسويق ===== | ||
+ | |||
+ | Welcome to Perl. | ||
+ | |||
+ | Perl is the Swiss Army chainsaw of scripting languages: powerful and adaptable. It was first developed by Larry Wall, a linguist working as a systems administrator for NASA in the late 1980s, as a way to make report processing easier. Since then, it has moved into a large number of roles: automating system administration, | ||
+ | |||
+ | Why did Perl become so popular when the Web came along? Two reasons: First, most of what is being done on the Web happens with text, and is best done with a language that's designed for text processing. More importantly, | ||
+ | |||
+ | It also didn't hurt that Perl is a friendly language. It plays well with your personal programming style. The Perl slogan is " | ||
+ | |||
+ | In this first part of our series, you'll learn a few basics about Perl and see a small sample program. | ||
+ | |||
+ | ===== كلمة تخص نظم التشغيل ===== | ||
+ | |||
+ | |||
+ | في هذه السلسلة, | ||
+ | |||
+ | |||
+ | |||
+ | ===== برنامجك الأول في بيرل ===== | ||
+ | |||
+ | |||
+ | أكتب السطر البرمجي التالي و ضعه في ملف سمه first.pl | ||
+ | <code perl> | ||
+ | # | ||
+ | print "Hi there!\n"; | ||
+ | </ | ||
+ | |||
+ | (حسب التقاليد, | ||
+ | |||
+ | الأن, شغله بمفسر بيرل (Perl interpreter) الخاص بك. من سطر اﻷوامر, | ||
+ | |||
+ | < | ||
+ | Hi there! | ||
+ | </ | ||
+ | \n تشير إلى " | ||
+ | |||
+ | |||
+ | ===== الدوال و الجمل ===== | ||
+ | |||
+ | |||
+ | بيرل بها مكتبة غنية بالدوال. | ||
+ | |||
+ | Perl has a rich library of functions. They' | ||
+ | |||
+ | دالة الطباعة print function هي احد الاجزاء متكررة الاستخدام في بيرل. تستخدمها لعرض ما تريد على الشاشة أو لارسال معلومات لملف (و سنناقش هذا في المقالة القادمة). و هي تأخد مجموعة من العناصر للمخرجات كوسائط للدالة. | ||
+ | |||
+ | The print function is one of the most frequently used parts of Perl. You use it to display things on the screen or to send information to a file (which we'll discuss in the next article). It takes a list of things to output as its parameters. | ||
+ | <code perl> | ||
+ | print "This is a single statement."; | ||
+ | print "Look, ", "a ", " | ||
+ | </ | ||
+ | برنامج البيرل يحتوي على جمل, كل منها تنتهي بفاصلة منقوطة semicolon. الجمل لا يجب أن تكون في سطر منفصل; يمكن أن توجد عدة جمل في سطر واحد أو أن توزع جملة واحدة | ||
+ | <code perl> | ||
+ | print "This is "; print "two statements.\n"; | ||
+ | "is only one statement.\n"; | ||
+ | </ | ||
+ | |||
+ | ===== الأرقام و السلاسل و علامات الإقتباس ===== | ||
+ | |||
+ | |||
+ | يوجد نوعين أساسيين من البيانات في بيرل: اﻷرقام و السلاسل strings. | ||
+ | |||
+ | التعامل مع الأرقام سهل, كلنا تعاملنا معهم. الشئ الوحيد الذي يجب عليك معرفته, | ||
+ | |||
+ | السلاسل أعقد قليلا. السلاسل هي مجموعة من الحروف داخل علامتي إقتباس مفردتين أو مزدوجتين: | ||
+ | |||
+ | <code perl> | ||
+ | ' | ||
+ | " | ||
+ | </ | ||
+ | |||
+ | الفرق بين علامات الإقتباس المفردة و علامات الإقتباس المزدوجة هو أن علامات الإقتباس المفردة تعني وجود محتوى يجب أن يؤخد كنص بينما علامات الإقباس المزدوجة تعني وجود محتوى يجب أن يفسر interpreted. | ||
+ | على سبيل المثال, | ||
+ | |||
+ | |||
+ | The difference between single quotes and double quotes is that single quotes mean that their contents should be taken literally, while double quotes mean that their contents should be interpreted. For example, the character sequence \n is a newline character when it appears in a string with double quotes, but is literally the two characters, backslash and n, when it appears in single quotes. | ||
+ | |||
+ | <code perl> | ||
+ | print "This string\nshows up on two lines."; | ||
+ | print 'This string \n shows up on only one.'; | ||
+ | </ | ||
+ | |||
+ | (Two other useful backslash sequences are \t to insert a tab character, and \\ to insert a backslash into a double-quoted string.) | ||
+ | |||
+ | ===== Variables ===== | ||
+ | |||
+ | |||
+ | If functions are Perl's verbs, then variables are its nouns. Perl has three types of variables: scalars, arrays and hashes. Think of them as " | ||
+ | |||
+ | Scalars are single things. This might be a number or a string. The name of a scalar begins with a dollar sign, such as $i or $abacus. You assign a value to a scalar by telling Perl what it equals, like so: | ||
+ | |||
+ | <code perl> | ||
+ | $i = 5; | ||
+ | $pie_flavor = ' | ||
+ | $constitution1776 = "We the People, etc."; | ||
+ | </ | ||
+ | |||
+ | You don't need to specify whether a scalar is a number or a string. It doesn' | ||
+ | |||
+ | If you use a double-quoted string, Perl will insert the value of any scalar variables you name in the string. This is often used to fill in strings on the fly: | ||
+ | |||
+ | <code perl> | ||
+ | $apple_count = 5; | ||
+ | $count_report = "There are $apple_count apples."; | ||
+ | print "The report is: $count_report\n"; | ||
+ | </ | ||
+ | |||
+ | The final output from this code is The report is: There are 5 apples.. | ||
+ | |||
+ | Numbers in Perl can be manipulated with the usual mathematical operations: addition, multiplication, | ||
+ | |||
+ | <code perl> | ||
+ | $a = 5; | ||
+ | $b = $a + 10; # $b is now equal to 15. | ||
+ | $c = $b * 10; # $c is now equal to 150. | ||
+ | $a = $a - 1; # $a is now 4, and algebra teachers are cringing. | ||
+ | </ | ||
+ | |||
+ | You can also use special operators like ++, --, +=, -=, /= and *=. These manipulate a scalar' | ||
+ | |||
+ | <code perl> | ||
+ | $a = 5; | ||
+ | | ||
+ | $a += 10; # Now it's 16; we added 10. | ||
+ | $a /= 2; # And divided it by 2, so it's 8. | ||
+ | </ | ||
+ | |||
+ | Strings in Perl don't have quite as much flexibility. About the only basic operator that you can use on strings is concatenation, | ||
+ | |||
+ | <code perl> | ||
+ | $a = " | ||
+ | $b = $a + " | ||
+ | $c = $a . " | ||
+ | </ | ||
+ | |||
+ | Remember that Perl converts strings to numbers transparently whenever it's needed, so to get the value of $b, the Perl interpreter converted the two strings " | ||
+ | |||
+ | Just remember, the plus sign adds numbers and the period puts strings together. | ||
+ | |||
+ | Arrays are lists of scalars. Array names begin with @. You define arrays by listing their contents in parentheses, | ||
+ | |||
+ | <code perl> | ||
+ | @lotto_numbers = (1, 2, 3, 4, 5, 6); # Hey, it could happen. | ||
+ | @months = (" | ||
+ | </ | ||
+ | |||
+ | The contents of an array are indexed beginning with 0. (Why not 1? Because. It's a computer thing.) To retrieve the elements of an array, you replace the @ sign with a $ sign, and follow that with the index position of the element you want. (It begins with a dollar sign because you're getting a scalar value.) You can also modify it in place, just like any other scalar. | ||
+ | |||
+ | <code perl> | ||
+ | @months = (" | ||
+ | print $months[0]; | ||
+ | $months[2] = " | ||
+ | </ | ||
+ | If an array doesn' | ||
+ | |||
+ | <code perl> | ||
+ | $winter_months[0] = " | ||
+ | </ | ||
+ | |||
+ | Arrays always return their contents in the same order; if you go through @months from beginning to end, no matter how many times you do it, you'll get back July, August and September in that order. If you want to find the length of an array, use the value $# | ||
+ | |||
+ | <code perl> | ||
+ | @months = (" | ||
+ | print $# | ||
+ | $a1 = $# | ||
+ | $#months = 0; # Now @months only contains " | ||
+ | </ | ||
+ | |||
+ | Hashes are called ``dictionaries'' | ||
+ | |||
+ | <code perl> | ||
+ | %days_in_month = ( " | ||
+ | </ | ||
+ | |||
+ | You can fetch any value from a hash by referring to $hashname{key}, | ||
+ | |||
+ | <code perl> | ||
+ | print $days_in_month{" | ||
+ | $days_in_month{" | ||
+ | </ | ||
+ | |||
+ | If you want to see what keys are in a hash, you can use the keys function with the name of the hash. This returns a list containing all of the keys in the hash. The list isn't always in the same order, though; while we could count on @months to always return July, August, September in that order, keys %days_in_summer might return them in any order whatsoever. | ||
+ | |||
+ | <code perl> | ||
+ | @month_list = keys %days_in_summer; | ||
+ | # @month_list is now (' | ||
+ | </ | ||
+ | |||
+ | The three types of variables have three separate namespaces. That means that $abacus and @abacus are two different variables, and $abacus[0] (the first element of @abacus) is not the same as $abacus{0} (the value in abacus that has the key 0). | ||
+ | |||
+ | ===== التعليقات ===== | ||
+ | |||
+ | لاحظ, في بعض نماذج الأكواد السابقة في القسم السابق, | ||
+ | |||
+ | Notice that in some of the code samples from the previous section, I've used code comments. These are useful for explaining what a particular piece of code does, and vital for any piece of code you plan to modify, enhance, fix, or just look at again. (That is to say, comments are vital for all code.) | ||
+ | |||
+ | أي شئ في سطر من كود بيرل يتبع إشارة # فهو تعليق. (بالتأكيد, | ||
+ | |||
+ | <code perl> | ||
+ | print "Hello world!\n"; | ||
+ | # This entire line is a comment. | ||
+ | </ | ||
+ | |||
+ | ===== الحلقات ===== | ||
+ | |||
+ | |||
+ | غالبا في كل مرة تكتب فيها برنامج, | ||
+ | |||
+ | |||
+ | Perl has several different functions that are useful for flow control, the most basic of which is for. When you use the for function, you specify a variable that will be used for the loop index, and a list of values to loop over. Inside a pair of curly brackets, you put any code you want to run during the loop: | ||
+ | |||
+ | <code perl> | ||
+ | for $i (1, 2, 3, 4, 5) { | ||
+ | print " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | This loop prints the numbers 1 through 5, each on a separate line. | ||
+ | |||
+ | A handy shortcut for defining loops is using .. to specify a range of numbers. You can write (1, 2, 3, 4, 5) as (1 .. 5). You can also use arrays and scalars in your loop list. Try this code and see what happens: | ||
+ | |||
+ | <code perl> | ||
+ | @one_to_ten = (1 .. 10); | ||
+ | $top_limit = 25; | ||
+ | for $i (@one_to_ten, | ||
+ | print " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The items in your loop list don't have to be numbers; you can use strings just as easily. If the hash %month_has contains names of months and the number of days in each month, you can use the keys function to step through them. | ||
+ | |||
+ | <code perl> | ||
+ | for $i (keys %month_has) { | ||
+ | print "$i has $month_has{$i} days.\n"; | ||
+ | } | ||
+ | |||
+ | for $marx (' | ||
+ | print "$marx is my favorite Marx brother.\n"; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== The Miracle of Compound Interest ===== | ||
+ | |||
+ | |||
+ | You now know enough about Perl - variables, print, and for() - to write a small, useful program. Everyone loves money, so the first sample program is a compound-interest calculator. It will print a (somewhat) nicely formatted table showing the value of an investment over a number of years. (You can see the program at compound_interest.pl) | ||
+ | |||
+ | The single most complex line in the program is this one: | ||
+ | |||
+ | <code perl> | ||
+ | $interest = int (($apr / 100) * $nest_egg * 100) / 100; | ||
+ | </ | ||
+ | |||
+ | $apr / 100 is the interest rate, and ($apr / 100) * $nest_egg is the amount of interest earned in one year. This line uses the int() function, which returns the integer value of a scalar (its value after any fractional part has been stripped off). We use int() here because when you multiply, for example, 10925 by 9.25%, the result is 1010.5625, which we must round off to 1010.56. To do this, we multiply by 100, yielding 101056.25, use int() to throw away the leftover fraction, yielding 101056, and then divide by 100 again, so that the final result is 1010.56. Try stepping through this statement yourself to see just how we end up with the correct result, rounded to cents. | ||
+ | |||
+ | ===== Play Around! ===== | ||
+ | |||
+ | |||
+ | At this point you have some basic knowledge of Perl syntax and a few simple toys to play with - print, for(), keys(), and int(). Try writing some simple programs with them. Here are two suggestions, | ||
+ | |||
+ | * A word frequency counter. How often does each word show up in an array of words? Print out a report. (Hint: Use a hash to count of the number of appearances of each word.) | ||
+ | |||
+ | * Given a month and the day of the week that's the first of that month, print a calendar for the month. (Remember, you need \n to go to a new line.) |