2015-11-03 72 views
0

由于我完成了任何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"; 
    } 
} 
+2

你应该通过阅读和吸收[Perl的子程序(http://perldoc.perl.org/perlsub.html)和 [Perl模块,风格指南](http://perldoc.perl.org启动/perlmodstyle.html) – Borodin

+0

[Perl Subroutine Arguments]的可能重复(http://stackoverflow.com/questions/19234209/perl-subroutine-arguments) –

+0

请勿使用原型(sub-declaration中的圆括号)。他们不是必需的,他们不会做你认为他们做的事。他们会更可能给你奇怪的错误比其他任何事情。它们被用来使子程序模仿内置函数,没有别的。你的应该是'sub addCustomer {'。 – TLP

回答

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"; 
    } 
} 
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+),你也可以使用signaturesfeature

use feature qw{ signatures }; 

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

对于空白的参数,使用undefq()如果你不使用命名参数:

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

请注意,其他安装不太可能有Perl v5.20可用,所以如果有任何意图编写可移植的东西,那么您应该坚持传统技术 – Borodin

+0

为什么使用'q()'时最简单的方法是''''或'“”',这是每个人都能理解的。 – TLP

+0

@TLP:[PBP](https://www.safaribooksonline.com/library/view/perl-best-practices/0596001738/ch04.html) – choroba

2

你想做到这一点:

sub addCustomer() 

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

但是你可以从@_读取参数 - 这是一个输入标量列表 - 可以是引用,但不需要。

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

注 - 分配给一个列表将消耗值的休息,所以你不能:

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

而对于传入散列或散列引用一个长长的清单可能是有用的。

相关问题