[30148] in Perl-Users-Digest
Perl-Users Digest, Issue: 1391 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Mar 25 18:09:51 2008
Date: Tue, 25 Mar 2008 15:09:17 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Tue, 25 Mar 2008 Volume: 11 Number: 1391
Today's topics:
Output to file is not flushing immediately <void.no.spam.com@gmail.com>
Re: Output to file is not flushing immediately xhoster@gmail.com
Re: perl pack string xhoster@gmail.com
Re: perl pack string <ben@morrow.me.uk>
Re: perl pack string <jsahiwal@gmail.com>
Re: perl pack string <jsahiwal@gmail.com>
Re: perl pack string xhoster@gmail.com
Re: perl pack string <jsahiwal@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 25 Mar 2008 13:19:58 -0700 (PDT)
From: "void.no.spam.com@gmail.com" <void.no.spam.com@gmail.com>
Subject: Output to file is not flushing immediately
Message-Id: <8192621d-6ddf-48d5-8629-b037b3417b40@59g2000hsb.googlegroups.com>
I have a perl script that writes stuff to a text file. While the
script is running, I do a "tail -f" on the text file to see the
output. But I can tell that the output isn't being flushed
immediately, because the script writes something to the file
immediately before sleeping for 60 seconds, but I don't see that
output show up in the "tail -f" until after it comes back from
sleeping.
I thought that putting a "\n" at the end of a print would do a flush,
but that's obviously not the case.
I also read that if you put "$| = 1" at the beginning of your script,
then it would do a flush after every print or write command. I tried
that, and it still does not flush.
------------------------------
Date: 25 Mar 2008 20:38:04 GMT
From: xhoster@gmail.com
Subject: Re: Output to file is not flushing immediately
Message-Id: <20080325163807.164$QM@newsreader.com>
"void.no.spam.com@gmail.com" <void.no.spam.com@gmail.com> wrote:
> I have a perl script that writes stuff to a text file. While the
> script is running, I do a "tail -f" on the text file to see the
> output. But I can tell that the output isn't being flushed
> immediately, because the script writes something to the file
> immediately before sleeping for 60 seconds, but I don't see that
> output show up in the "tail -f" until after it comes back from
> sleeping.
>
> I thought that putting a "\n" at the end of a print would do a flush,
> but that's obviously not the case.
use IO::Handle;
#...
$fh->autoflush();
>
> I also read that if you put "$| = 1" at the beginning of your script,
> then it would do a flush after every print or write command. I tried
> that, and it still does not flush.
That will only turn autoflush on for the STDOUT (or whatever the currently
selected filed handle is).
Read all about it here:
perldoc -q flush
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.
------------------------------
Date: 25 Mar 2008 16:20:47 GMT
From: xhoster@gmail.com
Subject: Re: perl pack string
Message-Id: <20080325122049.102$tE@newsreader.com>
unxl3arn3r <jsahiwal@gmail.com> wrote:
> On Mar 24, 4:57 pm, xhos...@gmail.com wrote:
> > unxl3arn3r <jsahi...@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.
------------------------------
Date: Tue, 25 Mar 2008 16:51:30 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: perl pack string
Message-Id: <i5nnb5-j0c1.ln1@osiris.mauzo.dyndns.org>
Quoth xhoster@gmail.com:
> unxl3arn3r <jsahiwal@gmail.com> wrote:
> > On Mar 24, 4:57 pm, xhos...@gmail.com wrote:
> > > unxl3arn3r <jsahi...@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
------------------------------
Date: Tue, 25 Mar 2008 10:51:56 -0700 (PDT)
From: unxl3arn3r <jsahiwal@gmail.com>
Subject: Re: perl pack string
Message-Id: <68914ff7-dc87-40a3-81f2-eebfe01a2054@t54g2000hsg.googlegroups.com>
On Mar 25, 12:51 pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth xhos...@gmail.com:
>
>
>
> > unxl3arn3r <jsahi...@gmail.com> wrote:
> > > On Mar 24, 4:57 pm, xhos...@gmail.com wrote:
> > > > unxl3arn3r <jsahi...@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
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.
------------------------------
Date: Tue, 25 Mar 2008 10:53:05 -0700 (PDT)
From: unxl3arn3r <jsahiwal@gmail.com>
Subject: Re: perl pack string
Message-Id: <1a359ae4-38c9-4a10-a459-d3e1f7ed0105@2g2000hsn.googlegroups.com>
On Mar 25, 12:51 pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth xhos...@gmail.com:
>
>
>
> > unxl3arn3r <jsahi...@gmail.com> wrote:
> > > On Mar 24, 4:57 pm, xhos...@gmail.com wrote:
> > > > unxl3arn3r <jsahi...@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
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);
------------------------------
Date: 25 Mar 2008 20:58:35 GMT
From: xhoster@gmail.com
Subject: Re: perl pack string
Message-Id: <20080325165837.818$YX@newsreader.com>
unxl3arn3r <jsahiwal@gmail.com> wrote:
> On Mar 25, 12:51 pm, Ben Morrow <b...@morrow.me.uk> 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.
------------------------------
Date: Tue, 25 Mar 2008 14:08:02 -0700 (PDT)
From: unxl3arn3r <jsahiwal@gmail.com>
Subject: Re: perl pack string
Message-Id: <bfb19883-d5c0-4479-93ad-54e39b0152d3@8g2000hse.googlegroups.com>
On Mar 25, 4:58 pm, xhos...@gmail.com wrote:
> unxl3arn3r <jsahi...@gmail.com> wrote:
> > On Mar 25, 12:51 pm, Ben Morrow <b...@morrow.me.uk> 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.
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 1391
***************************************