[28426] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 9790 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Oct 2 14:05:49 2006

Date: Mon, 2 Oct 2006 11:05:06 -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, 2 Oct 2006     Volume: 10 Number: 9790

Today's topics:
    Re: case insensitive hash lookup <jack_posemsky@yahoo.com>
    Re: case insensitive hash lookup <bik.mido@tiscalinet.it>
    Re: case insensitive hash lookup anno4000@radom.zrz.tu-berlin.de
    Re: case insensitive hash lookup <bik.mido@tiscalinet.it>
    Re: case insensitive hash lookup usenet@DavidFilmer.com
    Re: Does Perl have a wait command? beartiger@gmail.com
        Help with Code <dw149@acmex.gatech.edu>
    Re: Help with Code <mritty@gmail.com>
    Re: Help with Code <David.Squire@no.spam.from.here.au>
    Re: Help with Code <scobloke2@infotop.co.uk>
    Re: Help with Code anno4000@radom.zrz.tu-berlin.de
    Re: Help with Code <bik.mido@tiscalinet.it>
        How to delete temporary file after displaying in browse <ynl@nsparks.net>
    Re: How to delete temporary file after displaying in br <bart@nijlen.com>
    Re: How to delete temporary file after displaying in br <bik.mido@tiscalinet.it>
    Re: How to make 2 dimensinal aray in Perl $mat(x,y)? <traggatt@gmail.com>
    Re: how to read last row in a datafile ? <tzz@lifelogs.com>
    Re: Modifying a one-liner beartiger@gmail.com
    Re: My first socket question <jgibson@mail.arc.nasa.gov>
        MySql - push a hash into an array <john1949@yahoo.com>
    Re: MySql - push a hash into an array <mritty@gmail.com>
        Problem running "cvs commit -m" inside a Perl script <newshound@austin.rr.com>
    Re: Problem running "cvs commit -m" inside a Perl scrip <tadmc@augustmail.com>
    Re: Problem running "cvs commit -m" inside a Perl scrip <newshound@austin.rr.com>
    Re: Problem running "cvs commit -m" inside a Perl scrip <mritty@gmail.com>
    Re: Problem running "cvs commit -m" inside a Perl scrip anno4000@radom.zrz.tu-berlin.de
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: 2 Oct 2006 08:11:41 -0700
From: "Jack" <jack_posemsky@yahoo.com>
Subject: Re: case insensitive hash lookup
Message-Id: <1159801901.445249.129780@i42g2000cwa.googlegroups.com>


Aukjan van Belkum wrote:
> Jack wrote:
> > Hello,
> >
> > I noticed the hash lookup code below doesnt allow for case insensitive
> > nor pattern match lookups.. I want to essentially be able to do the
> > same as this (notice the "i"):
> > if ($temp2  =~ m/Abdomen/i) {  print " case insensitive pattern matched
> > !  "; }
> > with my hash lookup:
> > if (exists $hash{$temparray2[$sourcefield]}) {  print " exact match
> > found only "  }
> >
>
> If you just wish to use the lowercase value as a hash key, you can use
> the Perl function 'lc' ( perldoc -f lc ). This will return the lowercase
> of the value given:
>
> if ( exists $hash{ lc( $temparray2[$sourcefield] ) } {
> 	print " Lowercase key exists in hash";
> }
>
>
> Aukjan

thanks but this does not cover mixed case in the word, nor pattern
matching within the hash lookup - how can this be tested ??  ... if I
were to iterate through an array I could easily do this, but thats
slower than a hash lookup

Jack



------------------------------

Date: 2 Oct 2006 17:28:58 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: case insensitive hash lookup
Message-Id: <a0c2i2d8ddimthva0r9411elbvvhr32b19@4ax.com>

On 2 Oct 2006 07:35:23 -0700, "Jack" <jack_posemsky@yahoo.com> wrote:

>I noticed the hash lookup code below doesnt allow for case insensitive
>nor pattern match lookups.. I want to essentially be able to do the

For the case insensitive part I'd suggest just using lowercase keys
and check $hash{lc $key}. You may automatize all this with a tied
hash, maybe there's even a ready-made module for that, but I don't
think such a beast would be of much use. You should definitely use a
tied hash if you want keys that can be regexen.


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


------------------------------

Date: 2 Oct 2006 16:28:37 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: case insensitive hash lookup
Message-Id: <4oct1lFdsbl6U1@news.dfncis.de>

Jack <jack_posemsky@yahoo.com> wrote in comp.lang.perl.misc:
> 
> Aukjan van Belkum wrote:
> > Jack wrote:
> > > Hello,
> > >
> > > I noticed the hash lookup code below doesnt allow for case insensitive
> > > nor pattern match lookups.. I want to essentially be able to do the
> > > same as this (notice the "i"):
> > > if ($temp2  =~ m/Abdomen/i) {  print " case insensitive pattern matched
> > > !  "; }
> > > with my hash lookup:
> > > if (exists $hash{$temparray2[$sourcefield]}) {  print " exact match
> > > found only "  }
> > >
> >
> > If you just wish to use the lowercase value as a hash key, you can use
> > the Perl function 'lc' ( perldoc -f lc ). This will return the lowercase
> > of the value given:
> >
> > if ( exists $hash{ lc( $temparray2[$sourcefield] ) } {
> > 	print " Lowercase key exists in hash";
> > }
> >
> >
> > Aukjan
> 
> thanks but this does not cover mixed case in the word,

It does, if you lower-case all keys before storing.

> nor pattern
> matching within the hash lookup - how can this be tested ??

A hash key is always a specific string and lookup is always for
that exact key.  This is the basic operation of the data structure
called a hash.  "Pattern matching within the hash lookup" is a
contradiction in terms.

> ... if I
> were to iterate through an array I could easily do this, but thats
> slower than a hash lookup

A hash is as fast as it is *because* it only has one key (or a very
limited set of keys) to consider in each lookup.  Thus the access time
can be kept independent of the number of keys.  If all keys must be
considered, you lose that property willy-nilly.

Anno


------------------------------

Date: 2 Oct 2006 18:51:27 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: case insensitive hash lookup
Message-Id: <ekg2i29ii7a6b1ruj11m7o3bchvr65g8ri@4ax.com>

On 2 Oct 2006 08:11:41 -0700, "Jack" <jack_posemsky@yahoo.com> wrote:

>thanks but this does not cover mixed case in the word, nor pattern
>matching within the hash lookup - how can this be tested ??  ... if I
>were to iterate through an array I could easily do this, but thats
>slower than a hash lookup

See my other answer. Pattern match "within the hash lookup" *can* be
implemented, but you have to make choices: for example, what is more
than two regexen match your key? However it's not "regular" hash
lookup anymore. Hence it will be slower anyway.

All in all, some moderate amount of XY smell mixed with a slight
premature optimization flavour. Care to be more explicity about what
you *really* want to do?


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


------------------------------

Date: 2 Oct 2006 10:46:12 -0700
From: usenet@DavidFilmer.com
Subject: Re: case insensitive hash lookup
Message-Id: <1159811172.926991.264770@c28g2000cwb.googlegroups.com>

Jack wrote:

> I noticed the hash lookup code below doesnt allow for case insensitive
> nor pattern match lookups..

Tie::CPhash will let you have a case-preserving hash with
case-insensitive lookups (if you must).  But general pattern matching
on hash keys is a nuther thing...

-- 
David Filmer (http://DavidFilmer.com)



------------------------------

Date: 2 Oct 2006 09:21:03 -0700
From: beartiger@gmail.com
Subject: Re: Does Perl have a wait command?
Message-Id: <1159806063.016410.64340@c28g2000cwb.googlegroups.com>

J=FCrgen Exner wrote:
> ToddAndMargo@gbis.com wrote:
> >   I am a bit new to Perl.  Does Perl have
> > a wait command
>
> What happened when you just tried to read the documentation?
> See "perldoc -f wait"

You answer the question below.  It described a function he was not
interested in.

> > that will suspend on the command
> > for a determined amount of time?
>
> You are not looking for wait(), you are looking for sleep().

You couldn't just give that information.  No, you had to be a sarcastic
asshole first.


J



------------------------------

Date: Mon, 2 Oct 2006 16:23:15 +0000 (UTC)
From: David Williams <dw149@acmex.gatech.edu>
Subject: Help with Code
Message-Id: <efredj$p24$1@news-int.gatech.edu>

Hello all,
I am asking for help with the following code:


	if($old=~/checksum=(\d+)/)

	I think the =~ is not equal to meaning if
	$old (which is a filehandler) is not equal to something.

	also, checksum, is that a UNIX command? checksum is not a variable 
	anywhere in the code I am debugging.  I did a man page on checksum
	and got nothing back.

	Lastly, what is \d+ ? The PERL book says \d is digit but  I don't  
	understand.

	Thanks for any help!

David 

		
-- 
David Williams
Georgia Institute of Technology, Atlanta Georgia, 30332
Email: dw149@prism.gatech.edu


------------------------------

Date: 2 Oct 2006 09:33:07 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Help with Code
Message-Id: <1159806787.362810.185310@i3g2000cwc.googlegroups.com>

David Williams wrote:
> I am asking for help with the following code:
>
>
> 	if($old=~/checksum=(\d+)/)
>
> 	I think the =~ is not equal to

No, that's not correct.  =~ is the binding operator.  It says to look
for a pattern (the right argument) within the string (the left
argument).

The "not equal to" operator is != for numbers, and ne for strings

> meaning if
> 	$old (which is a filehandler) is not equal to something.

If $old is a filehandle, then you're not going to be able to do any
useful comparisons on it.  You must read a string from the filehandle
and do your comparison or pattern matching on that string.  You
generally do this with the < > operator, like so:

my $line = <$old>;

> 	also, checksum, is that a UNIX command? checksum is not a variable
> 	anywhere in the code I am debugging.  I did a man page on checksum
> 	and got nothing back.

In the above code, 'checksum' is simply a part of the pattern that is
being searched for in the variable $old.  It is not a variable nor a
UNIX command.

> 	Lastly, what is \d+ ? The PERL book says \d is digit but  I don't
> 	understand.

\d+ is a regular expression token that means "1 or more of any digit".


The entirety of the code above says:

"if the string contained in $old contains the string 'checksum',
followed by an equals sign, followed by 1 or more of any digit,
then..."

(it also stores the digits that it found in the variable $1, if the
pattern is actually successful).

You can read more about regular expressions by typing these into your
command window:
perldoc perlretut
perldoc perlre
perldoc perlreref

And you can find all the operators and what they mean by typing:
perldoc perlop

Hope this helps,
Paul Lalli



------------------------------

Date: Mon, 02 Oct 2006 17:35:32 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: Help with Code
Message-Id: <efrf4k$2f6$1@gemini.csx.cam.ac.uk>

David Williams wrote:
> Hello all,
> I am asking for help with the following code:
> 
> 
> 	if($old=~/checksum=(\d+)/)
> 
> 	I think the =~ is not equal to meaning if
> 	$old (which is a filehandler) is not equal to something.

No. It is an operator that associates a string with a regular expression
for matching.
> 
> 	also, checksum, is that a UNIX command? checksum is not a variable 
> 	anywhere in the code I am debugging.  I did a man page on checksum
> 	and got nothing back.

That's because it is part of the regular expression pattern in the match
in that line. It's a literal pattern of characters.

> 	Lastly, what is \d+ ? The PERL 

There's no such language as "PERL". It's Perl.

> book says \d is digit but  I don't  
> 	understand.

You need to go back to your book, or to the documentation that comes
with Perl, and learn the basics about regular expressions and the Perl
operators that use them. See, for example:

perldoc perlretut


DS


------------------------------

Date: Mon, 02 Oct 2006 17:47:21 +0100
From: Ian Wilson <scobloke2@infotop.co.uk>
Subject: Re: Help with Code
Message-Id: <drmdnTcPMucB37zYRVny2Q@bt.com>

David Williams wrote:
> Hello all,
> I am asking for help with the following code:

I recommend you read an introductory book such as "Learning Perl".

You may like to review what is available at http://learn.perl.org

Also, at a command prompt you can review the doumentation included with 
perl - e.g. view the table of contents using the command `perldoc perltoc`

> 	if($old=~/checksum=(\d+)/)
> 
> 	I think the =~ is not equal to meaning if
> 	$old (which is a filehandler) is not equal to something.

I find it's not worth guessing blindly, reading the documentation works 
well for me. Start at `perldoc perlop` and look for "Binding Operators"

> 
> 	also, checksum, is that a UNIX command? checksum is not a variable 
> 	anywhere in the code I am debugging.  I did a man page on checksum
> 	and got nothing back.

Your "checksum=" is fixed text in a Perl regular expression. It is 
something that is being searched for in the contents of the variable 
$old. In your case $old presumably contains something like
"lorem ipsum checksum=3489589713485 dolor sit amet" and your program 
needs to extract the checksum value.

> 
> 	Lastly, what is \d+ ? The PERL book says \d is digit but  I don't  
> 	understand.

\d matches "0", "1" ... "8" or "9"
+ means match one or more of the previous character
(\d+) 'captures' a sequence of one or more digits, for example an 
integer such as "6" or "1238". Capturing means that perl stores the 
matched text in a special variable for you to use later.

> 
> 	Thanks for any help!

Please follow the references before posting more questions of this sort.


------------------------------

Date: 2 Oct 2006 16:48:16 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: Help with Code
Message-Id: <4ocu6gFdjli1U1@news.dfncis.de>

David Williams  <dw149@acmex.gatech.edu> wrote in comp.lang.perl.misc:
> Hello all,
> I am asking for help with the following code:
> 
> 
> 	if($old=~/checksum=(\d+)/)
> 
> 	I think the =~ is not equal to meaning if

No.  "Not equal" can be expressed as "!=" or "ne" in Perl.  The "=~"
you have here is a binding operator.  In this case it means to match
the string $old against the pattern (/checksum=(\d+)/) on the right side.

> 	$old (which is a filehandler) is not equal to something.

It doesn't make sense for $old to be a filehandle (not filehander).
The pattern match that happens is only useful with a string.

> 	also, checksum, is that a UNIX command? checksum is not a variable 
> 	anywhere in the code I am debugging.  I did a man page on checksum
> 	and got nothing back.

It may or may not be a Unix command, that doesn't matter.  In your
code it is just part of the pattern to match.

> 	Lastly, what is \d+ ? The PERL book says \d is digit but  I don't  
> 	understand.

You need to understand regular expressions, which are Perl's (and many
other languages') way of expressing string patterns.  The specific
pattern /checksum=(\d+)/ matches any string that contains the characters
"checksum=" immediately followed by one or more digits.

    "hihi haha checksum=123 hoho"

would be an example.

> 	Thanks for any help!

You won't be able to debug a Perl program (even a short one) with
ad-hoc explanations given on Usenet.  You'll either have to learn
enough Perl to understand the program or get someone else to do it.

Anno


------------------------------

Date: 2 Oct 2006 19:00:41 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Help with Code
Message-Id: <73h2i29r5dcr85jgpuc9ofthjhtdc334h2@4ax.com>

On Mon, 2 Oct 2006 16:23:15 +0000 (UTC), David Williams
<dw149@acmex.gatech.edu> wrote:

>Subject: Help with Code

*PLEASE* put the subject of your post in the Subject.

>Hello all,
>I am asking for help with the following code:
>
>
>	if($old=~/checksum=(\d+)/)

Very very basic Perl syntax. It checks whether the string contained in
$old contains a substring consisting of the literal string 'checksum='
followed by a positive number of digits.

>	I think the =~ is not equal to meaning if
>	$old (which is a filehandler) is not equal to something.

  perldoc perlop

And I bet $old is *not* a filehandler^H!

>	also, checksum, is that a UNIX command? checksum is not a variable 

It's a string!

>	anywhere in the code I am debugging.  I did a man page on checksum

I think that DEBUGGING that code would require some familiarity with
ELEMENTARY Perl.

>	Lastly, what is \d+ ? The PERL book says \d is digit but  I don't  
>	understand.

Read more of it. But I doubt it is a "PERL" book. See

  perldoc -q difference.

>	Thanks for any help!

HTH.


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


------------------------------

Date: Mon, 2 Oct 2006 17:21:28 +0200
From: Yohan N Leder <ynl@nsparks.net>
Subject: How to delete temporary file after displaying in browser ?
Message-Id: <MPG.1f8b4ce71734cd499898d6@news.tiscali.fr>

How do you proceed to unlink a temporary file at the end of your CGI 
treatment. I have to upload an image from client, display-it in his 
browser with some info about it, then delete it on server.

Would you fork the unlink ? Would you add a sleep prior to unlink after 
printing toward browser ? Do you write a separate script which would 
delete all temporaries file periodically ?

What's the right way according to you ?


------------------------------

Date: 2 Oct 2006 08:54:49 -0700
From: "Bart Van der Donck" <bart@nijlen.com>
Subject: Re: How to delete temporary file after displaying in browser ?
Message-Id: <1159804489.823095.242200@m73g2000cwd.googlegroups.com>

Yohan N Leder wrote:

> How do you proceed to unlink a temporary file at the end of your CGI
> treatment. I have to upload an image from client, display-it in his
> browser with some info about it, then delete it on server.
>
> Would you fork the unlink ?

No.

> Would you add a sleep prior to unlink after printing toward browser ?

Maybe just to make sure - it won't hurt anyhow. But be careful for max
runtime flags, esp. in combintation with potentially large file
uploads.

> Do you write a separate script which would
> delete all temporaries file periodically ?

Yes, a low-priority job deleting files older than X.

> What's the right way according to you ?

Normally you can just use unlink without need for extra precautions
here. The CGI has sent everything before the unlink-command is invoked,
that is, unlink will "wait" until the output stream is closed.

-- 
 Bart



------------------------------

Date: 2 Oct 2006 18:54:39 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: How to delete temporary file after displaying in browser ?
Message-Id: <4sg2i2l95iv3plmb1hb4e38ejpka228ece@4ax.com>

On Mon, 2 Oct 2006 17:21:28 +0200, Yohan N Leder <ynl@nsparks.net>
wrote:

>How do you proceed to unlink a temporary file at the end of your CGI 
>treatment. I have to upload an image from client, display-it in his 
>browser with some info about it, then delete it on server.
>
>Would you fork the unlink ? Would you add a sleep prior to unlink after 
>printing toward browser ? Do you write a separate script which would 
>delete all temporaries file periodically ?
>
>What's the right way according to you ?

I'm not really sure if I understand. It seems that you create a
temporary but *static* file to be served directly by the browser. Why
don't you serve it dinamically instead?


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


------------------------------

Date: 2 Oct 2006 08:18:44 -0700
From: "tim" <traggatt@gmail.com>
Subject: Re: How to make 2 dimensinal aray in Perl $mat(x,y)?
Message-Id: <1159802324.107966.21460@e3g2000cwe.googlegroups.com>


Tad McClellan wrote:
> tim <traggatt@gmail.com> wrote:
>
> > sorry about before....
>
>
> No you're not.

Yes I am, I just didn't know what it was before!
 
> So long!

Bye



------------------------------

Date: Mon, 02 Oct 2006 12:55:32 -0400
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: how to read last row in a datafile ?
Message-Id: <g69mz8eirnf.fsf@CN1374059D0130.kendall.corp.akamai.com>

On  2 Oct 2006, bart@nijlen.com wrote:

Jack wrote:
>> Hi there, does anyone know how to capture the last row in a datafile
>> into a variable without having to read through each record of the
>> entire file ?
>
> On UNIX (and some Windows versions):
>
> my $lastline = `tail -1 /path/to/file.txt`;

That should be /usr/bin/tail or whatever's appropriate on the system.
Calling unqualified programs is a dangerous practice.

Ted


------------------------------

Date: 2 Oct 2006 09:17:15 -0700
From: beartiger@gmail.com
Subject: Re: Modifying a one-liner
Message-Id: <1159805835.114521.33670@h48g2000cwc.googlegroups.com>

Ted Zlatanov wrote:
> On  1 Oct 2006, beartiger@gmail.com wrote:
>
> > Then why do *you* spell it PERL here?
> >
> > http://cgi2.cs.rpi.edu/~lallip/perl/spring06/index.shtml
>
> You may not know that all-uppercase letters are an acceptable way to
> capitalize headings and titles, at least in the USA.

You evidently know nothing of what you speak.  That's not a heading or
a title.  It's a logo.  There is no title on the site.  The headings
are init-capped, e.g.:

     http://cgi2.cs.rpi.edu/~lallip/perl/spring06/hw4.shtml


John



------------------------------

Date: Mon, 02 Oct 2006 10:02:07 -0700
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: My first socket question
Message-Id: <021020061002077563%jgibson@mail.arc.nasa.gov>

In article <6m12i212s0gcl8n0b8pn2uq60jtecnt3q4@4ax.com>, Michele Dondi
<bik.mido@tiscalinet.it> wrote:

> On 02 Oct 2006 02:33:01 GMT, xhoster@gmail.com wrote:
> 
> >>   while (1) {
> >>       $cnt++;
> >>       $val=rand;  # these are the important calculations!!
> >>       next unless $sel->can_read(0.2);
> >
> >Why the 0.2?  If the main task is $cnt++ and $val=rand, then it should
> >be spending most of it's time there and not waiting for someone to make
> >a connection that quite likely will not come within any given 0.2 anyway.

The benefit of using a timeout to can_read to control program execution
and timing is better responsiveness. If you use sleep (or select) to
pause the program when it has nothing to do but wait for clients, you
cannot respond to the client until the timeout to sleep has expired. If
you use a timeout parameter for can_read, you can respond immediately.
After the client has been serviced, the program can decide to perform
some calculations or call can_read again, perhaps with a shorter
timeout this time.

> 
> No really good reason but utter ignorance. In my ignorance I thought
> the call may block if I don't timeout. Indeed can_read()'s
> documentation mentions that possibility in connection with
> "registered" handles. But I'm not really sure if I know what that
> means. No, I'm sure: I *don't* know.

"registered" means that IO::Select::add has been called on the
IO::Select object with the handle for the socket. Read up on Unix's
select statement. IO::Select is a Perl mapping to that routine, which
is the principal method in Unix for handling I/O for multiple entities
and avoiding blocking or polling.

> 
> BTW: the "real" application is not that computation intensive, and
> actually has a sleep(1) in the main cycle. I get your point, though.
> And I thank you for your informative reply.

That is a common situation. It is rare for a program that needs to
service clients to be CPU-bound. Thus, some sort of sleep or timeout is
usually inserted. Using the timeout of select is a good method of
sleeping while remaining responsive.

 Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------        
                http://www.usenet.com


------------------------------

Date: Mon, 2 Oct 2006 18:43:27 +0100
From: "John" <john1949@yahoo.com>
Subject: MySql - push a hash into an array
Message-Id: <U4mdnREl_qon0rzYnZ2dnUVZ8s-dnZ2d@eclipse.net.uk>

Hi

I am reading a row of hashes from a MySQL table.
I need to add that row to an array to create an 'array of hashes'
I should then be able to access, say, $xyz[34]{'steel'}.

The following is part of the code:

my @xyz=({}); # array of hashes

$sql="SELECT coal,iron,steel,china,wool,cloth,fish,livestock FROM industry";
$sth=$dbh->prepare($sql);
$sth->execute or die "Unable to execute query: $dbh->errstr\n";

my $row;
while ($row = $sth->fetchrow_hashref) {push (@xyz,$row)}

$sth->finish();
$dbh- >disconnect();

print $xyz[6]{'cloth'};

The print statement is empty.  Any ideas?

Regards
John




------------------------------

Date: 2 Oct 2006 10:55:05 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: MySql - push a hash into an array
Message-Id: <1159811705.165180.7860@k70g2000cwa.googlegroups.com>

John wrote:
> I am reading a row of hashes from a MySQL table.
> I need to add that row to an array to create an 'array of hashes'
> I should then be able to access, say, $xyz[34]{'steel'}.
>
> The following is part of the code:
>
> my @xyz=({}); # array of hashes

There is no need for that initialization.  Indeed, it will hurt you in
the long run.  You are specifically adding one completely empty hash
reference to your array.  Therefore, when all is said and done, your
array will have one more hash than the number of rows in your data set.
 Change to:
my @xyz;

> $sql="SELECT coal,iron,steel,china,wool,cloth,fish,livestock FROM industry";
> $sth=$dbh->prepare($sql);
> $sth->execute or die "Unable to execute query: $dbh->errstr\n";
>
> my $row;

> while ($row = $sth->fetchrow_hashref) {push (@xyz,$row)}
>
> $sth->finish();
> $dbh- >disconnect();
>
> print $xyz[6]{'cloth'};
>
> The print statement is empty.  Any ideas?

Well the first and most obvious is: Are you sure that the statement
handler returned at least 7 rows?    The second and second-most obvious
is: Are you sure that the 7th row's value for the 'cloth' column was
neither NULL nor the empty string?

What do you get for these two lines?

use Data::Dumper;
print Dumper(\@xyz);

Finally, are you aware that DBI has a means by which to do this
already?
my @xyz = @{ $sth->fetchall_arrayref( { } ) };

Paul Lalli



------------------------------

Date: 2 Oct 2006 09:11:33 -0700
From: "Newshound" <newshound@austin.rr.com>
Subject: Problem running "cvs commit -m" inside a Perl script
Message-Id: <1159805493.751535.151680@i42g2000cwa.googlegroups.com>

Hi all,

I'm trying to take a "cvs commit -m" command line with a multi-line
log message and run it inside a Perl script.  For instance, trying to
run the command line:

prompt> cvs commit -m"This is a\
? multi-line log message." filename
prompt>

I would be trying to run this inside Perl as:

my $mesg = "\"This is a\\\nmulti-line log message."
RunCommand("cvs commit -m $mesg filename","commit.log");
    ....
sub RunCommand  {
    my $Command = shift;
    my $Logfile = shift;
    open(CMD_PIPE, "$Command |" )
    open( LOG, ">$Logfile" )
    while ( <CMD_PIPE> )  { print LOG $_; }
    close( CMD_PIPE );
    close( LOG )
}

There is some other stuff going on in the subroutine for stderr
redirection, skipping the command and just echoing it, etc., but
this captures the main point of the code.

The problem I am having is that this doesn't work correctly; it
always produces malformed log strings.  I'm not a Perl jockey,
I know enough to get by, and the extra backslashes for multiple
layers of quoting sometimes confuse me, but I've tried the above
with various numbers of backslashes as well as splitting the
message string up into a concatenation of substrings to isolate
the newline, etc.

I just can't quite seem to find the right code.  I'm trying to make
sure that a backslash is present to indicate the multi-line nature
of the message, as well as a newline to actually split the message
onto another line, but what cvs commit receives is not what I intend.

Can someone please help?  I'd really appreciate it.

Thanks,
Mike



------------------------------

Date: Mon, 2 Oct 2006 12:11:54 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Problem running "cvs commit -m" inside a Perl script
Message-Id: <slrnei2i2q.p2n.tadmc@magna.augustmail.com>

Newshound <newshound@austin.rr.com> wrote:

> my $mesg = "\"This is a\\\nmulti-line log message."
              ^^
              ^^

You have only one double quote character in your string.

Don't you want another one to delimit the end of the string?

You are also missing a semicolon at the end of that statement.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


------------------------------

Date: 2 Oct 2006 10:19:17 -0700
From: "Newshound" <newshound@austin.rr.com>
Subject: Re: Problem running "cvs commit -m" inside a Perl script
Message-Id: <1159809557.065409.111620@m7g2000cwm.googlegroups.com>

Sorry, that was a typo on my part.  I do actually have an escaped
double-quote at the end in my script, like so:

my $mesg = "\"This is a\\\nmulti-line log message.\""

Thanks for catching that.

--Mike

P.S.--I think I've gotten it somewhat working with a "here" document,
but it has some annoying side-effects I'd like to avoid if I can manage
to get the original code working.


Tad McClellan wrote:
> Newshound <newshound@austin.rr.com> wrote:
>
> > my $mesg = "\"This is a\\\nmulti-line log message."
>               ^^
>               ^^
>
> You have only one double quote character in your string.
>
> Don't you want another one to delimit the end of the string?
>
> You are also missing a semicolon at the end of that statement.
>
>
> --
>     Tad McClellan                          SGML consulting
>     tadmc@augustmail.com                   Perl programming
>     Fort Worth, Texas



------------------------------

Date: 2 Oct 2006 10:31:02 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Problem running "cvs commit -m" inside a Perl script
Message-Id: <1159810262.518933.163910@e3g2000cwe.googlegroups.com>

Newshound wrote:
> I'm trying to take a "cvs commit -m" command line with a multi-line
> log message and run it inside a Perl script.  For instance, trying to
> run the command line:
>
> prompt> cvs commit -m"This is a\
> ? multi-line log message." filename
> prompt>
>
> I would be trying to run this inside Perl as:
>
> my $mesg = "\"This is a\\\nmulti-line log message."

As has already been pointed out, you have a typo here.  Please copy and
paste your code, do not retype.

You're also making things harder for yourself than they have to be, by
using quoting delimiters that you actually want to have in your string.

You're also apparently not understanding that the \ in your original
shell command is merely an identifier to the shell that the command
didn't end when you pushed enter.  There should not actually be a slash
there.

Write that command as either:
my $message = qq{"This is a\nmulti-line log message."};
or
my $message = '"This is a
multi-line log messsage"';

> RunCommand("cvs commit -m $mesg filename","commit.log");
>     ....
> sub RunCommand  {
>     my $Command = shift;
>     my $Logfile = shift;
>     open(CMD_PIPE, "$Command |" )

Where is the semi-colong that terminates this statement?  Please copy
and paste your code, please do not retype.

Please do use lexical filehandles, not global barewords.

Please do check the return value of all system calls.

open my $CMD_PIPE, "$Command |" or die "Cannot start '$Command': $!";

>     open( LOG, ">$Logfile" )

All four of the previous comments apply here, plus, you should please
make sure to use the three-argument form of open:

open my $LOG, '>', $Logfile or die "Cannot open '$Logfile': $!";

Paul Lalli



------------------------------

Date: 2 Oct 2006 17:39:14 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: Problem running "cvs commit -m" inside a Perl script
Message-Id: <4od162Fe396fU1@news.dfncis.de>

Newshound <newshound@austin.rr.com> wrote in comp.lang.perl.misc:
> Hi all,
> 
> I'm trying to take a "cvs commit -m" command line with a multi-line
> log message and run it inside a Perl script.  For instance, trying to
> run the command line:
> 
> prompt> cvs commit -m"This is a\
> ? multi-line log message." filename
> prompt>
> 
> I would be trying to run this inside Perl as:
> 
> my $mesg = "\"This is a\\\nmulti-line log message."

You're fighting the shell.  Perhaps you can avoid it.

> RunCommand("cvs commit -m $mesg filename","commit.log");
>     ....
> sub RunCommand  {
>     my $Command = shift;
>     my $Logfile = shift;
>     open(CMD_PIPE, "$Command |" )
>     open( LOG, ">$Logfile" )
>     while ( <CMD_PIPE> )  { print LOG $_; }
>     close( CMD_PIPE );
>     close( LOG )
> }

Don't re-type code, copy-paste is from a runnable source.

Use the list form for the command, that executes it without an
intervening shell.  A command argument can be a multiline string
(indeed any binary string) without problems.  Change the beginning
of RunCommand to (untested)

    my @Command = @{ shift() };
    my $Logfile = shift;
    open(CMD_PIPE, '-|', @Command );

Try it with

    RunCommand( [ 'echo', "one\ntwo\nthree"], '/tmp/log');

The multiline argument to "echo" should be exactly reproduced in
/tmp/log

> There is some other stuff going on in the subroutine for stderr
> redirection, ...

Now this could be the fly in the ointment because that's hard to
do without a shell.  You can either cope with shell quoting (it's
doable, but I'm not going there), or fork and redirect yourself before
exec()ing @Command.  IPC::Open3 deals with part of that (and then some
more).

Anno


------------------------------

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 V10 Issue 9790
***************************************


home help back first fref pref prev next nref lref last post