Isn't $printnumber the object reference??Well... your problem seems to be that in Perl, not everything is an object. A number is not an object.
So, you have to use ordinary operators for ordinary variables to make it do what you want. Here that would be something like
or$printnumber += 1;
$printnumber++;
Just for fun, I'll make $printnumber an object, and have it accept your syntax:
#!/usr/bin/perl -w { package NumberObject; use overload '""' => \&as_string, '0+' => \&as_number, '+' => \&ad +d; sub new { my $class = shift; my $value = shift; bless \$value, $class; } sub as_string { return "" . ${+shift}; } sub as_number { return 0 + ${+shift}; } sub add { my $self = shift; return ref($self)->new($self->as_number + shift); } sub inc { my $self = shift; $$self += shift; } } $\ = "\n"; my $printnumber = new NumberObject(-1); print "initial value:"; print $printnumber; print "looping:"; for (1 .. 10) { inc $printnumber 1; print $printnumber; } print "overload magic (adding 5):"; $printnumber += 5; print $printnumber; print "still an object (adding 2):"; inc $printnumber 2; print $printnumber;
initial value: -1 looping: 0 1 2 3 4 5 6 7 8 9 overload magic (adding 5): 14 still an object (adding 2): 16
In reply to Re: Syntax error with loops
by bart
in thread Syntax error with loops
by OxYgEn
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |