Object::eBay::Currency - Represents a currency used by eBay


Object-eBay documentation Contained in the Object-eBay distribution.

Index


Code Index:

NAME

Top

Object::eBay::Currency - Represents a currency used by eBay

SYNOPSIS

Top

    # assuming that $item is an Object::eBay::Item object
    my $price = $item->selling_status->current_price;

    # supports string, numeric and boolean context
    print "Going for $price\n";          # "Going for USD12.99"
    print "Bargain!\n" if $price < 20;   # numeric context
    print "Has price\n" if $price;       # boolean context

    # accessor methods are also available
    my $currency_id = $price->currency_id; # just the currency ID
    my $value       = $price->value;       # same as numeric context
    my $string      = $price->as_string;   # same as string context

DESCRIPTION

Top

Many of eBay's API calls return values which represent an amount of a particular currency. Item prices are a good example. An Object::eBay::Currency object represents a particular quantity of a particular currency. Methods throughout Object::eBay return Currency objects where appropriate.

As mentioned in the SYNOPSIS string, numeric and boolean context are supported. In numeric context, the object evaluates to the amount of currency represented. In string context, the object evaluates to a string which represents the currency and the quantity both (see as_string for details). Boolean context returns the same value as numeric context. This overloading of boolean context makes Object::eBay::Currency objects behave as expected in boolean context instead of returning true all the time.

METHODS

Top

as_bool

Returns a boolean representation of the currency amount. Namely, if the currency amount is 0, false is returned. Otherwise, true is returned. This method provides the implementation for boolean context.

as_string

Returns a string representation of the currency amount. The string is produced by concatenating the currency ID with the quantity. For example 10 U.S. Dollars is represented as 'USD10.00' The value will always be rounded to two numbers after the decimal. The rounding algorithm is that used by sprintf('%.2f').

This method provides the value for string context.

currency_id

Returns a string identifying the type of currency this object represents. The possible values are determined by eBay.

value

Returns the quantity of currency represented by this object. It's generally a bad idea to use this method unless you already know the type of currency. For example, if the return value is '10', that could mean 10 U.S. Dollars or 10 Euros.

This method provides the value for numeric context.

DIAGNOSTICS

Top

Missing 'content' and/or 'currencyID'

This exception is thrown when trying to construct a Currency object from an invalid hashref. This indicates a problem (or change) with eBay's XML response.

CONFIGURATION AND ENVIRONMENT

Top

Object::eBay::Currency requires no configuration files or environment variables.

DEPENDENCIES

Top

* Class::Std

INCOMPATIBILITIES

Top

None known.

BUGS AND LIMITATIONS

Top

Please report any bugs or feature requests to bug-object-ebay at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Object-eBay. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc Object::eBay;

You can also look for information at:

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/Object-eBay

* CPAN Ratings

http://cpanratings.perl.org/d/Object-eBay

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=Object-eBay

* Search CPAN

http://search.cpan.org/dist/Object-eBay

ACKNOWLEDGEMENTS

Top

AUTHOR

Top

Michael Hendricks <michael@ndrix.org>

LICENSE AND COPYRIGHT

Top


Object-eBay documentation Contained in the Object-eBay distribution.

package Object::eBay::Currency;
our $VERSION = '0.4.0';

use Class::Std; {
    use warnings;
    use strict;
    use Carp;

    my %value_for       :ATTR( :get<value>       );
    my %currency_id_for :ATTR( :get<currency_id> );

    sub BUILD {
        my ($self, $ident, $args_ref) = @_;
        my $details = $args_ref->{object_details};

        my $msg = "Missing 'content' and/or 'currencyID'\n";
        my $content = $details->{content};
        my $id = $details->{currencyID};
        croak $msg if !defined($content) || !defined($id);

        $value_for{$ident}       = $details->{content};
        $currency_id_for{$ident} = $details->{currencyID};
    }

    sub as_string :STRINGIFY {
        my ($self) = @_;
        return $self->get_currency_id() . sprintf('%.2f', $self->get_value());
    }

    # aliases providing naming similar to other Object::eBay classes
    sub value       :NUMERIFY { $_[0]->get_value()       }
    sub as_bool     :BOOLIFY  { $_[0]->get_value() != 0  }
    sub currency_id           { $_[0]->get_currency_id() }
}

1;

__END__