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

hello, I have a problem with the parameter passing mechanism among 2 subroutines. my code is as follows.

I have 2 subroutines. the first one takes 2 parameters from command line:
* text file name containing file list
* file format

then it reads the given file, and fetches individual file names (with full path).

then it passes a file path and format to second subroutine called test_format. but these parameters becomes "unreadable" from the array object containing parameters, when I try to convert this array to an hash object (args2hash). because when I print the array object, it gives this output.

"/abc/def/xyz.dat format dat"

Actually it should be like this:

"input /abc/def/xyz.dat format dat"

The "input" keyword is missing.

What should I do?

sub test_filelist_format { my $obj = shift; my $args = Parser::args2hash (@_); my $input = $args->{'input'}; my $format = $args->{'format'}; if (!$input || !$format) { die("input file or format missing!\n"); } ## if not list readable, return -1 if ( $obj->is_readable(file=>$input) < 0 ) {return -1} ... my $formatstatus = test_format ("input" => $line, "format" => $for +mat); ... } sub test_format { my $obj = shift; my $args = Parser::args2hash (@_); my $input = $args->{'input'}; my $format = $args->{'format'}; my $gzip = $args->{'gzip'}; my $columns = $args->{'columns'}; if (!$input || !$format) { die("input file or format missing!\n"); } if ( $obj->is_readable(file=>$input) < 0 ) {return -1}; ... }

Replies are listed 'Best First'.
Re: parameter passing problem
by kcott (Archbishop) on Jul 02, 2013 at 15:38 UTC

    G'day gokhan,

    Welcome to the monastery.

    You've written both test_filelist_format() and test_format() as instance methods. If that's what they're supposed to be, you should invoke them like this:

    $object->method(@args)

    If they're supposed to be just normal subroutines, you'd call them like this:

    function(@args)

    and you should remove this line:

    my $obj = shift;

    You haven't shown sufficient code for me to advise which way around these should be: I see no use of $obj in either but you've omitted code from both so maybe $obj is used where you've shown "...". Regardless, that should be enough information for you to fix your current problem. I'd recommend you look in other parts of your code for similar issues.

    -- Ken

      problem solved :) thanks for advise
      $obj is not important for here. Because it gives error before coming to lines of $obj usage. I've updated the code. I added both the lines for $obj and args2hash function.
      Sorry, I didn't know the function of "shift". I am new in Perl. Ok, the problem may be the shift operation.
        Perl documentation is available online (shift) and at your command prompt:
        perldoc -f shift
Re: parameter passing problem
by Loops (Curate) on Jul 02, 2013 at 15:01 UTC

    If you add a debugging display in test_format to show the contents of the $obj variable, you should see that it contains the word "input"

Re: parameter passing problem (in mystery code)
by Anonymous Monk on Jul 02, 2013 at 14:58 UTC

    What should I do?

    Fix your code so it isn't broken, fix Parser::args2hash