Changeset 1991


Ignore:
Timestamp:
04/27/17 00:47:33 (7 years ago)
Author:
nanardon
Message:

Use hash for loadCSV options to allow more parameters

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/LATMOS-Accounts-Web/lib/LATMOS/Accounts/Web/Controller/Tools/CSV.pm

    r1990 r1991  
    4242    loadCSV( 
    4343        $upload->fh, 
    44         sub { 
     44        cb => sub { 
    4545            my ($res, $count) = @_; 
    4646 
     
    7575            return 1; 
    7676        }, 
    77         sub { 
     77        initcb => sub { 
    7878            my ($csv) = @_; 
    7979            @{ $c->stash->{attrs} } = $csv->column_names; 
  • trunk/LATMOS-Accounts/bin/la-load-csv

    r1988 r1991  
    6868loadCSV( 
    6969    $fh, 
    70     sub { 
     70    cb => sub { 
    7171        my ($res, $linecount) = @_; 
    7272 
     
    8282        $ochelper->Automate($info) or 
    8383            die "Cannot create object line $linecount (not enough information ?)\n"; 
    84     } 
     84    }, 
    8585); 
    8686 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Utils.pm

    r1985 r1991  
    180180Parse CVS files and return an array for each parsed line. 
    181181 
    182 If defined call C<$callback> for parsed line. 
    183  
    184 C<$initcallback>, if defined, is called just after the first line. 
     182C<%options> may contains 
     183 
     184=over 4 
     185 
     186=item C<initcb> An function call after parsing first line 
     187 
     188=item C<cb> A function call for each line 
     189 
     190=back 
    185191 
    186192=cut 
    187193 
    188194sub loadCSV { 
    189     my ($fh, $cb, $initcb) = @_; 
     195    my ($fh, %opt) = @_; 
    190196 
    191197    my $csv = Text::CSV->new({ 
     
    201207    $csv->column_names($columns); 
    202208 
    203     if ($initcb) { 
    204         $initcb->($csv); 
     209    if ($opt{initcb}) { 
     210        $opt{initcb}->($csv); 
    205211    } 
    206212 
     
    209215    while ( my $row = $csv->getline_hr( $fh ) ) { 
    210216        $linecount++; 
    211         if ($cb) { 
    212             if (! $cb->($row, $linecount)) { 
     217        if ($opt{cb}) { 
     218            if (! $opt{cb}->($row, $linecount)) { 
    213219                return; 
    214220            } 
Note: See TracChangeset for help on using the changeset viewer.