[30634] in Perl-Users-Digest
Perl-Users Digest, Issue: 1879 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Sep 26 14:09:51 2008
Date: Fri, 26 Sep 2008 11:09:12 -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 Fri, 26 Sep 2008 Volume: 11 Number: 1879
Today's topics:
Re: expect.pm - stdout buffering issue <yandry77@gmail.com>
Re: expect.pm - stdout buffering issue <yandry77@gmail.com>
Re: expect.pm - stdout buffering issue <josef.moellers@fujitsu-siemens.com>
Re: How to unable the use of tainted mode in a CGI scri <azol@non-non-non>
Re: How to unable the use of tainted mode in a CGI scri <tim@burlyhost.com>
Re: IPC:Shareable <clauskick@hotmail.com>
Making a Method Work with Either Classes or Instances <sun_tong_001@users.sourceforge.net>
Re: Making a Method Work with Either Classes or Instanc <sun_tong_001@users.sourceforge.net>
Re: Making a Method Work with Either Classes or Instanc <ben@morrow.me.uk>
Re: Making a Method Work with Either Classes or Instanc <sun_tong_001@users.sourceforge.net>
news reader with perl scripting <anfi@onet.eu>
Object Oriented Perl questions <sun_tong_001@users.sourceforge.net>
Re: Object Oriented Perl questions <glennj@ncf.ca>
Re: Opening files on the web for reading <tzz@lifelogs.com>
Terminate the entire Perl script when control_C in an a <chen_zhitao@yahoo.com>
Re: Terminate the entire Perl script when control_C in <josef.moellers@fujitsu-siemens.com>
Re: Terminate the entire Perl script when control_C in xhoster@gmail.com
Re: Terminate the entire Perl script when control_C in xhoster@gmail.com
Re: Terminate the entire Perl script when control_C in <ben@morrow.me.uk>
Re: Words to numbers <tzz@lifelogs.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 26 Sep 2008 06:41:37 -0700 (PDT)
From: Andry <yandry77@gmail.com>
Subject: Re: expect.pm - stdout buffering issue
Message-Id: <25beded2-1606-4e79-b3e6-5e607119105a@8g2000hse.googlegroups.com>
On Sep 25, 6:38=A0pm, "J. Gleixner" <glex_no-s...@qwest-spam-no.invalid>
wrote:
> Andry wrote:
>
> [...]
>
> > Nevertheless, the 'sleep' commands still look out of sync. I'm
> > starting thinking that also some delayed answers from the ssh server
>
> No and there's no such thing as an ssh server.
>
> You're script is basically doing:
>
> print "Running ls\n";
> #send()
> my $out =3D `ls`;
>
> print "sleeping for 5\n";
> sleep 5;
>
> #expect(...)
> print "the output of ls: $out\n" if $out;
>
> Expect will run the command, then you tell it what you
> expect to happen and/or when that command is finished,
> usually by testing if the prompt is being displayed.
> At that point your command has finished running and
> you can get the output by using before(), or other
> methods. =A0Then you can sleep, or send another command
> and wait for the prompt. Rinse and repeat...
Hi guys,
Here is a better explanation of what I mentioned yesterday.
Based on all your previous suggestions and some intuitive attempts, I
found a way to get the script display the STDOUT correctly and log all
the output to the log_file, text-log.txt (playing with expect/sleep
commands). I also added a couple of partial logs ("ls -l" output into
text1-log.txt and "ls" output into text2-log.txt), teeing STDOUT.
Notice that, amazingly, if I remove any of the sleep commands I get
the wrong output collected by text1-log.txt/text2-log.txt.
Here is the script (please, read more after the script text below):
*************************************************************
#!/usr/bin/perl -w
use Expect;
use warnings;
use File::Tee qw(tee);
$Expect::Log_Stdout =3D 1;
$timeout =3D 5;
$|=3D1;
$exp =3D new Expect();
$exp->raw_pty(1);
$exp->log_user(1);
$exp->log_stdout(1);
$exp->log_file("text-log.txt", "w");
$exp->spawn("ssh -l root 10.17.39.29") or die "Cannot spawn ssh: $!
\n";
$exp->expect($timeout, ['-re', "[Pp]assword" =3D> sub {
$_[0]->send("XXXXXXXXX\n");
sleep 1;
} ]);
$exp->expect($timeout, "prompt-string");
sleep 2;
open(my $oldout, ">&STDOUT")
or die "Can't dup STDOUT; $!";
open my $target1, '>', 'text1-log.txt' or die $!;
tee STDOUT, $target1;
$exp->send("\n");
$exp->expect($timeout, "prompt-string");
$exp->send("\n");
$exp->expect($timeout, ['-ex', "prompt-string" =3D> sub {
$exp->send("ls -l\n");
$exp->expect($timeout, "prompt-string");
sleep 2;
} ] );
$exp->expect($timeout, "prompt-string");
close STDOUT;
open STDOUT, ">&", $oldout
or die "Can't dup \$oldout: $!";
open my $target2, '>', 'text2-log.txt' or die $!;
tee STDOUT, $target2;
$exp->send("\n");
$exp->expect($timeout, "prompt-string");
$exp->send("\n");
$exp->expect($timeout, ['-ex', "prompt-string" =3D> sub {
$exp->send("ls\n");
$exp->expect($timeout, "prompt-string") or die "couldn't see prompt
after 'send ls'\n";
sleep 1;
} ] );
$exp->expect($timeout, "prompt-string");
$exp->send("\n");
close STDOUT;
open STDOUT, ">&", $oldout
or die "Can't dup \$oldout: $!";
$exp->expect($timeout, ['-ex', "prompt-string" =3D> sub {
$exp->send("exit\n");
} ]);
$exp->log_file(undef);
*************************************************************
The problem is that: if you run the same script ten times, only 5 out
of 10 times you get the proper output captured by the proper partial
log file (either text1-log.txt or text2-log.txt).
The rest of the times you get the wrong output logged into text1-
log.txt/text2-log.txt: when this happens, usually text1-log.txt
contains only the few first lines, whereas text2-log.txt contains the
rest of "ls -l" output instead (and not "ls" output!).
The log_file=3Dtext-log.txt is always filled with all the expected
output instead.
Since the problem comes out randomly I suspect that the SSH command
response can affect the STDOUT I/O operations. Otherwise I don't see a
reason why the script should give different results when run multiple
times.
Any thought about the above explained issues?
Btw, J.Gleixner, do you think I get some better result using
exp_before() as you mentioned?
Thanks,
Andrea
------------------------------
Date: Fri, 26 Sep 2008 06:56:04 -0700 (PDT)
From: Andry <yandry77@gmail.com>
Subject: Re: expect.pm - stdout buffering issue
Message-Id: <3e0fdc08-50c0-4871-a144-7b8f439b6480@34g2000hsh.googlegroups.com>
On Sep 25, 6:38=A0pm, "J. Gleixner" <glex_no-s...@qwest-spam-no.invalid>
wrote:
> Andry wrote:
>
> [...]
>
> > Nevertheless, the 'sleep' commands still look out of sync. I'm
> > starting thinking that also some delayed answers from the ssh server
>
> No and there's no such thing as an ssh server.
>
> You're script is basically doing:
>
> print "Running ls\n";
> #send()
> my $out =3D `ls`;
>
> print "sleeping for 5\n";
> sleep 5;
>
> #expect(...)
> print "the output of ls: $out\n" if $out;
>
> Expect will run the command, then you tell it what you
> expect to happen and/or when that command is finished,
> usually by testing if the prompt is being displayed.
> At that point your command has finished running and
> you can get the output by using before(), or other
> methods. =A0Then you can sleep, or send another command
> and wait for the prompt. Rinse and repeat...
Hi guys,
Based on your suggestions and some intuitive attempts, I found a way
to display STDOUT correctly and, at the same time, get all the
expected output logged into log_file=3Dtext-log.txt.
I also added a couple of partial log files, teeing STDOUT, putting "ls
-l" output into text1-log.txt and "ls" into text2-log.txt.
Notice that if I remove any of the sleep commands the collected output
of text1-log.txt/text2-log.txt gets messed up.
Here is the script (please read more after the script below):
************************************************************************
#!/usr/bin/perl -w
use Expect;
use warnings;
use File::Tee qw(tee);
$Expect::Log_Stdout =3D 1;
$timeout =3D 5;
$|=3D1;
$exp =3D new Expect();
$exp->raw_pty(1);
$exp->log_user(1);
$exp->log_stdout(1);
$exp->log_file("text-log.txt", "w");
$exp->spawn("ssh -l root 10.17.39.29") or die "Cannot spawn ssh: $!
\n";
$exp->expect($timeout, ['-re', "[Pp]assword" =3D> sub {
$_[0]->send("XXXXXXXXX\n");
sleep 1;
} ]);
$exp->expect($timeout, "prompt-string");
sleep 2;
open(my $oldout, ">&STDOUT")
or die "Can't dup STDOUT; $!";
open my $target1, '>', 'text1-log.txt' or die $!;
tee STDOUT, $target1;
$exp->send("\n");
$exp->expect($timeout, "prompt-string");
$exp->send("\n");
$exp->expect($timeout, ['-ex', "prompt-string" =3D> sub {
$exp->send("ls -l\n");
$exp->expect($timeout, "prompt-string");
sleep 2;
} ] );
$exp->expect($timeout, "prompt-string");
close STDOUT;
open STDOUT, ">&", $oldout
or die "Can't dup \$oldout: $!";
open my $target2, '>', 'text2-log.txt' or die $!;
tee STDOUT, $target2;
$exp->send("\n");
$exp->expect($timeout, "prompt-string");
$exp->send("\n");
$exp->expect($timeout, ['-ex', "prompt-string" =3D> sub {
$exp->send("ls\n");
$exp->expect($timeout, "prompt-string") or die "couldn't see prompt
after 'send ls'\n";
sleep 1;
} ] );
$exp->expect($timeout, "prompt-string");
$exp->send("\n");
close STDOUT;
open STDOUT, ">&", $oldout
or die "Can't dup \$oldout: $!";
$exp->expect($timeout, ['-ex', "prompt-string" =3D> sub {
$exp->send("exit\n");
} ]);
$exp->log_file(undef);
************************************************************************
But the problem is that if I run the same script ten times, 5 out of
10 times the wrong output is captured by text1-log.txt/text2-log.txt.
That is, text1-log.txt only collects the first few lines of output
while text2-log.txt collects the rest of "ls -l" output (and not "ls"
output).
Since the problem occurs in a random way, I suspect that a kind of
jitter in SSH responses can affect STDOUT I/O operations of the
script. Otherwise I don't see a reason why the script should give
different results when run multiple times.
Any thought about the above explained issues?
Btw, J.Gleixner, do you think I can get some better result using the
exp_before() you mentioned?
Thanks,
Andrea
------------------------------
Date: Fri, 26 Sep 2008 15:57:07 +0200
From: Josef Moellers <josef.moellers@fujitsu-siemens.com>
Subject: Re: expect.pm - stdout buffering issue
Message-Id: <gbipni$aep$1@nntp.fujitsu-siemens.com>
Andry wrote:
> On Sep 25, 6:38 pm, "J. Gleixner" <glex_no-s...@qwest-spam-no.invalid>
> wrote:
>> Andry wrote:
>>
>> [...]
>>
>>> Nevertheless, the 'sleep' commands still look out of sync. I'm
>>> starting thinking that also some delayed answers from the ssh server
>> No and there's no such thing as an ssh server.
>>
>> You're script is basically doing:
>>
>> print "Running ls\n";
>> #send()
>> my $out = `ls`;
>>
>> print "sleeping for 5\n";
>> sleep 5;
>>
>> #expect(...)
>> print "the output of ls: $out\n" if $out;
>>
>> Expect will run the command, then you tell it what you
>> expect to happen and/or when that command is finished,
>> usually by testing if the prompt is being displayed.
>> At that point your command has finished running and
>> you can get the output by using before(), or other
>> methods. Then you can sleep, or send another command
>> and wait for the prompt. Rinse and repeat...
>
> Hi guys,
> Here is a better explanation of what I mentioned yesterday.
> Based on all your previous suggestions and some intuitive attempts, I
> found a way to get the script display the STDOUT correctly and log all
> the output to the log_file, text-log.txt (playing with expect/sleep
> commands). I also added a couple of partial logs ("ls -l" output into
> text1-log.txt and "ls" output into text2-log.txt), teeing STDOUT.
> Notice that, amazingly, if I remove any of the sleep commands I get
> the wrong output collected by text1-log.txt/text2-log.txt.
> Here is the script (please, read more after the script text below):
> *************************************************************
[ code removed ]
> *************************************************************
> The problem is that: if you run the same script ten times, only 5 out
> of 10 times you get the proper output captured by the proper partial
> log file (either text1-log.txt or text2-log.txt).
> The rest of the times you get the wrong output logged into text1-
> log.txt/text2-log.txt: when this happens, usually text1-log.txt
> contains only the few first lines, whereas text2-log.txt contains the
> rest of "ls -l" output instead (and not "ls" output!).
> The log_file=text-log.txt is always filled with all the expected
> output instead.
>
> Since the problem comes out randomly I suspect that the SSH command
> response can affect the STDOUT I/O operations. Otherwise I don't see a
> reason why the script should give different results when run multiple
> times.
>
> Any thought about the above explained issues?
>
> Btw, J.Gleixner, do you think I get some better result using
> exp_before() as you mentioned?
I tried this and I don't see any differences in the files other than
modification times and/or sizes of files affected by the logins.
Very strange,
Josef
--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html
------------------------------
Date: Fri, 26 Sep 2008 17:34:00 +0200
From: Azol <azol@non-non-non>
Subject: Re: How to unable the use of tainted mode in a CGI script ?
Message-Id: <MPG.23471f55f04628bf9897f0@news.free.fr>
In article <87d4ir2pc8.fsf@hacking.dk>, peter@makholm.net says...
> The access log and error log contains quite different information. So
> when asked for the error log there is only a very minimal chance that
> the access log contains the needed information.
>
Yes, but there's not any error.log in the log directory, so, maybe
access.log combinate acceses and errors in a same file here.
------------------------------
Date: Fri, 26 Sep 2008 10:37:26 -0700
From: Tim Greer <tim@burlyhost.com>
Subject: Re: How to unable the use of tainted mode in a CGI script ?
Message-Id: <r59Dk.75755$PK.65320@newsfe04.iad>
Azol wrote:
> In article <87d4ir2pc8.fsf@hacking.dk>, peter@makholm.net says...
>> The access log and error log contains quite different information. So
>> when asked for the error log there is only a very minimal chance that
>> the access log contains the needed information.
>>
>
> Yes, but there's not any error.log in the log directory, so, maybe
> access.log combinate acceses and errors in a same file here.
Where is your log directory? Are you looking for the error log in the
log directory for the web server? A lot of hosts have individual
directories for user's domain logs, or one central location, while the
actual error logs are usually still located in the standard log
location for the web service (wherever that might be, depending on
their configuration and type of service). Ask your host if you're
unsure.
By the way, that access log alone showed a 200 response, rather than a
500 response, so it made it appear as though, from the access log
standpoint anyway, that it's working (this is why you need to locate
and show the associated log entry from the error log, when you test the
script and have it fail). Did you ask your host to clarify how/why
Taint isn't working?
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
------------------------------
Date: Fri, 26 Sep 2008 06:28:53 -0700 (PDT)
From: Snorik <clauskick@hotmail.com>
Subject: Re: IPC:Shareable
Message-Id: <63c89b5f-edaa-495e-9554-dbed18e4ebc4@k7g2000hsd.googlegroups.com>
On Sep 24, 8:51=A0pm, Ted Zlatanov <t...@lifelogs.com> wrote:
> On Tue, 23 Sep 2008 07:42:26 -0700 (PDT) Snorik <clausk...@hotmail.com> w=
rote:
>
> S> On Sep 23, 3:27=A0pm, Ted Zlatanov <t...@lifelogs.com> wrote:
>
> >> This is not showing all your code;
> >> your initialization should have an ID for the shared memory segment
> >> you're using. =A0If you use the same ID, you will reuse the same space=
, so
> >> you don't have to destroy it if you'll be reusing it regularly.
>
> S> I just noticed that reusing it means that for a second run with other
> S> parameters, I also receive part of the result from the first one. So,
> S> I better not keep it :)
>
> S> For initialization, I just use the following:
>
> S> =A0my $ipc =3D tie %shared, 'Tie::ShareLite', -key=3D>1971, -mode=3D>0=
600, -
> S> create=3D>'yes', -destroy =3D>'no' or die("Could not tie to shared mem=
ory:
> S> $!")
Ted, sorry it took me some time to respond...
> Do this in the parent (the one which forks all the others) with
> -destroy =3D> 'yes'
I think I am doing this in the parent, that means before before the
fork() occurs (which should be the parent):
my $ipc =3D tie %shared, 'Tie::ShareLite', -key=3D>1971, -mode=3D>0600, -
create=3D>'yes', -destroy =3D>'yes' or die("Could not tie to shared
memory: $!");
use File::Slurp qw/read_dir/;
my @ggs =3D [... removed to shorten - it does return an array]
foreach my $gg (@ggs)
{
chomp $gg;
my $pid =3D fork();
if ($pid)
{
push(@children, $pid);
}
elsif ($pid =3D=3D 0)
{
use File::Find::Rule;
[here stuff happens...]
I receive this for each element of the array
IPC::ShareLite fetch() error: Invalid argument at [..]Tie/ShareLite.pm
line 342
To me that says that the object is already destroyed?
*snip useful advice about child processes*
Thank you for that gem of useful information!
------------------------------
Date: Fri, 26 Sep 2008 10:44:42 -0500
From: * Tong * <sun_tong_001@users.sourceforge.net>
Subject: Making a Method Work with Either Classes or Instances
Message-Id: <paadnZoRDdv3nkDVnZ2dnUVZ_hGdnZ2d@golden.net>
Hi,
First of all, thanks a lot to Joost Diepenmaat for giving a comprehensive
answer to my previous class & method name question.
I am following the book "Intermediate Perl". In section 12.6, "Making a
Method Work with Either Classes or Instances", it has:
sub name {
my $either = shift;
ref $either
? $$either # it's an instance, return name
: "an unnamed $either"; # it's a class, return generic
}
but this stops working (with errror "Not a SCALAR reference") after I
change the constructor from blessing the single animal name to bless a
hash:
sub named {
ref( my $class = shift ) and croak 'class only';
my $name = shift;
my $self = { Name => $name, Color => $class->default_color };
bless $self, $class;
}
How can I fix it?
Thanks
tong
------------------------------
Date: Fri, 26 Sep 2008 10:48:45 -0500
From: * Tong * <sun_tong_001@users.sourceforge.net>
Subject: Re: Making a Method Work with Either Classes or Instances
Message-Id: <paadnZURDdvAmUDVnZ2dnUVZ_hGdnZ2d@golden.net>
Sorry, asked too soon.
On Fri, 26 Sep 2008 10:44:42 -0500, * Tong * wrote:
> I am following the book "Intermediate Perl". In section 12.6, "Making a
> Method Work with Either Classes or Instances", it has:
>
> sub name {
> my $either = shift;
> ref $either
> ? $$either # it's an instance, return name : "an
> unnamed $either"; # it's a class, return generic
> }
>
> but this stops working (with errror "Not a SCALAR reference") after I
> change the constructor from blessing the single animal name to bless a
> hash:
sub name {
my $either = shift;
ref $either
? $either->{Name}
: "an unnamed $either";
}
------------------------------
Date: Fri, 26 Sep 2008 16:54:11 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Making a Method Work with Either Classes or Instances
Message-Id: <36dvq5-0v2.ln1@osiris.mauzo.dyndns.org>
Quoth * Tong * <sun_tong_001@users.sourceforge.net>:
> Hi,
>
> First of all, thanks a lot to Joost Diepenmaat for giving a comprehensive
> answer to my previous class & method name question.
>
> I am following the book "Intermediate Perl". In section 12.6, "Making a
> Method Work with Either Classes or Instances", it has:
>
> sub name {
> my $either = shift;
> ref $either
> ? $$either # it's an instance, return name
Dereferencing the object with the $ operator is assuming the object will
be a ref to a scalar.
> : "an unnamed $either"; # it's a class, return generic
> }
>
> but this stops working (with errror "Not a SCALAR reference") after I
> change the constructor from blessing the single animal name to bless a
> hash:
>
> sub named {
> ref( my $class = shift ) and croak 'class only';
> my $name = shift;
> my $self = { Name => $name, Color => $class->default_color };
> bless $self, $class;
> }
>
> How can I fix it?
In this case, you want something like
sub name {
my $either = shift;
ref $either
? $either->{Name}
: "an unnamed $either";
}
using the correct deref operation for the type of the object.
Ben
--
#!/bin/sh
quine="echo 'eval \$quine' >> \$0; echo quined"
eval $quine
# [ben@morrow.me.uk]
------------------------------
Date: Fri, 26 Sep 2008 11:08:21 -0500
From: * Tong * <sun_tong_001@users.sourceforge.net>
Subject: Re: Making a Method Work with Either Classes or Instances
Message-Id: <paadnZQRDdtolUDVnZ2dnUVZ_hGdnZ2d@golden.net>
On Fri, 26 Sep 2008 16:54:11 +0100, Ben Morrow wrote:
>> sub name {
>> my $either = shift;
>> ref $either
>> ? $$either # it's an instance, return name
>
> Dereferencing the object with the $ operator is assuming the object will
> be a ref to a scalar.
Thanks a lot Ben. The error reports on the line "ref $either" and I was
silly enough not looking at the next line. :-)
cheers
tong
------------------------------
Date: Fri, 26 Sep 2008 14:23:58 +0200
From: Andrzej Adam Filip <anfi@onet.eu>
Subject: news reader with perl scripting
Message-Id: <l36fftw878@kurt.strange.twilightparadox.com>
Which newsreader would you recommend for perl scripting?
I would like to be able to:
a) rewrite outgoing posts
b) rewrite incoming posts
e.g. special marking for *verified* PGP/GPG signed messages
--
[pl>en Andrew] Andrzej Adam Filip : anfi@onet.eu : anfi@xl.wp.pl
A baby is God's opinion that the world should go on.
-- Carl Sandburg
------------------------------
Date: Fri, 26 Sep 2008 12:09:05 -0500
From: * Tong * <sun_tong_001@users.sourceforge.net>
Subject: Object Oriented Perl questions
Message-Id: <GqqdnXlfxb2sikDVnZ2dnUVZ_r_inZ2d@golden.net>
Hi,
Please take a look at the following program:
$ cat -n oo-test.pl
1 #!/usr/bin/env perl
2
3 package Animal; {
4
5 sub name {
6 my $either = shift;
7 ref $either
8 ? $either->{Name} # it's an instance, return name
9 : "an unnamed $either"; # it's a class, return
generic
10 }
11
12 our %REGISTRY;
13
14 sub registered {
15 return map { 'a ' . ref($_) . " named "
16 . (ref($_) ? $_->name : "" ) } values %
REGISTRY;
17 }
18
19 sub empty_all {
20 $REGISTRY = undef;
21 }
22
23 use Carp qw(croak);
24 use Scalar::Util qw(weaken); # in 5.8 and later
25 use WeakRef qw(weaken); # in 5.6 after CPAN installation
26
27 sub named {
28 ref( my $class = shift ) and croak 'class only';
29 my $name = shift;
30 my $self = { Name => $name, Color => "default_color" };
31 bless $self, $class;
32 $REGISTRY{$self} = $self;
33 WeakRef::weaken( $REGISTRY{$self} );
34 $self;
35 }
36 }
37
38 package Horse; {
39 use base qw(Animal);
40 }
41
42 package main;
43
44 my $tv_horse = Horse->named('Mr. Ed0');
45 print $tv_horse->name, "\n"; # prints "Mr. Ed.\n"
46 $tv_horse = undef;
47
48 Animal->empty_all;
49
50 my @horses = map Horse->named($_), ( 'Trigger', 'Mr. Ed' );
51 print "alive before block:\n", map( " $_\n", Animal-
>registered );
52 {
53 my @racehorses = Horse->named('Billy Boy');
54 print "alive inside block:\n", map( " $_\n", Animal-
>registered );
55 }
56 print "alive after block:\n", map( " $_\n", Animal->registered );
57 print "End of program.\n";
It runs like this:
$ oo-test.pl
Prototype mismatch: sub Animal::weaken ($) vs none at /usr/share/
perl/5.10/Exporter.pm line 66.
at ./oo-test.pl line 25
Mr. Ed0
alive before block:
a Horse named Trigger
a Horse named Mr. Ed
a named
alive inside block:
a Horse named Trigger
a Horse named Billy Boy
a Horse named Mr. Ed
a named
alive after block:
a Horse named Trigger
a named
a Horse named Mr. Ed
a named
End of program.
My questions are:
- how can I fix the "Prototype mismatch" error?
- I think I didn't do it right in "empty_all", how can I fix it?
- The "empty_all" is related with the erroneous "a named " entry in
"alive before" -- I think, or is it?
- How can I properly unregister each animal?
I tried to define sub DESTROY in package Animal as:
sub DESTROY {
my $self = shift;
$REGISTRY{$self} = undef;
print '[', $self->name, " has died.]\n";
}
but that doesn't seem to work.
Please help (whole program available at http://pastebin.com/m3c32b348)
thanks
--
tong
------------------------------
Date: 26 Sep 2008 17:53:01 GMT
From: Glenn Jackman <glennj@ncf.ca>
Subject: Re: Object Oriented Perl questions
Message-Id: <slrngdq8bu.osf.glennj@smeagol.ncf.ca>
At 2008-09-26 01:09PM, "* Tong *" wrote:
[...]
> 12 our %REGISTRY;
[...]
> 19 sub empty_all {
> 20 $REGISTRY = undef;
You mean: %REGISTRY = ();
> I tried to define sub DESTROY in package Animal as:
>
> sub DESTROY {
> my $self = shift;
> $REGISTRY{$self} = undef;
You mean: delete $REGISTRY{$self};
--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous
------------------------------
Date: Fri, 26 Sep 2008 09:55:49 -0500
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: Opening files on the web for reading
Message-Id: <8663oj7y8a.fsf@lifelogs.com>
On Fri, 26 Sep 2008 02:55:49 +0100 Ben Morrow <ben@morrow.me.uk> wrote:
BM> Quoth Eric Pozharski <whynot@pozharski.name>:
>> Ted Zlatanov <tzz@lifelogs.com> wrote:
>> *SKIP*
>> > Unfortunately, the docs say "The bad news is that the whole file is
>> > stored in memory after getting it or before putting it. This may cause
>> > problems if you are dealing with multi-gigabyte files!"
>>
>> > It would be nice to have a buffered reader/writer which wouldn't grab
>> > the whole file, using the LWP callbacks, as xhoster suggests... I
>> > haven't seen such a module.
>>
>> Obviously I've got something wrong (or, as ever, I'm incompetent). The
>> server must have means to be told stop-feeding/resume-feeding.
BM> Yes. See
BM> http://en.wikipedia.org/wiki/Transmission_Control_Protocol#Flow_control
BM> .
>> Or (in
>> case I understand networking a least bit) those gigabytes would be
>> buffered in kernel.
BM> Once the kernel buffers are full, the receiving end instructs the
BM> sending end to stop sending data.
Also, HTTP 1.1 supports partial transfers of data, so you can open a
persistent connection and keep requesting small pieces. I'd guess it's
better that TCP flow control if the goal was to allow random seeks, not
just sequential writes. Handling errors and chunk boundaries would
be... let's say "interesting to the right developer." :)
Ted
------------------------------
Date: Fri, 26 Sep 2008 05:27:21 -0700 (PDT)
From: Kuhl <chen_zhitao@yahoo.com>
Subject: Terminate the entire Perl script when control_C in an application
Message-Id: <b2e18321-5ce4-4595-9144-624b37ebe1f9@o40g2000prn.googlegroups.com>
Hi, all:
I wrote a Perl script. It invokes an application at some steps. If the
application is running or is waiting in the license queue, then
Control_C would terminate the application. But it does not terminate
the Perl script. The script continues to run following steps. However,
such behavior causes errors finally. So I want to terminate not only
the application when I do Control_C, but want to terminate the entire
Perl script. I wish that the script continues only if the application
finishes in the normal way. How to achieve this goal?
Thank you in advance.
------------------------------
Date: Fri, 26 Sep 2008 15:36:09 +0200
From: Josef Moellers <josef.moellers@fujitsu-siemens.com>
Subject: Re: Terminate the entire Perl script when control_C in an application
Message-Id: <gbiog8$4qa$3@nntp.fujitsu-siemens.com>
Kuhl wrote:
> Hi, all:
>
> I wrote a Perl script. It invokes an application at some steps. If the
> application is running or is waiting in the license queue, then
> Control_C would terminate the application. But it does not terminate
> the Perl script. The script continues to run following steps. However,
> such behavior causes errors finally. So I want to terminate not only
> the application when I do Control_C, but want to terminate the entire
> Perl script. I wish that the script continues only if the application
> finishes in the normal way. How to achieve this goal?
>
> Thank you in advance.
This is probably OS dependent, as on Linux, upon a Ctrl-C a SIGINT is
sent to all processes that have the current terminal as their
controlling tty. Thus the application *and* the Perl script will get killed.
This may, however, be different on other OSes.
--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html
------------------------------
Date: 26 Sep 2008 15:56:11 GMT
From: xhoster@gmail.com
Subject: Re: Terminate the entire Perl script when control_C in an application
Message-Id: <20080926115613.035$dP@newsreader.com>
Kuhl <chen_zhitao@yahoo.com> wrote:
> Hi, all:
>
> I wrote a Perl script. It invokes an application at some steps. If the
> application is running or is waiting in the license queue, then
> Control_C would terminate the application. But it does not terminate
> the Perl script.
Check the exit status of the application. If it exited due to a signal
(or whatever other criterion), then have Perl exit.
system "./application" and die "application ended abnormal $! $?";
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: 26 Sep 2008 15:58:41 GMT
From: xhoster@gmail.com
Subject: Re: Terminate the entire Perl script when control_C in an application
Message-Id: <20080926115842.797$GW@newsreader.com>
Josef Moellers <josef.moellers@fujitsu-siemens.com> wrote:
> Kuhl wrote:
> > Hi, all:
> >
> > I wrote a Perl script. It invokes an application at some steps. If the
> > application is running or is waiting in the license queue, then
> > Control_C would terminate the application. But it does not terminate
> > the Perl script. The script continues to run following steps. However,
> > such behavior causes errors finally. So I want to terminate not only
> > the application when I do Control_C, but want to terminate the entire
> > Perl script. I wish that the script continues only if the application
> > finishes in the normal way. How to achieve this goal?
> >
> > Thank you in advance.
>
> This is probably OS dependent, as on Linux, upon a Ctrl-C a SIGINT is
> sent to all processes that have the current terminal as their
> controlling tty. Thus the application *and* the Perl script will get
> killed.
The Perl script will get signaled. However, perl might arrange to ignore
those signals. That is what "system" does, anyway.
perldoc -f system
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: Fri, 26 Sep 2008 16:49:28 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Terminate the entire Perl script when control_C in an application
Message-Id: <8tcvq5-0v2.ln1@osiris.mauzo.dyndns.org>
Quoth Josef Moellers <josef.moellers@fujitsu-siemens.com>:
> Kuhl wrote:
> >
> > I wrote a Perl script. It invokes an application at some steps. If the
> > application is running or is waiting in the license queue, then
> > Control_C would terminate the application. But it does not terminate
> > the Perl script. The script continues to run following steps. However,
> > such behavior causes errors finally. So I want to terminate not only
> > the application when I do Control_C, but want to terminate the entire
> > Perl script. I wish that the script continues only if the application
> > finishes in the normal way. How to achieve this goal?
How are you invoking the program? Using system? If so, you can check $?
to see if the program exitted on SIGINT, using the WIFSIGNALED and
WTERMSIG functions from the POSIX module.
> > Thank you in advance.
>
> This is probably OS dependent, as on Linux, upon a Ctrl-C a SIGINT is
> sent to all processes that have the current terminal as their
> controlling tty. Thus the application *and* the Perl script will get killed.
*However*, system explicitly ignores SIGINT for the duration of the
external command, so that perl isn't killed by a ^C. If you don't want
that, either fork/exec/wait yourself or check the exit status.
Ben
--
BEGIN{*(=sub{$,=*)=sub{print@_};local($#,$;,$/)=@_;for(keys%{ #ben@morrow.me.uk
$#}){/m/&&next;**=${$#}{$_};/(\w):/&&(&(($#.$_,$;.$+,$/),next);$/==\$*&&&)($;.$
_)}};*_=sub{for(@_){$|=(!$|||$_||&)(q) )));&((q:\:\::,q,,,\$_);$_&&&)("\n")}}}_
$J::u::s::t, $a::n::o::t::h::e::r, $P::e::r::l, $h::a::c::k::e::r, $,
------------------------------
Date: Fri, 26 Sep 2008 10:02:09 -0500
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: Words to numbers
Message-Id: <861vz77xxq.fsf@lifelogs.com>
On Thu, 25 Sep 2008 17:51:48 -0700 Jim Gibson <jimsgibson@gmail.com> wrote:
JG> In article
JG> <820d8d96-2839-45ed-8ca9-1bed871bb5f0@k37g2000hsf.googlegroups.com>,
JG> william <huxiankui@gmail.com> wrote:
>> I'm writing perl scripts to retrieve data from email messages. Here
>> are two .txt files.
>> ACNI050124_05_04_59.txt
>>
>> received fifteen thousand dollars ...
>>
>> ZLDV060318_19_32_11.txt
>> We have received one hundred thirty five thousand ...
>> I want to achieve the following output to an excel table.
>>
>> filename
>> dollars shares
>> ACNI050124_05_04_59.txt 15000 -9
>> ZLDV060318_19_32_11.txt -9 135000
>>
>> -9 simply means that we don't find any information related to shares
>> or dollars in the file.
(the comments are for the OP mainly)
Have you considered empty fields instead of special values to denote
absence of value? Specifically, you may need negative numbers for
shares later if you want to indicate buy/sell modes.
>>
>> It seems to be a simple task at first. But I realize that it is quite
>> complicated when I start to write the script. Any suggestions from you
>> will be highly appreciated.
JG> It doesn't seem simple at all. You are trying to parse free-form
JG> English written by various people and extract numerical data from
JG> alphabetic number names. My suggestion is to give it up before you
JG> start.
It's not impossible, and certainly it's interesting. Perhaps
http://web.media.mit.edu/~hugo/montylingua/ will be useful; it has Java
and Python interfaces and a Perl interface may be doable. At the very
least you can parse the montylingua analyzer output.
Ted
------------------------------
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 1879
***************************************