Mabooka-Mabooka has asked for the wisdom of the Perl Monks concerning the following question:

I am not a Perl programmer. But I badly need to fix my colleague's code, and I'm stuck. Please excuse me if I came to a wrong place.

Here's the slightly obfuscated snippet from real script:

#!/usr/local/bin/perl -w use strict; sub get_useful_number_1() { my ($self, $harness, $bridle_radius, $max_spit_distance, $num_legs +, $last_name, $day_phone, $days_without_water) = @_; my $camel_id; if (1 == 1) { $camel_id = get_camel_id($self, $harness, $bridle_radius, $max_ +spit_distance, $num_legs, $last_name, $day_phone); # line above: NO COMPLAINTS } } sub get_camel_id() { my ($self, $harness, $bridle_radius, $max_spit_distance, $num_legs +, $last_name, $day_phone) = @_; my $camel_id = 18; if ($camel_id == 18) { return($camel_id); } else { return(-18); } } sub get_useful_number_2() { my ($self, $harness, $bridle_radius, $max_spit_distance, $num_legs +, $last_name, $day_phone, $days_without_water) = @_; my $camel_id; if (1 == 1) { $camel_id = get_camel_id($self, $harness, $bridle_radius, $max_ +spit_distance, $num_legs, $last_name, $day_phone); # line above: Too many arguments for main::get_camel_id at ./er +rand-01.pl line 41, near "$day_phone)" } } 1;

The code won't compile, giving a syntax error message "Too many arguments". The only difference between caller1 and caller2 is... the 2nd one is located after the function I need to call in the file.

My best bet: it's related to the sin #2: http://www.perl.com/pub/a/1999/11/sins.html#2

Can anybody bring piece back to my life please?

20050416 Janitored by Corion: Added formatting, changed PRE to CODE, linkified link

Replies are listed 'Best First'.
Re: Badly need to call a function...:-)
by BUU (Prior) on Apr 16, 2005 at 21:55 UTC
    In perl, you don't declare the subroutine names with parentheses. Remove them and ye shall be fine.
      Wow! You know, this has hit me once already, - when I 1st tried Perl a few years ago:->). This time, I didn't declare the subroutine, only tried to call it, with an exact copy of another such call in the same file...:-)... MANY THANKS!
Re: Badly need to call a function...:-)
by eibwen (Friar) on Apr 16, 2005 at 22:15 UTC
    Omit the function argument prescription unless you want to make an imposition.

    sub my_function { # ... }
    No Argument Limitations

    sub my_function () { # ... }
    Arguments limited to the empty list: ()

    Since your code above appends the empty list to the function defintions the arguments are limited to the empty list. Given that your invocation passes arguments, perl returns an error. In addition to use strict; you may also want to try use diagnostics; for an explanation of perl's error messages:

    (F) The function requires fewer arguments than you specified.

Re: Badly need to call a function...:-)
by Cody Pendant (Prior) on Apr 16, 2005 at 23:22 UTC
    I have to know. Why would someone use "if 1 == 1"? Is that part of the obfuscation or is it actually in the code?


    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print
      As far as I can tell, if (1==1) { } is being used as an alternative to commenting out the lines. While this may provide a simple way to skip a block of code by changing a single character, there appears to be a performance detriment:

      #!/usr/bin/perl -w use strict; if (1==1) { 1; }
      $ perl -MO=Deparse equal BEGIN { $^W = 1; } use strict 'refs'; do { '???': };
      #!/usr/bin/perl -w use strict; if (0==1) { 1; }
      $ perl -MO=Deparse unequal BEGIN { $^W = 1; } use strict 'refs'; do { '???' };
      #!/usr/bin/perl -w use strict; if (1==0) { 1; }
      $ perl -MO=Deparse unequal2 BEGIN { $^W = 1; } use strict 'refs'; do { '???' };

      #!/usr/bin/perl -w use strict; 1;
      $ perl -MO=Deparse uncommented BEGIN { $^W = 1; } use strict 'refs'; '???':
      #!/usr/bin/perl -w use strict; # 1;
      $ perl -MO=Deparse commented BEGIN { $^W = 1; }

      UPDATE: Added the if (1==1) { } corollary

      Dear pedant and all gurus: I am very pleased with your attention, didn't expect anything like that.
      regarding (1==1): gentlement, c'mon, I only wanted to "kind-of-preserve" the "if" statement without exposing the business logic.
      Greately surprised that this sort of things can have a performance implications. From the other hand, curious: can these tiny details have an impact on real-world stuff?
        The performance implication depends on your implementation. If it's a critical application, particularly one that iterates significantly, the difference may be significant. Conversely, if the code isn't iterative, or only sees a few iterations, the difference is likely neglible. Given this understanding, you may want to benchmark the codes using the Benchmark module:

        use Benchmark qw(cmpthese); @data = (1,2,3,4); # the values that would have been in @_ my $minCPUsec = 15; cmpthese(-$minCPUsec, { with => \&sub_with_ifs, without => \&sub_without_ifs });

        And change @_ in the referenced subs to @data.

        Additionally, you may be interested in the Benchmarking Your Code tutorial, but I'd recommend using cmpthese instead of timethese.