journey has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

I am moving data from an xml-file into a mysql db and need to make a substitution on the fly to clean up a string so it matches the required "datetime" format (replace a letter with a space and truncate). I naively added the substitution expression to the bind value in the script below and got the following error message:

Can't modify non-lvalue subroutine call at blabla.pl line X
Here is the code I am using:
use strict; use DBI; use XML::XPath use XML::XPath::XMLParser; my $dbh = DBI->connect ("DBI:mysql:database", "root", "password", { RaiseError => 1, PrintError => 0}); my $xp = XML::XPath->new (filename => "input_file.xml"); my $nodelist = $xp->find ('//row'); foreach my $row ($nodelist->get_nodelist ()) { $dbh->do ("INSERT INTO users1 ( create_string, username) VALUES ( +?, ?)", undef, # no need for \%attr $row->find ("CreateDate")->string_value () =~ s/(\d\d\ +d\d-\d\d-\d\d)T(\d\d:\d\d:\d\d).*00/$1 $2/g, $row->find ("UserName")->string_value ()); } $dbh->disconnect ()
Any guidance on where I should go from here would be appreciated.
Regards
---J
Sample data <USERS1> <row> <CreateDate>2008-03-17T13:31:14.345-07:00</CreateDate> <UserName>foo</UserName> <Email>michael@example.com</Email> <UserId>a58d0b09-4f97-42ef-901e-cee41103bc12</UserId> </row> <row> <CreateDate>2008-04-11T14:59:57.123-07:00</CreateDate> <UserName>bar</UserName> <Email>spot@example.com</Email> <UserId>4b618e05-1eca-494b-baa5-ab85bd46f0f3</UserId> </row> </USERS1>

To create table in datatbase
create_table_users1.sql CREATE TABLE users1 ( create_string datetime , username varchar(32) );

Replies are listed 'Best First'.
Re: DBI Modifying a bind value
by almut (Canon) on Oct 08, 2009 at 02:15 UTC
     Can't modify non-lvalue subroutine call

    You'll have to use an intermediate variable to do the substitution, and then pass that variable to fill in the placeholder:

    my $create_date = $row->find ("CreateDate")->string_value(); $create_date =~ s/(\d\d\d\d-\d\d-\d\d)T(\d\d:\d\d:\d\d+).*00/$1 $2/g; $dbh->do(..., $create_date, ...

    Not beautiful, but that's how life is, sometimes :)

      Fabulous!
      I was scared to use an intermediate variable and mess up the binding. It just did not occur to me that I could insert it just before the "do" call... Many thanks!