Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > perl pack string

Reply
Thread Tools

perl pack string

 
 
unxl3arn3r
Guest
Posts: n/a
 
      03-24-2008
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
 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      03-24-2008
unxl3arn3r <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      03-24-2008
unxl3arn3r 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");
> 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.


$ perl -le'
use Data:umper;
$Data:umper::Useqq = 1;

my $x = pack q[l!/Z*], "This is a test,This program is free software,you
can redistribute it terms as Perl network, This is why I said to talk";

print Dumper $x;
'
$VAR1 = "w\0\0\0This is a test,This program is free software,you can
redistribute it terms as Perl network, This is why I said to talk\0";


And then to unpack:

$ perl -le'
use Data:umper;
$Data:umper::Useqq = 1;

my $x = unpack q[l!/Z*], "w\0\0\0This is a test,This program is free
software,you can redistribute it terms as Perl network, This is why I
said to talk\0";

print Dumper $x;
'
$VAR1 = "This is a test,This program is free software,you can
redistribute it terms as Perl network, This is why I said to talk";


If you want to limit the size of the string to 100 characters:

perldoc -f substr



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
 
Reply With Quote
 
unxl3arn3r
Guest
Posts: n/a
 
      03-25-2008
On Mar 24, 4:57 pm, (E-Mail Removed) wrote:
> unxl3arn3r <(E-Mail Removed)> 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 ?
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      03-25-2008
unxl3arn3r <(E-Mail Removed)> wrote:
> On Mar 24, 4:57 pm, (E-Mail Removed) wrote:
> > unxl3arn3r <(E-Mail Removed)> 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.
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      03-25-2008

Quoth http://www.velocityreviews.com/forums/(E-Mail Removed):
> unxl3arn3r <(E-Mail Removed)> wrote:
> > On Mar 24, 4:57 pm, (E-Mail Removed) wrote:
> > > unxl3arn3r <(E-Mail Removed)> 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

 
Reply With Quote
 
unxl3arn3r
Guest
Posts: n/a
 
      03-25-2008
On Mar 25, 12:51 pm, Ben Morrow <(E-Mail Removed)> wrote:
> Quoth (E-Mail Removed):
>
>
>
> > unxl3arn3r <(E-Mail Removed)> wrote:
> > > On Mar 24, 4:57 pm, (E-Mail Removed) wrote:
> > > > unxl3arn3r <(E-Mail Removed)> 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

This msgrcv function is getting to me..... Now that I got the length
in front of the string, I see the actual length. But if i send a
message greater than the size of SIZE value, the queue goes in a tizzy
and keeps trying to read the message again and over again. Whats the
point of having this length, If it can't do its job right.
 
Reply With Quote
 
unxl3arn3r
Guest
Posts: n/a
 
      03-25-2008
On Mar 25, 12:51 pm, Ben Morrow <(E-Mail Removed)> wrote:
> Quoth (E-Mail Removed):
>
>
>
> > unxl3arn3r <(E-Mail Removed)> wrote:
> > > On Mar 24, 4:57 pm, (E-Mail Removed) wrote:
> > > > unxl3arn3r <(E-Mail Removed)> 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


I forgot to put the code snippet in,
#!/usr/bin/perl
use strict;
use IPC::Msg;
use IPC::SysV;

#$outgoing=$ARGV[0];
my $key = 999;
my $queue = msgget($key,0) or die $!;
my $type = 1234;

#$queue = new IPC::Msg($key,0);
my $string = "This is a very long string The msgsnd docs are lying.
The second a
rgument of msgsnd should be the message, with a native long";
my $value = length ($string);
my $message1 = pack("l! a*",$value,$string);
print "one=$message1 \n";
msgsnd($queue,$message1,1);
print $! ;


receive
#!/usr/bin/perl

use IPC::SysV qw(IPC_RMID IPC_PRIVATE S_IRWXU IPC_CREAT
IPC_NOWAIT );
use IPC::Msg;

my $key = 999;
my $i = 0;
my $queue = msgget($key,&IPC_CREAT | 0777) or die $!;
my ($string, $i,$buffer,$type);

for ($i;$i < 15; $i++) {

if ( msgrcv($queue,$buffer,150,0,0) ) {

($type,$string)=unpack("l! a*",$buffer);
print $buffer .$string . "\n" . $type;
print "------------------------------\n";
} else {

print "Failed to read properly $! \n";
print "Length to be read = length($buffer) \n";
}
}
print "Now Removing\n";
msgctl($queue,IPC_RMID,0);
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      03-25-2008
unxl3arn3r <(E-Mail Removed)> wrote:
> On Mar 25, 12:51 pm, Ben Morrow <(E-Mail Removed)> wrote:
> >
> > 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

....
>
> my $value = length ($string);
> my $message1 = pack("l! a*",$value,$string);


As Ben just explained, you should be packing the message type,
not the message length. So the original code was correct, despite
the documentation mis-describing it.


>
> if ( msgrcv($queue,$buffer,150,0,0) ) {
>
> This msgrcv function is getting to me..... Now that I got the length
> in front of the string, I see the actual length.


Where is it that you are you seeing the actual length?

> But if i send a
> message greater than the size of SIZE value, the queue goes in a tizzy
> and keeps trying to read the message again and over again.


It fails because that is what the system call msgrcv is documented to do
when the message is too big and when (msgflg & MSG_NOERROR) is 0. It "goes
into a tizzy" because that is what you coded it to do by wrapping a bizarre
loop around it. If you want it to truncate the message rather than
fail, then pass MSG_NOERROR rather than 0 as the flag to msgrcv.


> Whats the
> point of having this length, If it can't do its job right.


It does its jobs right. You just don't understand what its job is.

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.
 
Reply With Quote
 
unxl3arn3r
Guest
Posts: n/a
 
      03-25-2008
On Mar 25, 4:58 pm, (E-Mail Removed) wrote:
> unxl3arn3r <(E-Mail Removed)> wrote:
> > On Mar 25, 12:51 pm, Ben Morrow <(E-Mail Removed)> wrote:

>
> > > 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

> ...
>
> > my $value = length ($string);
> > my $message1 = pack("l! a*",$value,$string);

>
> As Ben just explained, you should be packing the message type,
> not the message length. So the original code was correct, despite
> the documentation mis-describing it.
>
>
>
> > if ( msgrcv($queue,$buffer,150,0,0) ) {

>
> > This msgrcv function is getting to me..... Now that I got the length
> > in front of the string, I see the actual length.

>
> Where is it that you are you seeing the actual length?
>
> > But if i send a
> > message greater than the size of SIZE value, the queue goes in a tizzy
> > and keeps trying to read the message again and over again.

>
> It fails because that is what the system call msgrcv is documented to do
> when the message is too big and when (msgflg & MSG_NOERROR) is 0. It "goes
> into a tizzy" because that is what you coded it to do by wrapping a bizarre
> loop around it. If you want it to truncate the message rather than
> fail, then pass MSG_NOERROR rather than 0 as the flag to msgrcv.
>
> > Whats the
> > point of having this length, If it can't do its job right.

>
> It does its jobs right. You just don't understand what its job is.
>
> 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.


Things would be easy to understand if there was a explanation of the
what flags were meant to do what. Even docs are crap, so I can't be
expected to master the perl msgrcv function without taking any help. I
misunderstood pack and Ben both. Going back to read it again.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
When Is A Visaster Service Pack Not A Service Pack? Lawrence D'Oliveiro NZ Computing 5 06-26-2008 09:11 PM
Buy 1 pack or 3 pack? (XP Pro x64) Rich Olver Windows 64bit 11 11-25-2006 11:33 PM
Excellent discount software packs - ImTOO Ripper Pack Platinum and ImTOO Ripper Pack Gold zhangelf01@gmail.com DVD Video 6 09-17-2006 03:27 AM
Pack parent control in child using control.pack(in_= syntax? Tim Jones Python 0 01-31-2004 10:22 PM
Poor Mans NIMH Battery Pack and SLA Battery Pack ajacobs2 Digital Photography 0 08-19-2003 12:42 PM



Advertisments