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

HI Monks, I wanted to pass differnt hashes to a sub routine at differnt times is it possible to do this way ?
{ my %MSISDN=(); #populate the subscription list to hash MSISDN open(FILE,"/tmp/subscription_list") || die "Unable to open sub +scription file"; sub file_merge(\%MSISDN); close(FILE); my %Calls=(); #populate the usage types to hash Calls open(FILE1,"/tmp/Call_Det")|| die "Unable to open call_details +file"; sub file_merge(\%Calls); close(FILE1); } sub file_merge(%hash_ref) { while (<>) { chomp; my ($key, @values) = split /-/, $_; push @{$hash_ref{$key}}, [ @values ]; } }

Replies are listed 'Best First'.
Re: hash as subroutine argument
by davorg (Chancellor) on Jul 02, 2009 at 13:16 UTC

    Well, yes it's possible to do it using something like your approach. Your file_merge() subroutine would look like this:

    sub file_merge { my $hash_ref = shift; while (<>) { chomp; my ($key, @values) = split /-/, $_; push @{$hash_ref->{$key}}, [ @values ]; } }

    But actually, that's not right as you're reading from the default filehandle each time.

    I'd do this completely differently.

    sub file_merge { my $filename = shift; open my $fh, '<', $filename or die "$filename: $!\n; my %hash; while (<$fh>) { chomp; my ($key, @values) = split /-/, $_; push @{$hash{$key}}, [ @values ]; } return %hash; }

    You would then call it like this:

    my %msisdn = file_merge('/tmp/subscription_list'); my %calls = file_merge('/tmp/Call_Det');

    Another (perhaps better) alternative would be to return a hash reference rather than a hash.

    As always strict and warnings will point out some of the major problems with your code.

    --

    See the Copyright notice on my home node.

    Perl training courses

      yeah this is much better option rather than passing hash as the argument. But the file hadle in the sub function has to be closed i suppose.
        But the file hadle in the sub function has to be closed i suppose.

        The filehandle is a lexical variable. It will be closed automatically as the variable goes out of scope (i.e. as the subroutine is exited).

        --

        See the Copyright notice on my home node.

        Perl training courses

Re: hash as subroutine argument
by ww (Archbishop) on Jul 02, 2009 at 13:18 UTC
    Did you try perl -c scriptname?

    Did you try it with use strict; use warnings;?

    Either would give you some significant hints.

      yeah i though i will first do with the logic and then use the strict and warning module.
        Wrong!

        As your code demonstrates, doing the logic avails nothing if the syntax is wrong. Use strict and warnings as intended -- as helpers -- to get the syntax right, so you can tell if the algorithm you've selected|developed\whatever produces what you intended.

        If your code won't compile, you'll never know if you've got the logic right.

Re: hash as subroutine argument
by BioLion (Curate) on Jul 02, 2009 at 13:11 UTC

    A minor change to your code...

    my %MSISDN=(); #populate the subscription list to hash MSISDN open(FILE,"/tmp/subscription_list") || die "Unable to open sub +scription file"; %MSISDN = %{ file_merge(\%MSISDN) }; close(FILE); my %Calls=(); #populate the usage types to hash Calls open(FILE1,"/tmp/Call_Det")|| die "Unable to open call_details +file"; %Calls = %{ file_merge(\%Calls) }; close(FILE1); } sub file_merge { ## take from @_ (the args passed to the subrtn) my $hash_ref = shift; while (<>) { chomp; my ($key, @values) = split /-/, $_; push @{$hash_ref{$key}}, [ @values ]; } return $hash_ref; }

    But you would be better off passing in the filepath, doing the reading and parsing in your sub and passing back the hash ref... see below by davorg...

    Just a something something...

      You're reading from the wrong filehandle (a bug you inherited from the original code).

      --

      See the Copyright notice on my home node.

      Perl training courses

        Sorry - didn't run the code, just tried to change the bit they asked about... lesson learned...

        Just a something something...
Re: hash as subroutine argument
by Anonymous Monk on Jul 02, 2009 at 13:15 UTC