Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl子例程从何处获取实际参数中缺少的值?_Perl_Optional Parameters_Subroutine - Fatal编程技术网

Perl子例程从何处获取实际参数中缺少的值?

Perl子例程从何处获取实际参数中缺少的值?,perl,optional-parameters,subroutine,Perl,Optional Parameters,Subroutine,在跟踪一个bug时,我遇到了以下Perl子例程get\u billable\u pages。它需要12个参数 sub get_billable_pages { my ($dbc, $bill_pages, $page_count, $cover_page_count, $domain_det_page, $bill_cover_page, $virtual_page_billing, $job,

在跟踪一个bug时,我遇到了以下Perl子例程
get\u billable\u pages
。它需要12个参数

sub get_billable_pages {
    my ($dbc,
        $bill_pages,      $page_count,      $cover_page_count,
        $domain_det_page, $bill_cover_page, $virtual_page_billing,
        $job,             $bsj,             $xqn,
        $direction,       $attempt,
    ) = @_;

    my $billable_pages = 0;

    if ($virtual_page_billing) {
        my @row;
        ### Below is testing on the existence of the 11th and 12th parameters ###
        if ( length($direction) && length($attempt) ) {
            $dbc->xdb_execute("
                SELECT convert(int, value)
                FROM   job_attribute_detail_atmp_tbl
                WHERE  job             = $job
                AND    billing_sub_job = $bsj
                AND    xqn             = $xqn
                AND    direction       = '$direction'
                AND    attempt         = $attempt
                AND    attribute       = 1
            ");
        }
        else {
            $dbc->xdb_execute("
                SELECT convert(int, value)
                FROM job_attribute_detail_tbl
                WHERE job             = $job
                AND   billing_sub_job = $bsj
                AND   xqn             = $xqn
                AND   attribute       = 1
            ");
        }
        $cnt = 0;
        ...;
但有时只调用10个参数

$tmp_det = get_billable_pages(
    $dbc2,
    $row[6],          $row[8],          $row[7],
    $domain_det_page, $bill_cover_page, $virtual_page_billing,
    $job1,            $bsj1,            $row[3],
);
函数对第11个和第12个参数进行检查

sub get_billable_pages {
    my ($dbc,
        $bill_pages,      $page_count,      $cover_page_count,
        $domain_det_page, $bill_cover_page, $virtual_page_billing,
        $job,             $bsj,             $xqn,
        $direction,       $attempt,
    ) = @_;

    my $billable_pages = 0;

    if ($virtual_page_billing) {
        my @row;
        ### Below is testing on the existence of the 11th and 12th parameters ###
        if ( length($direction) && length($attempt) ) {
            $dbc->xdb_execute("
                SELECT convert(int, value)
                FROM   job_attribute_detail_atmp_tbl
                WHERE  job             = $job
                AND    billing_sub_job = $bsj
                AND    xqn             = $xqn
                AND    direction       = '$direction'
                AND    attempt         = $attempt
                AND    attribute       = 1
            ");
        }
        else {
            $dbc->xdb_execute("
                SELECT convert(int, value)
                FROM job_attribute_detail_tbl
                WHERE job             = $job
                AND   billing_sub_job = $bsj
                AND   xqn             = $xqn
                AND   attribute       = 1
            ");
        }
        $cnt = 0;
        ...;
  • 当函数只传递10个参数时,第11个和第12个参数是什么

  • 因为第11个和第12个参数都是随机值,所以只使用10个参数调用函数是错误的吗

  • 我认为这可能是错误的根源,因为当程序失败时,第12个参数有一个古怪的值

  • 我没有看到函数的另一个定义,它只接受10个参数


  • 不,这不是一个bug。剩下的参数是“unde”,您可以检查这种情况

    sub foo {
        my ($x, $y) = @_;
        print " x is undef\n" unless defined $x;
        print " y is undef\n" unless defined $y;
    
    }
    
    foo(1);
    
    印刷品

     y is undef
    

    这些值从参数数组
    @
    复制到标量变量列表中

    如果数组比列表短,则多余变量设置为
    undef
    。如果数组长于列表,则会忽略多余的数组元素

    请注意,原始数组
    @
    未被赋值修改。不会创建或丢失任何值,因此它仍然是调用子例程时传递的实际参数的最终来源

    ikegami建议我提供一些Perl代码来演示数组对标量列表的赋值。这是主要基于他的编辑的Perl代码

    use strict;
    use warnings;
    
    use Data::Dumper;
    
    my $x = 44;             # Make sure that we
    my $y = 55;             # know if they change
    
    my @params = (8);       # Make a dummy parameter array with only one value
    
    ($x, $y) = @params;     # Copy as if this is were a subroutine
    
    print Dumper $x, $y;    # Let's see our parameters
    print Dumper \@params;  # And how the parameter array looks
    
    输出

    $VAR1 = 8;
    $VAR2 = undef;
    
    $VAR1 = [ 8 ];
    
    因此,
    $x
    $y
    都会被修改,但如果数组中的值不足,则使用
    undef
    。这就好像源数组被无限期地扩展为
    unde
    元素

    现在让我们看一下Perl代码的逻辑<代码>未定义在条件测试中计算为false,但您可以像这样应用运算符

    if ( length($direction) && length($attempt) ) { ... }
    
    如果您有
    使用警告
    ,Perl通常会产生
    使用未初始化值
    警告。但是
    length
    是不寻常的,如果您请求
    undef
    值的长度(并且您运行的是12版或更高版本的Perl 5),它只会返回
    undef
    ,而不会警告您


    关于“我没有看到函数的另一个定义,只需要10个参数”,Perl没有像C++和java那样的函数模板——它是在子程序中的代码来查看它已经通过的和相应的行为。< /P>这只是列表分配的标准行为;函数参数并没有什么特别之处。我正要说“Borodin很快会告诉您使用警告和严格限制”。他几乎肯定没有穿,因为un my'd

    $cnt=0
    在16.3版中,长度对未定义变量的作用方式没有改变吗?@HunterMcMillen是:“
    length undef
    现在返回
    undef
    ”@ThisSuitesBlack不感谢,不记得版本。@HunterMcMillen:没有人记得版本。这就是为什么我们都保留了一张重要语义变化的小册子,以及Thero inception.yikes的日期和版本!这就是为什么当您有2到3个以上的参数时,几乎总是最好使用一个带有命名键的hashref(如果您愿意,也可以使用hash)。长参数列表是常见的bug来源……而且作者似乎正在对数据库查询执行类似的操作—将选定值的列表放入数组(@row),而不是命名的hashref或命名的标量列表。同样,这是一个共同的信息来源bugs@plusplus更不用说他们没有在查询中使用占位符来避免SQL注入。