Skip to Content
Programming Perl, 3rd Edition
book

Programming Perl, 3rd Edition

by Larry Wall, Tom Christiansen, Jon Orwant
July 2000
Intermediate to advanced
1104 pages
35h 1m
English
O'Reilly Media, Inc.
Content preview from Programming Perl, 3rd Edition

Overloadable Operators

You can only overload certain operators, which are shown in Table 13.1. The operators are also listed in the %overload::ops hash made available when you use overload, though the categorization is a little different there.

Table 13-1. Overloadable Operators

CategoryOperators
Conversion"" 0+ bool
Arithmetic+ - * / % ** x . neg
Logical!
Bitwise& | ~ ^ ! << >>
Assignment+= -= *= /= %= **= x= .= <<= >>= ++ --
Comparison== < <= > >= != <=> lt le gt ge eq ne cmp
Mathematicalatan2 cos sin exp abs log sqrt
Iterative<>
Dereference${} @{} %{} &{} *{}
Pseudonomethod fallback =>

Note that neg, bool, nomethod, and fallback are not actual Perl operators. The five dereferencers, "", and 0+ probably don't seem like operators either. Nevertheless, they are all valid keys for the parameter list you provide to use overload. This is not really a problem. We'll let you in on a little secret: it's a bit of a fib to say that the overload pragma overloads operators. It overloads the underlying operations, whether invoked explicitly via their "official" operators, or implicitly via some related operator. (The pseudo-operators we mentioned can only be invoked implicitly.) In other words, overloading happens not at the syntactic level, but at the semantic level. The point is not to look good. The point is to do the right thing. Feel free to generalize.

Note also that = does not overload Perl's assignment operator, as you might expect. That would not do the right thing. More on that later.

We'll ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Perl, 4th Edition

Programming Perl, 4th Edition

Tom Christiansen, brian d foy, Larry Wall, Jon Orwant
Learning Perl, 7th Edition

Learning Perl, 7th Edition

Randal L. Schwartz, brian d foy, Tom Phoenix
Programming the Perl DBI

Programming the Perl DBI

Tim Bunce, Alligator Descartes

Publisher Resources

ISBN: 0596000278Supplemental ContentErrata