in reply to Squeezing $a+1 to be magical like $a++

Not that I approve of this, but you would need to overload binary + to recognise when it is called in void context. Otherwise my $y = $x + 1; would modify $x with chaos resulting. I don't think there is a way to do that in the manner of wantarray.

As an aside, $a and $b are already magical, representing package scope variables sacred to sort. Also be careful of numbers with leading zeros. They can be taken as octal and refuse to recognise '8' and '9'

Update: Rereading your question, I think I misunderstood what you want. Here is code without any overloading:

sub addsome { ++$_[0] for 1..$_[1]; }
This is rough and completely untested. The references to args are used so that the original string is modified with ++ magic.

Update2: The code works, but it's not very efficient. Now I understand that you want addition to act on alphanumeric strings according to the rules for ++ on non-numeric strings. I suspect you don't require modification in place. That makes my comment about void context useless, and suggests that overload of binary + is perfectly approporiate.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: Squeezing $a+1 to be magical like $a++
by pixel (Scribe) on Oct 10, 2001 at 13:16 UTC
    Not that I approve of this, but you would need to overload binary + to recognise when it is called in void context. Otherwise my $y = $x + 1; would modify $x with chaos resulting. I don't think there is a way to do that in the manner of wantarray
    .

    When you write a method that overrides a binary operation (like add), the method gets passed three arguments - the two operands and a third argument which tells you if the operands have been swapped (for code like 1 + $obj). If this third argument is undef then the original statement is in void context.

    Blessed Be
    The Pixel

      Since that third argument is ... ah, erm... overloaded, is it possible to tell the difference between 1 - $obj and $obj - 1 if they are both in void context? Will the third param be undef even if the arguments have been swapped?

      -Blake

        Actually, the third argument doesn't indicate whether the statement is in void context, but rather whether the statement is an assignment varient. Here's the relevant section from perldoc overload:


        Calling Conventions for Binary Operations

        The functions specified in the use overload ... directive are called with three (in one particular case with four, see the section on Last Resort) arguments. If the corresponding operation is binary, then the first two arguments are the two arguments of the operation. However, due to general object calling conventions, the first argument should always be an object in the package, so in the situation of 7+$a, the order of the arguments is interchanged. It probably does not matter when implementing the addition method, but whether the arguments are reversed is vital to the subtraction method. The method can query this information by examining the third argument, which can take three different values:

        FALSE the order of arguments is as in the current operation.
        TRUE the arguments are reversed.
        undef the current operation is an assignment variant (as in $a+=7), but the usual function is called instead. This additional information can be used to generate some optimizations. Compare the section on Calling Conventions for Mutators.


        The point is so that you can distinguish between

        print($obj += 6);

        and

        print($obj + 6);

        They should both print the same thing, but in the first case $obj should be increased by 6 and in the second case it shouldn't.

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you don't talk about Perl club."