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

Hello Everyone,
Please see following code:

sub Clear{ my $item=$_[0]; $item=~s/^\s+//; $item=~s/\s+$//; return $item; } sub Txn_Extract{ my $name_of_file=$_[0]; my $hash_ref=$_[1]; $hash_ref=Clear($hash_ref); #Here something goes wrong. +When I remove this line %DATA_HASH gets populated properly. Need to k +now what happens when I clear the $hash_ref variable with above sub C +lear. my @row; . .. . . } sub Main { Txn_Extract (file.txt,\%DATA_HASH); print Dumper \%DATA_HASH }

OUTPUT: VAR{}

Replies are listed 'Best First'.
Re: Understand why dumper printed null hash
by choroba (Cardinal) on Jun 12, 2012 at 10:11 UTC
    By exposing $hash_ref to string context in $hash_ref =~ s/...//, the value has been stringified to contain something like HASH(0x82a8768). Try adding another Dumper after calling Clear.
Re: Understand why dumper printed null hash
by Anonymous Monk on Jun 12, 2012 at 10:06 UTC

    $hash_ref=Clear($hash_ref); #Here something goes wrong.

    Yup, this is why

    #!/usr/bin/perl -- use strict; use warnings; use Data::Dump; my %hash = ( 1 .. 4 ); my $hashref = \%hash; print "$hashref\n"; dd $hashref; $hashref =~ s/^/WHY DID I DO THIS??? /; dd $hashref; __END__ HASH(0x99a99c) { 1 => 2, 3 => 4 } "WHY DID I DO THIS??? HASH(0x99a99c)"

      Thank you all!