DBD::CSV: Problem with userdefined functions

Posted by sid_com on Stack Overflow See other posts from Stack Overflow or by sid_com
Published on 2010-05-09T08:01:53Z Indexed on 2010/05/09 8:08 UTC
Read the original article Hit count: 185

Filed under:
|
|
|
|

From the SQL::Statement::Functions documentation:

Creating User-Defined Functions
...
More complex functions can make use of a number of arguments always passed to functions automatically. Functions always receive these values in @_:
sub FOO { my( $self, $sth, $rowhash, @params ); }

#!/usr/bin/env perl
use 5.012;
use warnings; use strict;
use DBI;

my $dbh = DBI->connect( "DBI:CSV:", undef, undef, { RaiseError => 1, } );
my $table = 'wages';
my $array_ref = [   [ 'id', 'number' ],  
            [ 0, 6900 ], 
            [ 1, 3200 ], 
            [ 2, 1800 ],    ];

$dbh->do( "CREATE TEMP TABLE $table AS import( ? )", {}, $array_ref );

sub routine {
    my $self = shift;
    my $sth = shift;
    my $rowhash = shift; #
    return $_[0] / 30; 
};

$dbh->do( "CREATE FUNCTION routine" );
my $sth = $dbh->prepare( "SELECT id, routine( number ) AS result FROM  $table" );
$sth->execute();
$sth->dump_results();

When I try this I get an error-message:

DBD::CSV::st execute failed: Use of uninitialized value $_[0] in division (/) at ./so.pl line 27.
[for Statement "SELECT id, routine( number ) AS result FROM "wages""] at ./so.pl line 34.

When I comment out the third argument I works as expected ( because it looks as if the third argument is missing ):

#!/usr/bin/env perl
...  
sub routine {
    my $self = shift;
    my $sth = shift;
    #my $rowhash = shift;
    return $_[0] / 30; 
};
...

0, 230
1, 106.667
2, 60
3 rows

Is this a bug?

© Stack Overflow or respective owner

Related posts about perl

Related posts about dbi