Trying to use a CSV file as a custom field values source: "Can't use string (...) as an ARRAY ref while "strict refs" in use."

I am trying to create a custom field values source with a CSV file as a
source.

The CSV file is in this format (for reference):

“System ID”,“UPC”,“EAN”,“Custom SKU”,“Manufact.
SKU”,“Item”,“Qty.”,“Price”,“Tax”,“Category”,“Manufacturer”,“Season”,“Department”,“MSRP”,“Tax
Class”,“Default Cost”,“Vendor”
“210000000002”,“”,“”,“”,“”,“LABOR - Do Something Important: Make
Moneys”,“-”,“$30.00”,“No”,“Service / Important
Stuff”,“”,“”,“”,“0.00”,“Labor”,“0.000000000”,“”

The CSV is a dump from my POS system and I would like to have Request
Tracker populate the values of an “Enter multiple values” type custom field
with specific elements from that CSV joined together (Columns 5 + 7).

The code I have hacked together to fill an array with the data I want is as
follows:

package RT::CustomFieldValues::LaborItems;

use strict;
use warnings;

use base qw(RT::CustomFieldValues::External);
use Text::CSV;

sub SourceDescription {
return ‘Labor items from the POS.’;
}

sub ExternalValues {
my @csvdata;
my $file =
‘/var/rt4/lib/RT/CustomFieldValues/item_listing_LABOR.csv’;

    my $csv = Text::CSV -> new({ sep_char => ',' });
    open ( my $cvsfh, '<', $file ) or die "Could not open $file: $!";

    while( my $row = $csv -> getline( $cvsfh ) ) {
            push @csvdata, $row;
            }
    shift( @csvdata );                              # Remove the

first entry (just contains the names of the columns).

    my @customvalues;
    my $i = 0;
    my $name;
    my $price;
    my $cvname;
    foreach ( @csvdata ){
            my $name = @{ $csvdata[$i][5] };
            my $price = @{ $csvdata[$i++][7] };
            my $cvname = join "", $name, " - ", $price;                    

Stick the price on the end of the name.

            push( @customvalues, name => $cvname );
            }
    return @customvalues;

}

RT::Base->_ImportOverlays();

1;

I’ve tried searching through other threads with this error, but haven’t been
able to translate anything I’ve learned into a working field values source
for RequestTracker. If I run the script sans RT specific code I can print
@customvalues and get an array of hashes, or dump @customvalues and see that
the information I want is making it that far at least.

The specific error I am getting is as follows:

[2491] [Sat Sep 27 16:54:57 2014] [error]: Can’t use string ("LABOR - Do
Something Important: "…) as an ARRAY ref while “strict refs” in use at
/var/rt4/sbin/…/lib/RT/CustomFieldValues/LaborItems.pm line 30, <$cvsfh>
line 114.
Stack:
[/var/rt4/sbin/…/lib/RT/CustomFieldValues/LaborItems.pm:30]
[/var/rt4/sbin/…/lib/RT/CustomFieldValues/External.pm:194]
[/usr/local/share/perl5/DBIx/SearchBuilder.pm:507]
[/var/rt4/share/html/Admin/CustomFields/Modify.html:257]
[/var/rt4/share/html/Admin/autohandler:49]
[/var/rt4/sbin/…/lib/RT/Interface/Web.pm:682]
[/var/rt4/sbin/…/lib/RT/Interface/Web.pm:370]
[/var/rt4/share/html/autohandler:53]
(/var/rt4/sbin/…/lib/RT/Interface/Web/Handler.pm:209)

I have a feeling I’ve just been in Python-land for too long and I’ve made a
silly mistake somewhere, so if anybody wouldn’t mind glancing over my code
I’d really be super stoked for the help!

Thanks for the assistance,
Aaron

View this message in context: http://requesttracker.8502.n7.nabble.com/Trying-to-use-a-CSV-file-as-a-custom-field-values-source-Can-t-use-string-as-an-ARRAY-ref-while-stri-tp58645.html

Wow, turns out I just needed a good nap…

Lots wrong with my first try, not sure how I thought that was going to work!

Anyways, for everybody else looking to figure this out, the functioning code
follows:

package RT::CustomFieldValues::LaborItems;

use strict;
use warnings;

use base qw(RT::CustomFieldValues::External);
use Text::CSV;

sub SourceDescription {
return ‘Labor items from the POS.’;
}

sub ExternalValues {
my @csvdata = []; # Create an empty array for CSV data.
my $file =
‘/var/rt4/lib/RT/CustomFieldValues/item_listing_LABOR.csv’;

    my $csv = Text::CSV -> new({ sep_char => ',' });
    open ( my $cvsfh, '<', $file ) or die "Could not open $file: $!";

    while( my $row = $csv -> getline( $cvsfh ) ) {
            push @csvdata, $row;
            }
            shift @csvdata;                                                                

Removes the first line, just has column names.

    my @customvalues;
    for my $csvline ( @csvdata ){
                    my $itemname = $csvline->[5];
                    my $price = $csvline->[7];
                    my $cvname = join "", $itemname, " - ", $price;                       

Stick the price on the end of the name.

        my $cvhash->{ 'name' } = $cvname ;
        push( @customvalues, $cvhash );
        }
    return \@customvalues;

}

#dump &ExternalValues;

RT::Base->_ImportOverlays();

1;

RT is now successfully populating the custom field values!

View this message in context: http://requesttracker.8502.n7.nabble.com/Trying-to-use-a-CSV-file-as-a-custom-field-values-source-Can-t-use-string-as-an-ARRAY-ref-while-stri-tp58645p58646.html