On Thu, Jun 21, 2001 at 02:01:22PM -0400, Chas Owens wrote:
> On 21 Jun 2001 12:40:07 -0400, Brett W. McCoy wrote:
> <snip />
> 
> >In C++, you can overload the = operator to use the copy constructor.  I don't 
>remember if in Perl
> > you can overload =
> 
> <snip />
> 
> use overload "=" => \&clone;

I take it, then, that you haven't actually tried this.

The program:

    #!/usr/bin/perl -w

    use overload "=" => sub { print "assignment"; return $_[0]; },
                 "+" => sub { print "addition";   return $_[0]; };

    my $obj = bless([], "main");

    print "obj  = obj + 1  ";  $obj  = $obj + 1;  print "\n";
    print "obj += 2        ";  $obj += 2;         print "\n";
    print "obj  = obj + 1  ";  $obj  = $obj + 1;  print "\n";
    print "obj  = 4        ";  $obj  = 4;         print "\n";
    print "obj += 2        ";  $obj += 2;         print "\n";


outputs:

    obj  = obj + 1  addition
    obj += 2        addition
    obj  = obj + 1  addition
    obj  = 4        
    obj += 2

The moral: you can't overload the assignment operator.  If you assign a
different value to a variable, the variable is no longer the original
object.  The exception, of course, is if you assign the object back to the
variable, which happens in the first, second, and third cases.


Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

Reply via email to