Reputation: 6387
I have a debug that just sent me on a wild goosechase in Perl. I fixed my original problem, but now I'm puzzled as to why my debug doesn't work. I have the following code:
sub debug {
my ($msg) = @_;
print $DBGFILE "DBG: $msg\n" if $dbg;
}
In one place, I have a call to print an array of hashes as follows:
debug "v----------- parsed variables: -------------";
debug Dumper @knownVars;
debug "^----------- parsed variables: -------------";
This only seems to output the first of close to 200 entries in the array:
DBG: v----------- parsed variables: -------------
DBG: $VAR1 = {
'local' => 1,
...
'op' => ':=',
};
DBG: ^----------- parsed variables: -------------
If I use the debugger, and do
p Dumper @knownVars
at that line, it outputs the entire array. The array is big (170 entries with around 40-200 lines each), so I'm wondering if I made syntax error in this or if this is some sort of buffer size limitation. (note perl 5.22.0)
Upvotes: 0
Views: 76
Reputation: 9231
To preface, the arguments passed to (and returned from) a function can only be one thing in transit: a list of scalar values. You cannot pass an array or hash variable, except by taking a reference to them, which is a scalar value. For more on this, see http://altreus.blogspot.com/2011/08/lists-and-things-made-of-lists.html.
debug Dumper @knownVars;
This passes the contents of @knownVars
as a list to Dumper. According to the documentation, Dumper will return a list of strings in list context, so it will return one string for each element. This list of strings is then passed to your debug function, that only acts on the first item passed in the argument list.
debug Dumper \@knownVars;
By passing a reference to the array instead of a list of the contents, this gives Dumper only one item to transform into one string, that your debug function will then receive.
Alternatively, you can update your debug function to loop through and act on all strings passed in @_
, as print does in the common idiom: print Dumper @items;
Upvotes: 2