26

I have a function below in perl

sub create_hash()
{
my @files = @_;

        foreach(@files){
         if(/.text/)
         {

         open($files_list{$_},">>$_") || die("This file will not open!");

         }
      }

}

I am calling this function by passing an array argument like below:

create_hash( @files2);

The array has got around 38 values in it. But i am getting compilation errors:

Too many arguments for main::create_hash at ....

what is the wrong that i am doing here?

my perl version is :

This is perl, v5.8.4 built for i86pc-solaris-64int
(with 36 registered patches, see perl -V for more detail)
Vijay
  • 65,327
  • 90
  • 227
  • 319
  • 11
    Take off the `()`? (As in `sub create_hash { .. }`) –  Aug 13 '12 at 06:53
  • what happens if you call your function like: create_hash( files2); (without "@" sign ) – Arfeen Aug 13 '12 at 06:54
  • @ pst if i remove them error is : Array found where operator expected at process.pl line 71, at end of line (Do you need to predeclare create_hash?) syntax error at process.pl line 71, near "create_hash @files2" – Vijay Aug 13 '12 at 06:59
  • @peter Wrong spot: remove at sub-declaration, not invocation. Although `create_hash @files2;` should still be valid *without* the prototype .. (did a line carry-over?) –  Aug 13 '12 at 07:00
  • 1
    BTW, 5.8.4 is a *really* ancient version of Perl (although that has nothing to do with your problem). You should really consider installing a newer version. [perlbrew](http://search.cpan.org/perldoc?App%3A%3Aperlbrew) can help with that. – cjm Aug 13 '12 at 07:03
  • I am not an admin. i can just use the perl available for coding. – Vijay Aug 13 '12 at 13:47
  • 1
    With perlbrew, you don't need to be an admin to install a new version of Perl for your own use. All you need is a suitable C toolchain and enough disk space. – cjm Aug 13 '12 at 17:25

2 Answers2

80

Your problem is right here:

sub create_hash()
{

The () is a prototype. In this case, it indicates that create_hash takes no parameters. When you try to pass it some, Perl complains.

It should look like

sub create_hash
{

In general, you should not use prototypes with Perl functions. They aren't like prototypes in most other languages. They do have uses, but that's a fairly advanced topic in Perl.

Community
  • 1
  • 1
cjm
  • 61,471
  • 9
  • 126
  • 175
-3

May use array reference as:

sub create_hash {
    my ($files) = @_;
    foreach(@{$files)){
      ...
    }
}

create_hash(\@files2);
cdtits
  • 1,118
  • 6
  • 7
  • One *could* use array-refs, but it would still generate an error with `sub create_hash() { rest_of_that_code }` .. –  Aug 13 '12 at 06:59
  • @pst: not "sub create_hash() {", it's "sub create_hash {" – cdtits Aug 13 '12 at 07:02
  • 2
    @cdtits, while your solution works, you failed to explain why Peter's code didn't work in the first place. – cjm Aug 13 '12 at 07:04
  • @cdtits That's my point -- the declaration is different. Switching to an array-ref is only secondary :) –  Aug 13 '12 at 07:06
  • @pst: then why comment on your use of an array reference but not mention the prototyping at all? – Borodin Aug 13 '12 at 07:17