I'm working on a perl module here at work for a reporting system we're developing. I'd like to be able to pass in a text file for parsing and munging, and programatically specify what to do as follows:
# Handles initial parse (gets field # names from first line of 'file.out' my $file = ParseFile('file.out'); # Specifies fields on which to subtotal $file->keys = qw( Cust_Grp Order_Date Item Name ); # Adds a field $file->field('Line_Total', sub { $row->{Unit_Price}*$row->{Qty} }); ... # Process file $file->process();
The '$file->field(...)' line allows me to dynamically add columns to the report based on existing data. I can't hard code the new column definitions, as this code needs to be somewhat abstract. For that reason, I'm using an anonymous sub to define the value of the field. As the file processor runs, each line will add a column to the report by running that sub, pulling values from that report line. I'd like the '$row' variable to be a hash reference filled with key/value pairs from the current line. I'd like it to be autopopulated in the sub, much like  sort {$a <=> $b} @list autopopulates the '$a' and '$b' in the code block there. Is there a way to do this? I've super searched on '$a $b', etc., but too many results are kicking out since those variables are often used as temp variables, etc. Any help would be appreciated. Thanks.

In reply to Need an $a and $b as in perl's sort routine by rational_icthus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.