Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to print sql query 1

Status
Not open for further replies.

roguedude

Technical User
Feb 25, 2003
10
US
Hi All,

I've essentially just started using perl and my first task is to access sales force and extract the database contents. So far, I've been successful getting a list of objects from the salesforce database, but any variation of "print" I use does not yeild the results I'm looking for. The following is an example of the closest I can get:

#!/usr/bin/perl -w
use use DBI;
use warnings;
use strict;


# Authenticate with the Salesforce API based on command line parameters.
my $sforce = 'username' => $ARGV[0],
'password' => $ARGV[1]
);

my $query = 'Select a.NAME, a.Type, a.Lead from MY_NEW_DB a';

my $result = $sforce->do_query( $query );


foreach my $field (@{ $result } ) {
print $field->{'NAME'} . {'Type'} . {'Lead'} . "\n";

}

Here is what the output is:

Odd number of elements in anonymous hash at ./connect line 27.
Odd number of elements in anonymous hash at ./connect line 27.
BobBuilderConstructionHASH(0xb3d4ff8)HASH(0xa792710)

Please help.

Thanks....
 
Code:
foreach my $field (@{$result}) {
   print $field->{'NAME'} . $field->{'Type'} . $field->{'Lead'} . "\n";
}

What you were doing was only printing $field->{'NAME'} and then printing the value of two anonymous hash references (not the values you were expecting; you were instead creating new anonymous hashrefs, and Perl complained because hashes need even numbers of elements, and then printing a hashref as a scalar produces a "HASH(0x...)" string.

So ya need to reference each key the same. :)

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Thanks for your reply and explanation. I was able to see the data using your sugestion; however got "Use of uninitialized value in concatenation (.) or string at ./connect line 27."
 
If you have any null values in your table, the corresponding values in the $field hash will be `undef` in Perl, and Perl gives warnings like that when trying to do something using a variable that has an undef value.

Ya could do
Code:
print (defined $field->{'NAME'} ? $field->{'NAME'} : '') . (defined $field->{'Type'} ? $field->{'Type'} : '') . (defined $field->{'Lead'} ? $field->{'Lead'} : '');

or more simply

Code:
foreach my $field (@{$result}) {
   my $name = $field->{'NAME'} || '';
   my $type = $field->{'Type'} || '';
   my $lead = $field->{'Lead'} || '';
   print $name . $type . $lead;

That'd work as long as none of those fields will have a value of the number 0, because 0, blank, and undef all return false.

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
ok, so, yeah, I definately have nulls within the data. Will the above simply ignore the nulls? I have other "padding" surronding the data. I have to ensure that the data remains intact (nulls and all), as it's being ported to a different db. In addition, there is a possibility of 0's within the data. Does this mean I should not use either example from above, or just don't use the second one?

Thsnks...
 
The second example, i.e.

Code:
my $name = $field->{'NAME'} || '';

is doing, $name = the return value of $field->{NAME}, if true; otherwise it returns '' if false. So if $field->{'NAME'} is undef, 0, or blank, $name will be ''; any other value in $field->{'NAME'} and $name would be that value.

If you're using Perl 5.10 there's a feature of "defined or" which helps. But if you can't use Perl 5.10, just use the longer form of "defined or"

Code:
my $name = defined $field->{'NAME'} ? $field->{'NAME'} : ''; # Perl < 5.10
my $name = $field->{'NAME'} // ''; # Perl 5.10

Anyway, if you want to preserve nulls and everything, then don't mess with the values. Perl DBI will return nulls as `undef`, and so if you keep it undef then that'll keep it null (i.e. if you insert an undef into an update/insert SQL query, it will write it in the table as null). Just note that whenever you try to use a variable that has an undef value, Perl complains a little bit. :p

You can temporarily stop it from complaining with the warnings pragma.

Code:
my $var = undef;

no warnings; # turn them off
print "var = $var\n";
use warnings; # turn them back on

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Thanks for your help. I was able to get the data and suppress the warnings using the info you provided. I hope I can return the favor one day...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top