The whole idea of pack is that you convert number types into a byte stream, which perl 
represents as a string -- pack always creates a string.

Using Inline, it is easy to get the string as a char * in C.

When you get the "char *" in C, though, if you have packed in one of the "native" 
formats, you can interpret the contents of the string as native machine data -- i.e. 
as a C array.  You are letting pack do the work of converting a perl array into a C 
array for you (remember an array in C is just a contiguous chunk of memory with the 
same type of data over and over in a row).

so if you, pack with, say, "l!", you will get longs (for long you need the ! to ensure 
that you get native length):

 my $list = pack( 'l!*',(65,66,67,68,69) );
  #printf "%vd\n",$list;

  cLinePlot($list,$list,5);

  __DATA__
  __C__
  void cLinePlot(char * x, char *y, int els) {

     long *long_x = (long *)x;
     long *long_y = (long *)y;
     /* ... */
  }

But if you use "i" you get ints:
 my $list = pack( 'i*',(65,66,67,68,69) );
  #printf "%vd\n",$list;

  cLinePlot($list,$list,5);

  __DATA__
  __C__
  void cLinePlot(char * x, char *y, int els) {

     int *int_x = (int *)x;
     int *int_y = (int *)y;
     /* ... */

  }

etc.

you can read a lot on pack from :
http://www.perldoc.com/perl5.6.1/pod/func/pack.html
(aka perldoc -f pack)

Hope this helps.

-JAS




--- Begin Message ---
Alright, 
I'm guessing that my version of perl was not compiled to support
64 bit integers, because when I use pack with "q" or "Q" I get
the error message:
Invalid type for pack: "q", or "Q"

Then, I tried to use SvTYPE to see what kind of SV it
was recieving:

  my $list = pack( 'n*',(65,66,67,68,69) );
  #printf "%vd\n",$list;

  cLinePlot(\$list,\$list,5);

  __DATA__
  __C__
  void cLinePlot(SV* x, SV* y, int els) {

     SV* tmpX;

     printf("hey in C\n");
     //tmpX = SvRV(x);
     printf("type of x=%d\n",SvTYPE(SvRV(x)));
  }

This resulted in: "type of x=4" which according to the following
http:[EMAIL PROTECTED]/msg00445.html
represents a SVt_PV (String, right?)

Any ideas why this would come across as a string?

Thanks...Brady

-----Original Message-----
From: Elizabeth Mattijsen [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, June 12, 2002 6:01 AM
To: Nicholas Clark
Cc: bbcannon; '[EMAIL PROTECTED]'
Subject: Re: Instantiating arrays with Inline


At 12:29 PM 6/12/02 +0100, Nicholas Clark wrote:
>On Wed, Jun 12, 2002 at 10:37:37AM +0200, Elizabeth Mattijsen wrote:
> > At 05:30 PM 6/11/02 -0600, bbcannon wrote:
> > >My initial try with "V" did not work, but I'm going to
> > >keep pecking at it.  If a double is represented in C with
> > >32 bits...
> > I'm assuming that.  It may not always be true.  I've been lead to
> > understand that some compilers for 64bit processors consider a double 
> to be
> > 64 bit.
>I think it's never true. I've never known float to be anything but 32 bits,
>and double to be anything but 64 bits on any system I've used, nearly all
>of which have int and long both as 32 bits.
>However, I'd not be surprised if float was 64 bits on Crays.

That just goes to show how rusty my C is...  ;-)


Liz


--- End Message ---

Reply via email to