in reply to Using ioctl()

ikegami is right that you can use pack and unpack create / read from structs. however, using pack() to write structs does not work portably, especially when you've got a struct made up of different-sized elements - AFAIK C compilers are allowed to switch elements in struct around, and anyway insert unused bytes to align elements depending on their type. IOW:

struct { byte a; long b; }

may take up 6 bytes on a system with 4-byte integers and a 2-byte alignment requirement for integers. OTOH on a system with 4-byte aligned 4-byte integers, it might take up 8 bytes.

Ofcourse, there are modules to deal with all of this, a CPAN search gave me this (probably incomplete) list:

note - I've not tested any of these and if none of them work, I know Inline::C does, but it's probably overkill for this application. Also, if you only need it to work on a specific system, you can hardcode the pack() template anyway (you might have to experiment to get the right one).

Replies are listed 'Best First'.
Re^2: Using ioctl()
by tye (Sage) on Oct 08, 2004 at 21:48 UTC

    The C standard allows for arbitrary padding for alignment reasons but in practice how structs are padded is pretty consistent among most C compilers.

    Also, most good structs are laid out such that no padding is required.

    So you can use pack quite portably in most cases. But, yes, there are exceptions.

    - tye