Michael Kraus <[EMAIL PROTECTED]> wrote:

: One of my classes has a method called format_cost which takes a
: number (integer representing a monetory amount in cents) and
: pretties it up for
: display as a dollar amount. I.e. format_cost(120000) returns
: "$1,200.00". 
: 
: When I have the function stand-alone it behaves as it should,
: however when I have it as a method on a class, and make a call
: to it (external to the class/object), it returns the class or
: object itself. 
 
[snip]
 
: displays:
: 
: Cost per billing period: Super_DB_Object
: Cost per billing period: Customer=HASH(0x82ddef8)
: Cost per billing period: $2,277.00
: Cost per billing period: 227700
: 
: 
: Clearly, I'm missing something crucial here... :)

    And so are we. Like the code for the method for
format_cost(), but I imagine that you are not shifting
the object off at the beginning of that subroutine.


    You can't use this as a method in an object.

sub format_cost {
    return sprintf '$%.2f', shift() / 100;
}


    You need to use this.

sub format_cost {
    my $self = shift;
    return sprintf '$%.2f', shift() / 100;
}


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to