[29557] in Perl-Users-Digest
Perl-Users Digest, Issue: 801 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Aug 27 21:09:41 2007
Date: Mon, 27 Aug 2007 18:09:07 -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 Mon, 27 Aug 2007 Volume: 11 Number: 801
Today's topics:
Re: 2 issues with "tie" <ben@morrow.me.uk>
Re: best practice ... requires <ben@morrow.me.uk>
comparing two arrays in perl <bhooshan.dixit@gmail.com>
Re: comparing two arrays in perl <peter@makholm.net>
Re: comparing two arrays in perl <mritty@gmail.com>
Re: comparing two arrays in perl <jurgenex@hotmail.com>
Re: comparing two arrays in perl <mritty@gmail.com>
fork off several children processes <rsarpi@gmail.com>
Re: fork off several children processes xhoster@gmail.com
Re: fork off several children processes <rsarpi@gmail.com>
GD install problems <fraser.tim@gmail.com>
Re: GD install problems <ben@morrow.me.uk>
Re: How to make this case insenstive <rvtol+news@isolution.nl>
Re: SCP/RCP stats ? <ben@morrow.me.uk>
Re: Tk::getSaveFile - use specified initialfile <ben@morrow.me.uk>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 27 Aug 2007 23:40:49 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: 2 issues with "tie"
Message-Id: <hg0cq4-k9r.ln1@osiris.mauzo.dyndns.org>
Quoth "comp.llang.perl.moderated" <ced@blv-sam-01.ca.boeing.com>:
> On Aug 26, 6:32 pm, Tuc <tuct...@gmail.com> wrote:
>
> > I seem to be running into some issues with tie in perl 5.8.8 with the
> > defined-or patch from the FreeBSD ports.
> >
> > I use :
> > use NDBM_File;
> > use Fcntl;
> >
> > I open up my file as :
> > tie (%fdb,'NDBM_File',"file.victims",O_RDWR|O_CREAT,0777) ||die $!;
> >
> > I write to it as
> > $fdb{$_}="TUC";
> >
> > I close it as :
> > untie %fdb;
> >
> > Seems pretty basic. But I've got 2 issues.
> >
> > 1) If $_="Fred", and the program iterates and does
> > $fdb{'Fred'}="TUC"; , if I immediately start another program up to
> > read "Fred" from the file, it claims it doesn't exist. As if it hasn't
> > sync'd. As soon as I do the "untie %fdb;", then the data becomes
> > available. Is there a way that as soon as I $fdb{'Fred'}="TUC"; it
> > becomes accessible?
>
> NDBM doesn't appear to have a 'sync' method to
> force a flush to disk. I don't know if there's
> a convenient workaround so, alternatively, you
> may want to consider using DB_File which does
> provide a 'sync'.
>
> DB_File also has other advantages and doesn't
> have have NDBM's key,value length max.
Under FreeBSD, <ndbm.h> is in fact implemented with <db.h> anyway, so
switching to DB_File should leave you still able to read your old
databases.
Ben
--
"Awww, I'm going to miss her."
"Don't you hate her?"
"Yes, with a fiery vengeance."
[ben@morrow.me.uk]
------------------------------
Date: Mon, 27 Aug 2007 15:18:49 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: best practice ... requires
Message-Id: <933bq4-e14.ln1@osiris.mauzo.dyndns.org>
Quoth mike <hillmw@charter.net>:
> Ok, so i decided to use packages.
>
> my scripts would look something like:
>
> #!/usr/bin/perl -w
Don't use -w, use
use warnings;
You need
use strict;
here as well. Have you seen the posting guidelines?
> #
> # name of this program is: main.cgi
> #
> use CGI;
> use pkg_a;
Packages with all lowercase names are reserved for pragmas (modules that
affect the way Perl parses your program, like strict and warnings).
Also, you should give your modules descriptive names (although I presume
this is just an example).
use PkgA;
> use pkg_b;
> my $x = &pkg_a::do_this();
Do you know what calling a sub with & does? I thought not. So don't do
it. You can avoid needing to call subs by their fully-qualified name by
using Exporter: see perldoc Exporter.
> #!/usr/bin/perl -w
> #
> # name of this program is: pkg_a.pm
> #
> package pkg_a;
You need
use strict;
use warnings;
here as well. They only apply to the file in which they appear. (Yes,
this is annoying :).)
>
> sub do_this()
Don't use prototypes unless you know what they do: Perl is not
Javascript.
sub do_this {
(Also, as a style issue, it is usual in Perl code to put the { on the
end of the line... it's not terribly important, but it can confuse
people.)
> {
> &pkg_b::do_that();
> }
>
> so you can see that my package is nested and I am now wondering how
> correctly to do this. You can see that I am calling a package from
> inside a package. I dont know if that is legal or not.
Your packages are not 'nested'. You simply have three packages:
main, defined in main.cgi;
pkg_a, defined in pkg_a.pm;
pkg_b, defined in pkg_b.pm.
Calling a function in pkg_b from pkg_a is no different from calling a
function in pkg_a from main. In order to keep pkg_a self-contained, you
should have a
use pkg_b;
at the top of pkg_a.pm as well: Perl will not load it more than once.
Then, when you've learned to use Exporter, you can import some subs into
pkg_a and some into main, and everything will be much neater :).
> I am getting this error message: Illegal character in prototype
No, you're not; well, not from that code, anyway. The 'prototype' is
this bit:
sub do_this()
^^
which isn't anything like the formal argument list other languages might
have here. I suspect your real code has something like
sub do_this($param)
which is not valid Perl. Simply omit the parens until you understand how
prototypes work in Perl.
Ben
--
For far more marvellous is the truth than any artists of the past imagined!
Why do the poets of the present not speak of it? What men are poets who can
speak of Jupiter if he were like a man, but if he is an immense spinning
sphere of methane and ammonia must be silent? [Feynmann] ben@morrow.me.uk
------------------------------
Date: Mon, 27 Aug 2007 18:17:23 -0000
From: bcdixit <bhooshan.dixit@gmail.com>
Subject: comparing two arrays in perl
Message-Id: <1188238643.878620.116330@r34g2000hsd.googlegroups.com>
Hi,
I have two arrays, for example,
@array = ("john","larry","kevin","peter","mike")
@exclude = ("john","kevin")
i want to delete from @array , values that exist in @exclude.
i.e. i want my @array to look like
@array("","larry","","peter","mike")
i have written this code but it doesnot seem to work.
foreach$i(0..$#exclude)
{
foreach$x(0..$#array)
{
if($array[$x] eq $exclude[$i])
{
delete($array[$x]);
next;
}
}
}
am I missing something? is there someother better way to do it in
perl?
thanks
-bd
------------------------------
Date: Mon, 27 Aug 2007 18:24:35 +0000
From: Peter Makholm <peter@makholm.net>
Subject: Re: comparing two arrays in perl
Message-Id: <87abscal98.fsf@hacking.dk>
bcdixit <bhooshan.dixit@gmail.com> writes:
> @array = ("john","larry","kevin","peter","mike")
> @exclude = ("john","kevin")
>
> i want to delete from @array , values that exist in @exclude.
> i.e. i want my @array to look like
What you need is an easy way to check if an value is in @exclude. This
can be done by converting it to a hash where easch element from
@exclude is an key with an true value:
This can be done this way
%exclude = map { $_ => 1 } @exclude;
or this way:
$exclude{$_} = 1 for @exclude;
(and probbaly many other ways).
Then you just need to iterate over @array one time.
//Makholm
------------------------------
Date: Mon, 27 Aug 2007 11:41:08 -0700
From: Paul Lalli <mritty@gmail.com>
Subject: Re: comparing two arrays in perl
Message-Id: <1188240068.061601.222240@y42g2000hsy.googlegroups.com>
On Aug 27, 2:17 pm, bcdixit <bhooshan.di...@gmail.com> wrote:
> I have two arrays, for example,
>
> @array = ("john","larry","kevin","peter","mike")
> @exclude = ("john","kevin")
>
> i want to delete from @array , values that exist in @exclude.
Your Question (or at least a variant of it) is Frequently Asked...
$ perldoc -q "difference of two arrays"
Found in /opt/perl/lib/5.6.1/pod/perlfaq4.pod
How do I compute the difference of two arrays? How do I
compute the intersection of two arrays?
> i.e. i want my @array to look like
>
> @array("","larry","","peter","mike")
>
> i have written this code but it doesnot seem to work.
Does not work is the worst possible error description. *HOW* does it
not work? What results does it produce that you did not expect?
>
> foreach$i(0..$#exclude)
> {
> foreach$x(0..$#array)
> {
> if($array[$x] eq $exclude[$i])
> {
> delete($array[$x]);
delete(), when used on array elements, will undefine internal
elements, and will remove the last element. This is generally not a
good thing, and also doesn't match your desired output. I think you
instead want:
$array[$x] = "";
> next;
"next" being the last statement in a block is a no-op. It simply says
to go up to the beginning of the current loop and start the next
iteration. That's what would happen anyway. I think you meant "last"
here.
> }
> }
> }
>
> am I missing something?
You tell me. The code (after being modified as I suggest) does
exactly what you say you want. If you disagree, post a short-but-
complete script with desired output and actual output.
> is there someother better way to do it in perl?
Yes, use hashes, as Peter and the FAQ both suggested.
Paul Lalli
------------------------------
Date: Mon, 27 Aug 2007 18:55:48 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: comparing two arrays in perl
Message-Id: <U6FAi.4380$Ay3.702@trndny02>
bcdixit wrote:
> I have two arrays, for example,
>
> @array = ("john","larry","kevin","peter","mike")
> @exclude = ("john","kevin")
>
> i want to delete from @array , values that exist in @exclude.
> i.e. i want my @array to look like
>
> @array("","larry","","peter","mike")
>
> i have written this code but it doesnot seem to work.
>
> foreach$i(0..$#exclude) {
> foreach$x(0..$#array) {
> if($array[$x] eq $exclude[$i]){
> delete($array[$x]);
[...]
> am I missing something?
Yes. It is very bad to change the array (add or remove elements) while
looping through it using foreach().
> is there someother better way to do it in perl?
Like explained in the FAQ: "How do I compute the difference of two arrays?
How do I compute the intersection of two arrays?"
jue
------------------------------
Date: Mon, 27 Aug 2007 12:26:13 -0700
From: Paul Lalli <mritty@gmail.com>
Subject: Re: comparing two arrays in perl
Message-Id: <1188242773.183560.174220@r29g2000hsg.googlegroups.com>
On Aug 27, 2:55 pm, "J=FCrgen Exner" <jurge...@hotmail.com> wrote:
> bcdixit wrote:
> > foreach$i(0..$#exclude) {
> > foreach$x(0..$#array) {
> > if($array[$x] eq $exclude[$i]){
> > delete($array[$x]);
> [...]
> > am I missing something?
>
> Yes. It is very bad to change the array (add or remove elements)\
> while looping through it using foreach().
The only element that could be deleted from that array is the last
one. I don't see how that could affect anything, as it would also
therefore be the last iteration of the loop.
Paul Lalli
------------------------------
Date: Mon, 27 Aug 2007 21:36:48 -0000
From: monk <rsarpi@gmail.com>
Subject: fork off several children processes
Message-Id: <1188250608.410756.23300@e9g2000prf.googlegroups.com>
Hi all, How can I fork off several children processes? sorry for the
dumb newbie question.
With this solution below, parent1() executes in parallel with
child1(), then parent2() executes in parallel with child2(), and so
on.
What I want is to have something like all parentX() subroutines
executing along with all childrenX() subroutines, all in parallel, all
together like a good family.
Is that possible?
This is what I have:
if ($pid = fork) {
print "processing parent subroutines\n";
parent1();
parent2();
parent3();
}
elsif (defined $pid) {
print "processing children subroutines\n";
child1();
child2();
child3();
exit(0);
}
else {
print "couldn't fork: $!\n";
}
#wait for children
waitpid($pid, 0);
print "Done with parent and children! \n";
exit 0;
------------------------------
Date: 27 Aug 2007 22:26:15 GMT
From: xhoster@gmail.com
Subject: Re: fork off several children processes
Message-Id: <20070827182618.121$pX@newsreader.com>
monk <rsarpi@gmail.com> wrote:
> Hi all, How can I fork off several children processes? sorry for the
> dumb newbie question.
>
> With this solution below, parent1() executes in parallel with
> child1(), then parent2() executes in parallel with child2(), and so
> on.
From what you have shown, this should only be the case if parent1() and
child1() take the same time to execute, parent2 and child2 take the
same time to execute, etc. Once parent1() finishes, it goes on to
parent2() regardless of what the other process is doing.
> What I want is to have something like all parentX() subroutines
> executing along with all childrenX() subroutines, all in parallel, all
> together like a good family.
I don't understand what you want. It sounds like you want to make
all the processes siblings. If that is the case, why do you name
some of them parents and some of them children?
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: Mon, 27 Aug 2007 16:18:21 -0700
From: monk <rsarpi@gmail.com>
Subject: Re: fork off several children processes
Message-Id: <1188256701.670005.227220@e9g2000prf.googlegroups.com>
> > What I want is to have something like all parentX() subroutines
> > executing along with all childrenX() subroutines, all in parallel, all
> > together like a good family.
>
> I don't understand what you want. It sounds like you want to make
> all the processes siblings. If that is the case, why do you name
> some of them parents and some of them children?
Thanks for your reply.
So should I have only *one* subroutine under the parent section,
and the rest of the subroutines under the child section?
Would that accomplish the goal of running several children in
parallel?
------------------------------
Date: Mon, 27 Aug 2007 19:37:41 -0000
From: simulant <fraser.tim@gmail.com>
Subject: GD install problems
Message-Id: <1188243461.220512.231020@22g2000hsm.googlegroups.com>
Environment:
AIX 5.2
perl 5.8.2
gcc 3.3.2
libgd 2.0.33
libpng 1.2.8
zlib - 1.2.3
Trying to install GD 2.35 on AIX 5.2 and receiving the following error
while running make:
cc_r -c -I/usr/local/pkgs/gd.2.0.33/include -I/usr/include -I/
usr/include/gd -D_ALL_SOURCE -D_ANSI_C_SOURCE -D_POSIX_SOURCE -
qmaxmem=16384 -qnoansialias -DUSE_NATIVE_DLOPEN -DNEED_PTHREAD_INIT -
q32 -D_LARGE_FILES -qlonglong -O -DVERSION=\"2.35\" -DXS_VERSION=
\"2.35\" "-I/usr/opt/perl5/lib/5.8.2/aix-thread-multi/CORE" -
DHAVE_PNG GD.c
/bin/sh: cc_r: not found.
make: 1254-004 The error code from the last command is 127.
Now I know the problem here is that I am not running cc_r ... I'm only
running gcc. But is there a way around this without having to put cc_r
on the server? Is it possible to either bypass this cc_r requirement
or get it to work with gcc somehow?
------------------------------
Date: Tue, 28 Aug 2007 00:40:17 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: GD install problems
Message-Id: <104cq4-q3c1.ln1@osiris.mauzo.dyndns.org>
Quoth simulant <fraser.tim@gmail.com>:
> Environment:
> AIX 5.2
> perl 5.8.2
> gcc 3.3.2
> libgd 2.0.33
> libpng 1.2.8
> zlib - 1.2.3
>
> Trying to install GD 2.35 on AIX 5.2 and receiving the following error
> while running make:
>
> cc_r -c -I/usr/local/pkgs/gd.2.0.33/include -I/usr/include -I/
> usr/include/gd -D_ALL_SOURCE -D_ANSI_C_SOURCE -D_POSIX_SOURCE -
> qmaxmem=16384 -qnoansialias -DUSE_NATIVE_DLOPEN -DNEED_PTHREAD_INIT -
> q32 -D_LARGE_FILES -qlonglong -O -DVERSION=\"2.35\" -DXS_VERSION=
> \"2.35\" "-I/usr/opt/perl5/lib/5.8.2/aix-thread-multi/CORE" -
> DHAVE_PNG GD.c
> /bin/sh: cc_r: not found.
> make: 1254-004 The error code from the last command is 127.
>
> Now I know the problem here is that I am not running cc_r ... I'm only
> running gcc. But is there a way around this without having to put cc_r
> on the server? Is it possible to either bypass this cc_r requirement
> or get it to work with gcc somehow?
In general you have to build Perl extensions with the same compiler you
used to build perl. If you installed perl from a binary package, and it
was built with a compiler you do not have and cannot install, you will
need to either install GD from the same source (if your package supplier
provides it) or rebuild perl yourself from source with gcc. (If you
*did* build perl yourself, then where has cc_r vanished to since then?)
It *may* be possible to bodge Config.pm to make gcc work (this is
commonly done with ActivePerl on Win32, which is built with a non-free
compiler), but it is rather difficult to get it right. A lot of the
options interact in rather confusing ways.
Ben
--
If you put all the prophets, | You'd have so much more reason
Mystics and saints | Than ever was born
In one room together, | Out of all of the conflicts of time.
ben@morrow.me.uk The Levellers, 'Believers'
------------------------------
Date: Tue, 28 Aug 2007 00:47:14 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: How to make this case insenstive
Message-Id: <favrbq.1f4.1@news.isolution.nl>
it_says_BALLS_on_your forehead schreef:
> Daniel:
>> I am checking to see if the name is the same name as in $row[1] but
>> should be case insensitive.
>>
>> my $line = DBI::neat_list(\@row, 70, ',');
>> if ($name ne $row[1])
>> do
>> error message
>
> an alternative to the lc() or uc() solution is to use a regex
> (although this is probably less efficient).
>
> if ( $name =~ m/$row[1]/i ) {
> do {
> blah
Missing: quotemeta.
--
Affijn, Ruud
"Gewoon is een tijger."
------------------------------
Date: Mon, 27 Aug 2007 23:51:02 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: SCP/RCP stats ?
Message-Id: <m31cq4-k9r.ln1@osiris.mauzo.dyndns.org>
Quoth RV <rvlebars@hotmail.com>:
> Hello,
>
> I've made a network application that make a syncronisation of several
> computers. This app works with the "RSync" command that is based on
> SSH. I would to know the difference between SCP and a simple RCP
> concerning CPU load, network load, and the augmentation of the size of
> a compressed packet compared to a non-compressed packet.
>
> It is a tool to know this kind of informations ???
This is not a Perl question. You would probably get better answers
elsewhere, such as a forum dealing with SSH, rsync, or your OS.
Note that rsync-over-ssh is not at all the same thing as scp. What is it
you wish to compare? rsync-over-ssh to scp (likely to be much more
efficient in terms of network load, while using more CPU time)?
rsync-over-ssh to rsync-over-rsh (likely to be less efficient in terms
of both network load and CPU time, while being secure)?
In any case, you can find out various things about the raw network
packets sent with tcpdump; you can measure the CPU time required for a
transfer crudely with time(1) or top(1), or less crudely with something
like gprof(1) or Devel::DProf.
Ben
--
Like all men in Babylon I have been a proconsul; like all, a slave ... During
one lunar year, I have been declared invisible; I shrieked and was not heard,
I stole my bread and was not decapitated.
~ ben@morrow.me.uk ~ Jorge Luis Borges, 'The Babylon Lottery'
------------------------------
Date: Tue, 28 Aug 2007 00:29:48 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Tk::getSaveFile - use specified initialfile
Message-Id: <cc3cq4-q3c1.ln1@osiris.mauzo.dyndns.org>
Quoth Josef Moellers <josef.moellers@fujitsu-siemens.com>:
> In an application which downloads an image from a webcam, I'd like to
> save an image if it's interesting.
> For that, I have a "Save image" button, which calls this sub:
>
> sub save {
> my $top = shift;
> my @tv = localtime;
>
> my $base = 'webcam-'
> . sprintf('%04d.%02d.%02d', $tv[5]+1900, $tv[4]+1, $tv[3])
> . '-'
> . sprintf('%02d:%02d:%02d', $tv[2], $tv[1], $tv[0])
> . '.pnm';
I would replace this whole thing with POSIX::strftime:
use POSIX qw/strftime/;
my $base = strftime 'webcam-%Y.%m.%d-%H:%M:%S.pnm', localtime;
> my $dst = $top->getSaveFile(-initialdir => $HOME,
> -defaultextension => '.pnm',
> -initialfile => $base,
> -title => 'Save Snapshot',
> -filetypes => [ ['Images', '.pnm' ],
> ['All files', '*' ] ]);
>
> if (defined $dst) {
> print STDERR "dst=$dst\n";
> } else {
> print STDERR "dst=<undef>\n";
> }
> if (defined $dst && $dst ne "") {
> copy($tmpnam, $dst) or die "Copy failed: $!";
> }
> }
>
> Unfortunately, I cannot just hit the "Save" button in the getSaveFile
> window to use the given "webcam-..." name, I have to somehow make it
> "dirty". How can I convince the getSaveFile widget to use this
> initialfile without further action (i.e. just hit the "Save" button)?
The following script
#!/usr/bin/perl
use strict;
use warnings;
use Tk;
use POSIX qw/strftime/;
my $MW = Tk::MainWindow->new(-title => 'Test');
$MW->Button(
-command => sub {
my $base = strftime 'webcam-%Y.%m.%d-%H:%M:%S.pnm', localtime;
my $dst = $MW->getSaveFile(
-initialdir => $ENV{HOME},
-defaultextension => '.pnm',
-initialfile => $base,
-title => 'Save snapshot',
-filetypes => [
[ 'Images' => '.pnm' ],
[ 'All files' => '*' ],
],
);
$dst //= '<undef>'; # since you have dor :)
warn "dst=$dst";
},
-text => 'Save...',
)->pack;
Tk::MainLoop;
__END__
works for me with Tk v804.027; that is, I press 'Save...' and I get a
save-as box with the filename filled in, and I press 'Save' in the box
and perl prints
dst=/home/mauzo/webcam-2007.08.28-00:27:54.pnm at tksave line 26.
to stderr. What happens when you run this script?
Ben
--
"Awww, I'm going to miss her."
"Don't you hate her?"
"Yes, with a fiery vengeance."
[ben@morrow.me.uk]
------------------------------
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 801
**************************************