Overloading a Perl Operator

Overloading a Perl Operator

am 05.10.2011 23:40:06 von Phillip Richcreek

I'm going to use a package called Data::Money in place of a similar
module that I had previously used but is no longer available for
downloading/ installing. Data::Money does everything that the other
package does (as far as the features that I am using), and it would
pretty much be a drop-in replacement but for one difference:
Data::Money does not overload the == and != but instead overloads the
<=> operator. So where I'm using " if ($a==$b)" and if($A!=$b)" a lot
of places, I'd have to change these to " if (! ($a<=>$b)) ?" some
places and "if($a<=>$b) ?" other places. I think I might also be using
some < and >, which would require way beyond just find and replace.

So...
This might be an opportunity to do some OO programming. Can someone
point me to basic documentation for writing code to overload an
operator in perl? Like, does it have to be in a "package"? Can I just
piggy-back it onto the Data::Money class? etc. No doubt this approach
will take 10 times the effort of just going in and brute force
changing my existing code, but this will be a lot more fun. Or not :-)

TIA,
Phil
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Overloading a Perl Operator

am 06.10.2011 00:37:04 von Jenda Krynicky

From: Phillip Richcreek

> I'm going to use a package called Data::Money in place of a similar
> module that I had previously used but is no longer available for
> downloading/ installing. Data::Money does everything that the other
> package does (as far as the features that I am using), and it would
> pretty much be a drop-in replacement but for one difference:
> Data::Money does not overload the == and != but instead overloads the
> <=> operator. So where I'm using " if ($a==$b)" and if($A!=$b)" a lot
> of places, I'd have to change these to " if (! ($a<=>$b)) ?" some
> places and "if($a<=>$b) ?" other places. I think I might also be using
> some < and >, which would require way beyond just find and replace.

I don't think you need to do any of this. According to the docs the
other comparison operators are autogenerated if the <=> is defined.

See
perldoc overload
section "MAGIC AUTOGENERATION".

Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Overloading a Perl Operator

am 06.10.2011 02:59:14 von Michael Zeng

--===============0700661209==
Content-Type: multipart/alternative; boundary=000e0cd6ec8e5a483a04ae96d591

--000e0cd6ec8e5a483a04ae96d591
Content-Type: text/plain; charset=ISO-8859-1

see programming perl book ---- overloading chapter ,




On Thu, Oct 6, 2011 at 6:37 AM, Jenda Krynicky wrote:

> From: Phillip Richcreek
>
> > I'm going to use a package called Data::Money in place of a similar
> > module that I had previously used but is no longer available for
> > downloading/ installing. Data::Money does everything that the other
> > package does (as far as the features that I am using), and it would
> > pretty much be a drop-in replacement but for one difference:
> > Data::Money does not overload the == and != but instead overloads the
> > <=> operator. So where I'm using " if ($a==$b)" and if($A!=$b)" a lot
> > of places, I'd have to change these to " if (! ($a<=>$b)) ?" some
> > places and "if($a<=>$b) ?" other places. I think I might also be using
> > some < and >, which would require way beyond just find and replace.
>
> I don't think you need to do any of this. According to the docs the
> other comparison operators are autogenerated if the <=> is defined.
>
> See
> perldoc overload
> section "MAGIC AUTOGENERATION".
>
> Jenda
> ===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz=====
> When it comes to wine, women and song, wizards are allowed
> to get drunk and croon as much as they like.
> -- Terry Pratchett in Sourcery
>
> _______________________________________________
> ActivePerl mailing list
> ActivePerl@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>



--
Yours Sincerely
Zeng Hong

--000e0cd6ec8e5a483a04ae96d591
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

see programming perl book ---- overloading chapter ,

=A0



=A0

On Thu, Oct 6, 2011 at 6:37 AM, Jenda Krynicky <=
span dir=3D"ltr"><Jenda@krynicky.cz=
> wrote:

; PADDING-LEFT: 1ex" class=3D"gmail_quote">From: Phillip Richcreek < ref=3D"mailto:pwrichcreek@gmail.com">pwrichcreek@gmail.com>


> I'm going to use a package called Data::Mone=
y in place of a similar
> module that I had previously used but is no=
longer available for
> downloading/ installing. Data::Money does eve=
rything that the other

> package does (as far as the features that I am using), and it would >> pretty much be a drop-in replacement but for one difference:
> =
Data::Money does not overload the == and !=3D but instead overloads the=


> <=3D> operator. So where I'm using " if ($a==$b)&qu=
ot; and if($A!=3D$b)" =A0a lot
> of places, I'd have to chan=
ge these to " if (! ($a<=3D>$b)) ?" some
> places and=
"if($a<=3D>$b) ?" other places. I think I might also be us=
ing

> some < and >, which would require way beyond just find and repla=
ce.

I don't think you need to do any of this. According to=
the docs the
other comparison operators are autogenerated if the <=
=3D> is defined.


See
=A0perldoc overload
section "MAGIC AUTOGENERATION".=


Jenda
=====3D Jenda@Krynicky.cz ===3D p://jenda.krynicky.cz/" target=3D"_blank">http://Jenda.Krynicky.cz =3D=
====
When it comes to wine, women and song, wizards are allowed<=
br>
to get drunk and croon as much as they like.
=A0 =A0 =A0 =A0-- Terry Pra=
tchett in Sourcery






--
=A0 =A0 =A0=
=A0 =A0 =A0 Yours Sincerely
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Zen=
g Hong


--000e0cd6ec8e5a483a04ae96d591--

--===============0700661209==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0700661209==--