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

Hi Everyone. I have trying to create a module in perl,but it is not behaving as thought. The Perl Module code is :
package iVPU_Parameter; require Exporter; @ISA=qw(Exporter); @EXPORT = qw(Configchk); sub Configchk() { &a=$_[0]; @arr_iVPU_standard=$_[1]; @arr_iVPU_current=$_[2]; blah blah blah }
I saved the file as iVPU_Parameter.pm and i am calling this module from a progam code:
use warnings; use iVPU_Parameter; $vlan_id=xxx; open(FH_CURRENT,"Running.txt"); open(FH_STANDARD,"Standard.txt"); @iVPU_current=<FH_CURRENT>; @iVPU_standard=<FH_STANDARD>; &Configchk($vlan_id,@arr_iVPU_standard,@arr_iVPU_current);
It is able to call the module But when i try to print the array arguments in the Perl Module iVPU_Parameter.pm,by
print @arr_iVPU_standard;
it does not print that ,but when i do the same in the calling program,it prints there so there is no problem in file handling.So i think the problem is in parameter passing or scope of the arguments. Also when i remove the ampersand (&) from the line
&Configchk($vlan_id,@arr_iVPU_standard,@arr_iVPU_current);
it shows the error as Too Many arguments . Could you please help in this matter.

Replies are listed 'Best First'.
Re: Module Creation and Function Call
by NetWallah (Canon) on Mar 19, 2012 at 18:04 UTC
    Try passing Array REFERENCES, instead of arrays, as parameter.

    Passing arrays confuses the receiving sub parameters, when it tries to decode the contents of @_.

    Obviously, you will need to modify the called sub as well, to receive the array reference(s) properly.

    sub Ref_receiver{ my ($ref1,$ref2)=@_; # What we get are SCALARs, each of which is a re +ference to an array print $ref1->[0]; # prints 33 } my (@x,@y); $x[0]=33; Ref_receiver (\@x, \@y); # Pass REFERENCES to the arrays @x, @y

                 All great truths begin as blasphemies.
                       ― George Bernard Shaw, writer, Nobel laureate (1856-1950)

Re: Module Creation and Function Call
by Riales (Hermit) on Mar 19, 2012 at 18:21 UTC

    You're getting the 'too many arguments' error because you're defining the Configchk subroutine with prototypes and when you call it with the ampersand, you circumvent prototypes. Change the subroutine definition to this:

    sub Configchk { &a=$_[0]; @arr_iVPU_standard=$_[1]; @arr_iVPU_current=$_[2]; blah blah blah }
      yeah i got it..

      thanks girish

Re: Module Creation and Function Call
by thargas (Deacon) on Mar 19, 2012 at 18:49 UTC

    First, add:

    use strict; use warnings;
    to your module so that perl will tell you of all the different things you're doing which probably aren't what you want.

    Then go read the docs on creating subs: perldoc perlsub ignoring the stuff about attributes (you don't need them) and prototypes (you don't want them either). That will tell you why you don't want to use &Configchk.

    Then you can read the docs for Exporter (perldoc Exporter) which may help with why you can't see @arr_iVPU_standard. (Hint: it's real name is @iVPU_Parameter::arr_iVPU_standard). But that doesn't really matter because you really don't want to do that either. Pass the array's in as array-refs, because (... you'll know why from reading perlsub)

    Perl comes with lots of well written, well organized documentation. I won't tell you to read all of it, but please do read the relevant parts. If you're not sure which they are, try: perldoc perl or google for it.

      Does any of your thoughtless boilerplate advice, other than "Perl comes with lots of well written, well organized documentation," do anything to help solve the OP's problem?
        I thought it might help, if followed. It was a selection of advice pointed at the problems in his code. I learned perl by reading the online docs, so it *is* possible.