Perl Cd Bookshelf [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Perl Cd Bookshelf [Electronic resources] - نسخه متنی

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید










31.16. use overload


In the Number module:


package Number;
use overload "+" => \&myadd,
"-" => \&mysub,
"*=" => "multiply_by";


In your program:

use Number;
$a = new Number 57;
$b = $a + 5;


The built-in operators work well on strings and numbers, but make
little sense when applied to object references (since, unlike C or C++,
Perl doesn''t allow pointer arithmetic). The overload pragma lets
you redefine the meanings of these built-in operations when applied to
objects of your own design. In the previous example, the call to the
pragma redefines three operations on Number objects: addition will
call the Number::myadd function, subtraction will call the
Number::mysub function, and the multiplicative assignment operator
will call the multiply_by method in class Number (or one of its base
classes). We say of these operators that they are now overloaded
because they have additional meanings overlaid on them (and not because
they have too many meanings--though that may also be the case).

For much more on overloading, see Chapter 13, "Overloading".






/ 875