Click here to get back home

perl pack string

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
perl pack string unxl3arn3r 03-24-2008
Posted by unxl3arn3r on March 24, 2008, 4:21 pm
Please log in for more thread options
Gurus
I seem to be unable to pack a string padded with nulls and containing
the length of the string prepended to it. This is my code snippet,
where am i going wrong ?

$message1 = pack("l! a*",1, "This is a test,This program is free
software,you can redistribute it terms as Perl network, This is why I
said to talk");
msgsnd($queue,$message1,0);

In my msgrcv i only want to recieve upto 100 characters. If i send a
string greater than 100 my script receiving the message fails.

Thanks
Perl Monger

Posted by xhoster on March 24, 2008, 4:57 pm
Please log in for more thread options
> Gurus
> I seem to be unable to pack a string padded with nulls and containing
> the length of the string prepended to it. This is my code snippet,
> where am i going wrong ?
>
> $message1 = pack("l! a*",1, "This is a test,This program is free
> software,you can redistribute it terms as Perl network, This is why I
> said to talk");

l! packs the "1", and a* packs the message. No where in there does the
length of the message enter into it.

I see that that is copied from the perldoc -f msgsnd. I can only
assume that that documentation is hosed.

Perhaps it should be this, instead?

pack("l! l!/a*",1,"foo")

Here l! packs the message type (1), l!/ packs the length of the message,
and a* packs the message itself.

> msgsnd($queue,$message1,0);

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Posted by unxl3arn3r on March 24, 2008, 10:23 pm
Please log in for more thread options
On Mar 24, 4:57 pm, xhos...@gmail.com wrote:
> > Gurus
> > I seem to be unable to pack a string padded with nulls and containing
> > the length of the string prepended to it. This is my code snippet,
> > where am i going wrong ?
>
> > $message1 = pack("l! a*",1, "This is a test,This program is free
> > software,you can redistribute it terms as Perl network, This is why I
> > said to talk");
>
> l! packs the "1", and a* packs the message. No where in there does the
> length of the message enter into it.
>
> I see that that is copied from the perldoc -f msgsnd. I can only
> assume that that documentation is hosed.
>
> Perhaps it should be this, instead?
>
> pack("l! l!/a*",1,"foo")
>
> Here l! packs the message type (1), l!/ packs the length of the message,
> and a* packs the message itself.
>
> > msgsnd($queue,$message1,0);
>
> Xho
>
> --
> --------------------http://NewsReader.Com/--------------------
> The costs of publication of this article were defrayed in part by the
> payment of page charges. This article must therefore be hereby marked
> advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
> this fact.

why do I need to have the second variable in pack ? All I need to do
is pack my outgoing string and prepad its length in front of it. Since
I am doing a* doesn't it already mean its a string of characters, then
why do i need to define the $type = 1 ?

Posted by xhoster on March 25, 2008, 12:20 pm
Please log in for more thread options
> On Mar 24, 4:57 pm, xhos...@gmail.com wrote:
> > > Gurus
> > > I seem to be unable to pack a string padded with nulls and
> > > containing the length of the string prepended to it. This is my code
> > > snippet, where am i going wrong ?
> >
> > > $message1 = pack("l! a*",1, "This is a test,This program is free
> > > software,you can redistribute it terms as Perl network, This is why I
> > > said to talk");
> >
> > l! packs the "1", and a* packs the message. No where in there does the
> > length of the message enter into it.
> >
> > I see that that is copied from the perldoc -f msgsnd. I can only
> > assume that that documentation is hosed.
> >
> > Perhaps it should be this, instead?
> >
> > pack("l! l!/a*",1,"foo")
> >
> > Here l! packs the message type (1), l!/ packs the length of the
> > message, and a* packs the message itself.
> >
> > > msgsnd($queue,$message1,0);
> >

(please don't quote sigs when you reply. Sig snipped)

> why do I need to have the second variable in pack ?

I don't understand your question. None of the packs I've seen in this
thread are given two variables. They are given three constants--a constant
format string, an constant integer, and a constant message string.


> All I need to do
> is pack my outgoing string and prepad its length in front of it.

But the code you posted prepended it with the native signed long
representation of 1. 1 is not the length of that message.

> Since
> I am doing a* doesn't it already mean its a string of characters, then
> why do i need to define the $type = 1 ?

If you believe that part of the documentation of "msgsnd", you have to do
that because that is what the interface requires that you do. If you don't
believe that part of the documentation, then I don't know what to tell you.
Perl has some dark corners. If you don't want to put up with them, then
don't use those parts of Perl. Anyway, your problem seems to be with sysV
messages, not with "pack" itself.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Posted by Ben Morrow on March 25, 2008, 12:51 pm
Please log in for more thread options

Quoth xhoster@gmail.com:
> > On Mar 24, 4:57 pm, xhos...@gmail.com wrote:
> > > > Gurus
> > > > I seem to be unable to pack a string padded with nulls and
> > > > containing the length of the string prepended to it. This is my code
> > > > snippet, where am i going wrong ?
> > >
> > > > $message1 = pack("l! a*",1, "This is a test,This program is free
> > > > software,you can redistribute it terms as Perl network, This is why I
> > > > said to talk");
> > >
> > > l! packs the "1", and a* packs the message. No where in there does the
> > > length of the message enter into it.
> > >
> > > I see that that is copied from the perldoc -f msgsnd. I can only
> > > assume that that documentation is hosed.
> > >
> > > Perhaps it should be this, instead?
> > >
> > > pack("l! l!/a*",1,"foo")
> > >
> > > Here l! packs the message type (1), l!/ packs the length of the
> > > message, and a* packs the message itself.
> > >
> > > > msgsnd($queue,$message1,0);
>
> > why do I need to have the second variable in pack ?
>
> I don't understand your question. None of the packs I've seen in this
> thread are given two variables. They are given three constants--a constant
> format string, an constant integer, and a constant message string.
>
> > All I need to do
> > is pack my outgoing string and prepad its length in front of it.
>
> But the code you posted prepended it with the native signed long
> representation of 1. 1 is not the length of that message.

The msgsnd docs are lying. The second argument of msgsnd should be the
message, with a native long message type on the beginning; that is, the
pack template 'l! a*' is correct, assuming your system doesn't pad after
longs in structures. No length is required anywhere: msgsnd(2) takes a
length argument, but perl can work this out from the length of the
passed-in string.

To the OP: why on earth aren't you using IPC::Msg? (Whose docs also lie,
by the way: there is no need for a pack in the argument to ->snd.)

Ben


Similar ThreadsPosted
strange output of pack in perl 5.8.0 September 9, 2004, 7:36 pm
"RFC": re [un]pack() September 10, 2004, 12:15 am
pack 'C3U*' not same as pack 'C3(xC)*' June 23, 2005, 7:48 am
Need help with pack October 1, 2005, 11:00 pm
Trying to force 32-bit in pack() October 12, 2004, 5:19 pm
pack and unpack ? January 9, 2005, 11:37 am
pack problem? August 27, 2006, 11:17 pm
sprintf or pack January 30, 2007, 7:33 am
pack /unpack issue November 10, 2005, 7:48 pm
pack, unpack and 64-bit values February 9, 2006, 12:55 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap