http://www.perlmonks.org?node_id=747513

if(length $string>256){ # remove trailing spaces while (length $string>256 && $string=~m/(?:^\s)*\s+$/){ $string =~ s/(.*)\s$/$1/; } # remove beginning spaces while (length $string>256 && $string=~m/^\s+[^\s]/){ $string =~ s/^\s(.*)/$1/; } # anything else if (length $string >256){ $string = substr($string,0,256); } }
--------------------------------------------------- $dbh is an SQLite database handle via DBI.
$dbh->do( "CREATE TABLE $table (" . "blah," . "line text UNIQUE," . "col" . ");" ) or die "Cannot create table: $DBI::errstr"; $sth = $dbh->prepare("INSERT INTO $table VALUES(?,?,?)"); #$sth->bind_param(2,"$fld2",DBI::SQL_VARCHAR); print $fld2; #prints 0002 $sth->execute($fld1,$fld2,$fld3);
I want $fld2 to insert 0002 into the database, but it's dropping the leading 0's. The following works, but it doesn't use prepare/execute (which I like to use)
$sql = "INSERT INTO $table VALUES('$fld1','$fld2','$fld3')"; #print $sql . "\n"; $dbh->do($sql);
--------------------------------------------------
#! /usr/lcl/bin/perl use DBI; use CGI; use CGI::Carp qw(fatalsToBrowser carpout); #use strict; print "Content-type: text/html\n\n"; # declare messag +e type #Run the program _main(); # Subroutine parse_form_data decodes the form information. # A reference to a variable named request_form is passed to it. # Request_form is treated as an associate array and filled with # key-value pairs sent by the browser. # Parameters are used if the requestor requires a signature and the # form needs to be resubmitted. All cgi parameters are lowercase, # all $in params are camelcase sub _main { &parse_form_data (*in); # call parse_for +m_data function my $cgi = new CGI(); $in{'User_First'} =~ s/([\w']+)/\u\L$1/g; $in{'User_Last'} =~ s/([\w']+)/\u\L$1/g; _forminfo(); } #end _main() sub parse_form_data { local (*FORM_DATA) = @_; local ($query_string, @key_value_pairs, $key_value, $key, $value); read(STDIN, $query_string, $ENV{'CONTENT_LENGTH'}); @key_value_pairs = split(/&/, $query_string); foreach $key_value (@key_value_pairs) { ($key, $value) = split(/=/, $key_value); # break up/s +tore param and value $value =~ tr/+/ /; # convert pl +uses to spaces $value =~ s/%([\da-fA-F][\da-fA-F])/pack("C", hex($1)) +/eg; # decode uri encoding if (defined($FORM_DATA{$key})) { $FORM_DATA{$key} = join (", ", $FORM_DATA{$key}, $value); } else { $FORM_DATA{$key} = $value; } } } # end parse_form_data() sub _forminfo { ... }