Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re^2: Hints Towards Writing a Module with New Operators

by JadeNB (Chaplain)
on Dec 20, 2009 at 03:46 UTC ( #813582=note: print w/replies, xml ) Need Help??

in reply to Re: Hints Towards Writing a Module with New Operators
in thread Hints Towards Writing a Module with New Operators

I discovered what appears to be a fact that logical operators like .EQV. and .NEQV. which are non-Perl can not be overloaded and that there are restrictions on what can be overloaded within Perl itself

This is basically correct, although the wording is strange. Overloading is something applied to objects, not operators; it changes how an object behaves when an operator is applied to it, without changing the default behaviour of that operator.

UPDATE: Perhaps the confusion comes from the term ‘operators’? Unlike some languages, the only kind of ‘operators’ that can be defined in Perl are prefix, and their names must be alphanumeric (or else invoked by &{...}-style trickery); there is no provision in Perl (or, rather, in the parser for Perl) for a user-defined infix or postfix operator—that's the price we pay for the syntactic flexibility that's already available.

the degeneracy of operators
I don't know what this means.
cause certain operators to lose their magical properties (ex, x=, .=)
Perhaps what you mean is that one can overload, say, x and x= separately, and that the overloaded behaviours need have nothing to do with one another? This is true, but not necessary: It's perfectly possible to overload, say, just x, and let Perl figure out for you how to perform x=.

Replies are listed 'Best First'.
Re^3: Hints Towards Writing a Module with New Operators
by Anonymous Monk on Dec 20, 2009 at 11:38 UTC
    "Overloading is something applied to objects, not operators

    " There's a concept called "Operator Overloading" that allows you to provide your own versions of operators to perform operations on class objects such as addition and subtraction of these objects lest Perl would treat these objects as strings that describe the class, a "string" here represents implementation and memory address (aka, references). If an operator is not overloaded and you called say + or - to add or subtract two objects (e.g defined data types) Perl would add/subtract on something like "Date=HASH(0x80f11fc)" ergo the need for Operator overloading which is described as:

    "When you overload one of Perl's built-in operators, you define how it behaves when it's applied to objects of a particular class", from Programming Perl.

    However, it seems that with overloading, there's a mixed statement because others describe it as:

    "The module is a standard part of the Perl distribution. It allows your objects to define how they will react to a number of Perl's operators. " from

    "the only kind of ‘operators’ that can be defined in Perl are prefix, and their names must be alphanumeric"

    You can define addition and subtraction operators, built-in functions, prefix and post fix operators and around 50 other functions out there...

    I think the entire concept is full of hassles that can be circumvented otherways around, but this is a SWAG for I don't have such information...and this discussion is amazing and informative... keep it going....

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://813582]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2022-12-08 02:50 GMT
Find Nodes?
    Voting Booth?

    No recent polls found