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

$printnumber += 1;
or
$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;
This produces:
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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.