ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl.net
perl.net
Re: Perl.NET and com libraries
by Severan Rault other posts by this author
Aug 25 2004 9:30AM messages near this date
view in the new Beta List Site
RE: Perl.NET and com libraries | Re: Perl.NET and com libraries
Hi Jan,

Ok I think I will just submit to you my piece of code (which works now)
Basically what I am doing is pulling table from DB and construct a pure  
Perl struct (ex array of hashes) to represnt it. I did not wanted to use  
DBI (even if it works) but either SOAP call via ExecuteSQL service I have  
built or using Microsoft.ApplicationBlock which is a nice way of accessing  
SQL.

so I have first a function called AccessDB which is suppose to returns an  
Array of Hashes,
each hashes being ColumnName=> Value of the Sql table. No problem with Soap  
this is how it is return
(minus a trick 'cause SOAP::Lite does not like Array of Hashes).
So I needed a function to convert my DataSet to an a pure Array of hash. 
So here are my 2 functions:
----------------------------------------------------------------
#Query SQL using SOAP or Application Blocks
sub AccessDB
{
	my ($query) = @_;
	my @row;
	if ($DBACCESS eq "DIRECT")
		{
		eval {
		my $res;
		if ($query =~ /^SELECT/i)
		{
			$res = SqlHelper-> ExecuteDataset($dsn,CommandType->Text, $query) ;
		}
		else
		{
			$res = 	SqlHelper-> ExecuteNonQuery($dsn,CommandType->Text, $query) ;
		}
		@row = DataSetToArrayOfHash($res-> {Tables}->[0]);
			};
		if ($@)
			{
				die $@;
			}		
		}
	elsif ($DBACCESS eq "SOAP")
		{
			eval {
			my $obj = localhost::Service1-> new();
			@row = $obj-> ExecuteSQL($query,$dsn);
					};
			if ($@)
			{
				die $@;
			}
		}
return @row;
}
------------------------------------------------------------
The function that turns my DataSet into an Array of hashes
(Would be so cool to include stuff like this in PerlNET if you ask me

sub DataSetToArrayOfHash {
	my ($c) = @_;
	return $c if (!ref $c);
	my $cols = $c-> {Columns};
	my $rows = $c-> {Rows};
	my @struct;
	foreach my $row (@$rows)
	{
		my %hash;		
				foreach my $col (@$cols)
				{
					my $realn = $col-> {ColumnName};
					my $val = $row-> get_Item($realn);
					$hash{$realn} = (!ref $val) ? $val : Convert-> ToString($val); #HERE I  
have to use Convert-> ToString ....
															  #Why $val-> ToString() does not work?????
				}
		push @struct, \%hash;
	}	
	return @struct;
}
-------------------------------------------------------------------------------
So yeah the question is why $val-> ToString() does not work, but  
Convert-> ToString($val) does ?

Is it clearer now?

Cheers

Severan


On Wed, 25 Aug 2004 01:23:36 -0700, Jan Dubois <jand@[...].com>   
wrote:

>  On Tue, 24 Aug 2004, Severan Rault wrote:
> > I am having a problem converting PerlNET::int64 back into a scalar...
> 
>  PerlNET will treat an int64 as a scalar, so I guess I still don't
>  understand your usecase.
> 
> > basically I have $val = bless( do{\(my $o = undef)}, 'PerlNET::Int64'
> 
>  Please don't bless arbitrary references into PerlNET internal classes.
>  They are intentionally undocumented and their implementation is subject
>  to change between releases. What are you trying to accomplish?
> 
> > ); If I do "print $val;" then it print the value because PerlNET call
> > ToString on it for me, right ? however if I try :
> 
>  Yes, PerlNET will call ToString() on .NET objects if you use them in a
>  string context. It is the Perlish thing to do: treat them as a string in
>  string context and treat them as a number in numeric context. Actually,
>  PerlNET even handles boolean context specially. It is supposed to "do
>  what I mean" automatically.
> 
>  Of course there is a limit on what Perl can do with int64 values in
>  numeric context, as Perl numbers don't have the same value range. We
>  have been thinking about implicitly converting them to Math::BigInt
>  objects if they cannot be represented accurately in either integers or
>  floating point numbers. Is this what you expect to happen?
> 
> > my $res = $val->ToString(); print $res;
> >
> > It does not work.... ($res it undef...)
> 
>  Well, according to your bless statement above, it is an undefined  
>  value...
> 
> > I am trying to do a DataSet to pure perl Struct converter...
> 
>  Can you provide some more information? Ideally a complete little sample
>  program that show the problem?
> 
> > Do you have any idea?
> 
>  Sorry, I still don't understand what you are trying to do. Don't try to
>  push PerlNET into the strongly typed language corner. It tries to stay
>  as close as possible to being dynamically typed and make decisions at
>  runtime based on actual types of variables at that point in time.
> 
>  I'm very interested to learn if there is a gap in things you can do with
>  PerlNET. But I still don't get the problem. So please show me a complete
>  sample. Show me how to do something in C# and explain why you can't do
>  it in PerlNET.
> 
>  Cheers,
>  -Jan
> 
> 



-- 
Severan Rault
Technical Director

Kikker Interactive Pte Ltd
hp: +6598173517
_______________________________________________
Perl.NET mailing list
Perl.NET@[...].com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Thread:
Severan Rault
Severan Rault
Jan Dubois
Severan Rault

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved