Perl如何将数据传递给模块

时间:2015-11-03 16:47:58

标签: perl

自从我完成任何Perl工作以来,我需要写一个信用卡处理模块。该模块将有几个子程序,但一旦我弄清楚如何做一个,我可以做其余的。第一个子例程是添加客户信息。我需要的信息是客户编号,名字,姓氏,地址,城市,州和邮政编码。所有这些信息都将由调用程序提供,但有些字段可能是空白的。

sub addCustomer()
{
    my $tx = new Business::OnlinePayment("USAePay");
    $tx->content(
        login           => LOGIN,
        password        => PASSWORD,
        type            => "CC",
        action          => 'Recurring Authorization',
        description    => 'Business::OnlinePayment test',
        amount         => '49.95',
        invoice_number => '100100',
        name           => 'Tofu Beast',
        card_number    => '46464646464646',
        expiration     => '11/08',
        address        => '1234 Bean Curd Lane, San Francisco',
        zip            => '94102',
    );
    $tx->submit();

    if($tx->is_success()) {
        print "Card processed successfully: ".$tx->authorization."\n";
    } else {
        print "Card was rejected: ".$tx->error_message."\n";
    }
}

3 个答案:

答案 0 :(得分:2)

传统方式:

addCustomer($number, $firstName, $lastName, $address, $city, $state, $zip);

sub addCustomer {
    my ($number, $firstName, $lastName, $address, $city, $state, $zip) = @_;
    ...

对于这么多参数,命名参数可能更具可读性:

addCustomer( number     => $number,
             first_name => $firstName,
             last_name  => $lastName,
             address    => $address,
             city       => $city,
             state      => $state,
             zip        => $zip,
           );

sub addCustomer {
    my %opts = ( city => 'New York', # The defaults.
                 @_);

在较新的Perl版本(5.20+)中,您还可以使用signatures feature

use feature qw{ signatures };

sub addCustomer($number, $firstName, $lastName, $address, $city, $state, $zip) {

对于空白参数,如果您不使用命名参数,请使用undefq()

addCustomer(123, q(), 'Doe', '123 Street', 'London', undef, 'WC1A1BN')

答案 1 :(得分:2)

您希望执行此操作:

sub addCustomer()

因为原型你的sub没有参数。你可能根本不想要原型,因为perl原型并不是其他人所谓的原型。

但你可以阅读@_中的args - 这是一个输入标量列表 - 可以作为参考,但不是必须的。

my ( $first_arg, $second, $another, @everything_else ) = @_; 

注意 - 分配到列表将使用其余值,因此您无法:

my ( @stuff, $last_arg ) = @_; 

对于传递散列或散列引用的长列表可能很有用。

答案 2 :(得分:1)

将参数作为哈希值传递(或者更确切地说,作为哈希引用)。

# Outside of the subroutine
my %new_customer = (
    login          => LOGIN,
    password       => PASSWORD,
    type           => "CC",
    action         => 'Recurring Authorization',
    description    => 'Business::OnlinePayment test',
    amount         => '49.95',
    invoice_number => '100100',
    name           => 'Tofu Beast',
    card_number    => '46464646464646',
    expiration     => '11/08',
    address        => '1234 Bean Curd Lane, San Francisco',
    zip            => '94102',
);

add_customer(\%new_customer);

# Your subroutine
sub add_customer {
    my ($cust_ref) = @_;

    # Note: Don't use indirect object notation
    my $tx = Business::OnlinePayment->new("USAePay");

    $tx->content( %$cust_ref );
    $tx->submit();

    if ($tx->is_success()) {
        print "Card processed successfully: ".$tx->authorization."\n";
    } else {
        print "Card was rejected: ".$tx->error_message."\n";
    }
}
相关问题