Archive for the ‘Perl’ Category
Using Perl to Import Products into Salesforce.com Opportunities
Believe it or not I am in sales… Didn’t start out like that, but as some techies would say sold my soul the devil ![]()
Anyway one task that I get to do too often these days is creating, and approving Salesforce.com opportunities from my customers. They usually send me an Excel file with their proposed purchase order, I need to upload it to Salesforce.com so I can get the discounts approved.
Adding every single part number by hand is tedious, boring work, especially at the end of the quarter when the pressure to “make the numbers” builds up and we have no time to waste. So I revived some of my old Perl memories and wrote a little script that imports all the products at once into an already created opportunity.
If this is useful to you just follow these steps:
- Create Salesforce.com opportunity, and save it
- Copy the last part of the URL (that is your opportunity ID).
Example: https://na6.salesforce.com/0068000000K32EA the ID is 0068000000K32EA - Run import-products with your product file, Salesforce.com credentials and opportunity ID
You are set!
The product file is just an Excel with 2 columns, the first being the SKU or part number, and the second the desired quantity. Save the file in CSV format (you can do this by selecting CSV as the save type in Excel). Now use this file to import your products.
Script usage:
import-products --csv=product.csv --opportunity=OpportunityID
--user=UserName --pass=Password
[--delete]
product-file.csv
MUST be a Comma Separated file with two columns (SKU, Quantity).
No header line.
opportunityID
MUST be an existant Salesforce Opportunity to which you want to add
the products from the product.csv to.
user, pass
Salesforce.com user name and password.
delete
Delete all of the opportunity's products before adding any new ones.
Requirements:
- Perl 5.8 or higher
CPAN modules
- WWW::Salesforce::Simple
- Getopt::Long;
- Text::CSV_XS;
- Scalar::Util
You can get the source code from here. Hope you can use it and that it saves you some time as well!
CPAN Recursive dependency detected
Today I got a “Recursive dependency detected” message on CPAN while installing Config::Genreal.
Recursive dependency detected:
Test::Harness
=> A/AN/ANDYA/Test-Harness-3.17.tar.gz
=> File::Spec
=> S/SM/SMUELLER/PathTools-3.31.tar.gz
=> Scalar::Util
=> G/GB/GBARR/Scalar-List-Utils-1.22.tar.gz
=> Test::More
=> M/MS/MSCHWERN/Test-Simple-0.94.tar.gz
=> Test::Harness.
Cannot continue.
I installed Test::Harness by hand and when returning to CPAN all was cleared.
cd /root/.cpan/build/Test-Harness-3.17
perl MakeFile.PL
make
make test
make install
Eval Code Blocks or subroutines in a Perl Regular Expression
Some times you need some intelligence behind your Perl regular expressions, so you need to capture some pieces of text into $1, $2,… and then process them before you replace them in the string.
Simple example you need to convert multiple dates included in a big string from 12/24/2009 to 2009-12-24.
You could do the following:
my $line = "abc","cde","12/24/2009","fff","11/23/2008","sss";
my $rr = \&my_sub;
$line =~ s/(\d{1,2})\/(\d{1,2})\/(\d{4})/$rr->($1,$2,$3)/ge;
print $line;
sub my_sub {
my ($s1, $s2, $s3) = @_;
return sprintf("%04d-%02d-%02d",$s3,$s1,$s2);
}
Notice the /ge in the regular expression? That is what gets your code executed before the substitutions get made. This being Perl I know there are another 200 ways of doing this differently, and I am sure 198 are faster and simpler. Care to comment? What is your way of doing this?
Easy Debugging for your Perl Catalyst Web Applications
I decided to give Catalyst a try and see how it works for me.
When writing and code I feel that the easiest it is to debug it the better the quality of the product, as such although I love perl, I hate the perl debugger. It is all text/command line, very unfriendly and time consuming.
So long ago I decided to use a perl graphical debugger and after testing several packages I finally settled with ActiveState’s Komodo IDE.
It is a great IDE, and great debugger, where you can inspect all your code and variables in a nice, friendly way.
I also supports remote dubbuging which comes in handy to debug Catalyst applications.
- Download the Komodo IDE and remote debugging tools from ActiveState and install them locally on your system, if your using the MyApp_servre.pl to develop and test it, or in the target system where you application resides.There are several versions of the remote debugging tools, make sure the one you download matched your Komodo version, and make sure you download the right platform as well.
- tar zxf Komodo-PerlRemoteDebugging-5.1.0-27487-macosx-x86.tar.gz (in my case I am using MAC,)
- mkdir /usr/local/lib/perl
- cp -R Komodo-PerlRemoteDebugging-5.1.0-27487-macosx-x86 /usr/local/lib/perl
- vi /etc/profile
and add the lines, please replace the 55217 with the port your Komodo IDE expects, you can get that value in the Debug Menu, Listener Status (Port field).
export PERL5LIB=/usr/local/lib/perl/
export PERLDB_OPTS=RemotePort=localhost:55217
export DBGP_IDEKEY=
save /etc/profile and execute: . /etc/profile to read it (the dot is important.)
Now you can open your controller in Komodo and add the variable $DB::single=1 where you want your breakpoint.
# controller file: database.pm
sub index : Path :Args(0) {
my ( $self, $c ) = @_;
# template file we want to use
$c->stash->{template} = 'list_users.tt';
$DB::single = 1; # Debugger Breakpoint
# here we selected the ModelName::table_name
$c->stash->{users} = $c->model('OrdersDB::users');
}
- Start you application server with
perl -d script/MyApp_server.pl (-d starts debug mode) - leave your Komodo open, open your browser and point it to http://localhost:3000/database
- I you go back to Komodo you’ll your application the Debugger stopped at your breakpoint ($DB::single).
Enjoy the easy life of debugging Catalyst applications
Web Application Development
I am sort of an old school programmer, in the sense that when I started, there were no GUIs, web interfaces, or fancy IDEs. I never really developed any code professionally although I have done a few projects here and there, more for the fun of it than for profit.
I have developed a lot of small plugins for WordPress, or Trac, but never have I started a Web Application from scratch. I now want to write a small order management web app for my company but the reality is that I don’t know where to turn to. I would like to use one of the new Web Application Frameworks, but I don’t want to have to learn Ruby or Python. I am most comfortable with Perl.
So what do you suggest I do? Catalyst, Jifty, any other options?
Your comments are welcome!