[25832] in Perl-Users-Digest
Perl-Users Digest, Issue: 8069 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed May 11 09:05:28 2005
Date: Wed, 11 May 2005 06:05: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 Wed, 11 May 2005 Volume: 10 Number: 8069
Today's topics:
Games.pl again <hackeras@gmail.com>
Re: Games.pl again <no@email.com>
Re: Games.pl again <hackeras@gmail.com>
Re: Games.pl again <hackeras@gmail.com>
Re: Games.pl again <tadmc@augustmail.com>
Re: Games.pl again <no@email.com>
Re: How can I prevent perl from exiting during a networ (Song Lining)
Loading a hash from a string containing newlines <usenet739_yahoo_com_au>
Re: Loading a hash from a string containing newlines <zen13097@zen.co.uk>
Re: Media Lending Library - web based application <tadmc@augustmail.com>
Re: Simple error using GD::Graph on Windows <sisyphus1@nomail.afraid.org>
Re: Storable - Win32 to Linux <joe@inwap.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 11 May 2005 09:49:25 +0300
From: Nikos <hackeras@gmail.com>
Subject: Games.pl again
Message-Id: <d5s9tj$q5t$1@nic.grnet.gr>
#=======================LOADING THE .TXT TO THE
DATABASE========================
my (@row, $gamename, $gamedesc, $gamecount);
my $select = $dbh->prepare( "SELECT * FROM games WHERE gamename=?" );
my $insert = $dbh->prepare( "INSERT INTO games (gamename, gamedesc,
gamecounter) VALUES (?, ?, ?)" );
my $update = $dbh->prepare( "UPDATE games SET gamedesc=?, gamecount=?+1
where gamename=?" );
open (FILE, "<../data/games/descriptions.txt") or die $!;
while (<FILE>) {
chomp;
($gamename, $gamedesc) = split /\t/;
$select->execute( $gamename );
if ($select->rows) {
$update->execute( $gamedesc, $gamecount, $gamename );
}
else {
$insert->execute( $gamename, $gamedesc, 0 );
}
}
close (FILE);
#=============================SHOW THE GAMES
TABLE==============================
print span( {class=>'lime'}, "Áðü åäþ ìðïñåßò íá êáôåâÜóåéò ùñáßá
áðëÜ ðáé÷íßäéá ðïõ Ý÷ù åðéëÝîåé!" ), br;
print span( {class=>'yellow'}, "Ìðïñåßò íá åðéêïéíùíÞóåéò ìáæß ìïõ óôï
hackeras\@gmail.com" ), br() x 2;
$sth = $dbh->prepare( "SELECT * FROM games" );
$sth->execute;
print start_form(-action=>'games.pl');
print table( {class=>'games'} );
while( $row = $sth->fetchrow_hashref )
{
print Tr(
td( {-width=>'20%'}, submit( $row->{gamename} )),
td( {-width=>'75%'}, $row->{gamedesc} ),
td( {-width=>'5%'}, $row->{gamecounter} )
);
}
print end_table;
print end_form;
print br;
if ( !param() ) {
print p( {-align=>'center'}, a( {href=>'index.pl'}, img
{src=>'../data/images/back.gif'} ));
}
#===============================================================================
Its no that i want to deliberately ignore the advice you proposed to me,
its just that i almost never use primary key as text only as integers
with auto_increment.
Apart from that i wanted to see how would it work they way i have it.
Is there really anned for a primary key?
We can always select from our mysql table to see if the record exists or
not to update or insert accordingly!
--
"Of course I cant stop you. And that would really bum me out
if that were my job. But my job isnt to stop you, its to
make it as difficult as possible, for as many as possible,
for as long as possible ."
------------------------------
Date: Wed, 11 May 2005 09:45:29 +0100
From: Brian Wakem <no@email.com>
Subject: Re: Games.pl again
Message-Id: <3edv1aF2g1j7U1@individual.net>
Nikos wrote:
> my $select = $dbh->prepare( "SELECT * FROM games WHERE gamename=?" );
> We can always select from our mysql table to see if the record exists or
> not to update or insert accordingly!
I think it would be better/faster to do a SELECT COUNT(*) FROM games
WHERE ....... and then check whether the value is true or not, rather than
selecting an unknown number of rows in their entirety.
--
Brian Wakem
------------------------------
Date: Wed, 11 May 2005 15:37:57 +0300
From: Nikos <hackeras@gmail.com>
Subject: Re: Games.pl again
Message-Id: <d5sub3$htp$1@nic.grnet.gr>
Brian Wakem wrote:
> Nikos wrote:
>
>
>>my $select = $dbh->prepare( "SELECT * FROM games WHERE gamename=?" );
>
>
>>We can always select from our mysql table to see if the record exists or
>>not to update or insert accordingly!
>
>
>
>
> I think it would be better/faster to do a SELECT COUNT(*) FROM games
> WHERE ....... and then check whether the value is true or not, rather than
> selecting an unknown number of rows in their entirety.
>
>
Yes i agree.
But how am i gona check if the value is true or not?
--
"Of course I cant stop you. And that would really bum me out
if that were my job. But my job isnt to stop you, its to
make it as difficult as possible, for as many as possible,
for as long as possible ."
------------------------------
Date: Wed, 11 May 2005 15:44:42 +0300
From: Nikos <hackeras@gmail.com>
Subject: Re: Games.pl again
Message-Id: <d5suno$i8c$1@nic.grnet.gr>
Perhaps you mean like this:
my ($gamename, $gamedesc, $gamecount);
my $select = $dbh->prepare( "SELECT count(*) FROM games WHERE gamename=?" );
my $insert = $dbh->prepare( "INSERT INTO games (gamename, gamedesc,
gamecounter) VALUES (?, ?, ?)" );
my $update = $dbh->prepare( "UPDATE games SET gamedesc=?, gamecount=?+1
where gamename=?" );
open (FILE, "<../data/games/descriptions.txt") or die $!;
while (<FILE>) {
chomp;
($gamename, $gamedesc) = split /\t/;
if ( $select->execute($gamename) ) {
$update->execute( $gamedesc, $gamecount, $gamename );
}
else {
$insert->execute( $gamename, $gamedesc, 0 );
}
}
close (FILE);
------------------------------
Date: Wed, 11 May 2005 07:48:30 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Games.pl again
Message-Id: <slrnd83vou.gpd.tadmc@magna.augustmail.com>
Nikos <hackeras@gmail.com> wrote:
> my (@row, $gamename, $gamedesc, $gamecount);
You should declare variables in the smallest possible scope.
That usually means declare them where you first use them.
Your code never uses @row, so you don't need to declare it at all.
Your code never puts a value into $gamecount (do you have warnings enabled?).
> my $insert = $dbh->prepare( "INSERT INTO games (gamename, gamedesc,
> gamecounter) VALUES (?, ?, ?)" );
^^^^^^^^^^^
^^^^^^^^^^^
> my $update = $dbh->prepare( "UPDATE games SET gamedesc=?, gamecount=?+1
^^^^^^^^^
One of these things is not like the other,
one of these things just doesn't belong...
> ($gamename, $gamedesc) = split /\t/;
my($gamename, $gamedesc) = split /\t/; # declare at first use
> if ( !param() ) {
unless ( param() ) {
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 11 May 2005 13:51:47 +0100
From: Brian Wakem <no@email.com>
Subject: Re: Games.pl again
Message-Id: <3eedf4F2llnbU1@individual.net>
Nikos wrote:
> Perhaps you mean like this:
>
> my ($gamename, $gamedesc, $gamecount);
> my $select = $dbh->prepare( "SELECT count(*) FROM games WHERE gamename=?"
> ); my $insert = $dbh->prepare( "INSERT INTO games (gamename, gamedesc,
> gamecounter) VALUES (?, ?, ?)" );
> my $update = $dbh->prepare( "UPDATE games SET gamedesc=?, gamecount=?+1
> where gamename=?" );
>
> open (FILE, "<../data/games/descriptions.txt") or die $!;
> while (<FILE>) {
> chomp;
>
> ($gamename, $gamedesc) = split /\t/;
>
> if ( $select->execute($gamename) ) {
> $update->execute( $gamedesc, $gamecount, $gamename );
> }
> else {
> $insert->execute( $gamename, $gamedesc, 0 );
> }
> }
> close (FILE);
Not quite. I meant like this:-
...
$select->execute($gamename);
my $record_exists = $select->fetchrow_array();
if ($record_exists) {
# update
}
else {
# insert
}
--
Brian Wakem
------------------------------
Date: 10 May 2005 23:54:56 -0700
From: songlining@gmail.com (Song Lining)
Subject: Re: How can I prevent perl from exiting during a network error?
Message-Id: <fabd4f77.0505102254.600d833b@posting.google.com>
Thanks guys!
Got the problems solved according to your suggestions, here's the
code:
do {
$sock = new IO::Socket::INET(
PeerAddr => $PeerAddr,
PeerPort => $PeerPort,
Proto => 'tcp',
);
if (!$sock) {
print STDERR localtime() . ": Could not connect to M2000, will try
again in 10 secs.\n";
select(undef, undef, undef, 10);
}
eval {
while(<$sock>) {
# do something here...
}
}
# if $! is defined, that means something goes wrong within the eval
block, normally a network problem.
} until ($sock && !($!));
This way, the client will never exit.
Thanks again!
Song Lining
Jim Gibson <jgibson@mail.arc.nasa.gov> wrote in message news:<090520050921330579%jgibson@mail.arc.nasa.gov>...
> In article <fabd4f77.0505080732.428a101a@posting.google.com>, Song
> Lining <songlining@gmail.com> wrote:
>
> > Hi, guys
> >
> > Got a very frustrating problem while writing a perl socket client to
> > connect to a server.
> >
> > The perl client will quit by itself everytime I stop the server and
> > the errno is either "ECONNRESET: Connection aborted" or "ECONNABORTED:
> > Connection reset by peer". The problem is that I need to keep the
> > client alive even the server is shutdown, so I tried to ignore all the
> > signals in %SIG but that didn't help either. Here's a snipet of the
> > code:
>
> [ client code snipped ]
>
> You cannot keep a socket connection alive if the other host involved
> has closed it. You need to trap and detect this error and repeat the
> connect step. Implement your client accordingly and keep trying to
> connect until you either succeed or give up. Put a time delay between
> connection attempts so that you don't hog the network and inadvertantly
> create a denial-of-service attack. Intelligent clients will start with
> a short delay and increase it for each unsuccessful attempt, up to some
> maximum.
>
> Good luck.
>
>
> ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
> http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
> ---= East/West-Coast Server Farms - Total Privacy via Encryption =---
------------------------------
Date: Wed, 11 May 2005 16:52:22 +1000
From: "Scott Bass" <usenet739_yahoo_com_au>
Subject: Loading a hash from a string containing newlines
Message-Id: <4281aba7$0$26856$5a62ac22@per-qv1-newsreader-01.iinet.net.au>
Hi,
my $var = "TESTID: A\nB\nOBJECTIVE: C\nD\nPROCEDURE: E\nF\nRESULTS: G\nH\n";
my %hash = $var =~ /(TESTID|OBJECTIVE|PROCEDURE|RESULTS):(.+)/mg;
print $hash{"TESTID"};
print $hash{"OBJECTIVE"};
print $hash{"PROCEDURE"};
print $hash{"RESULTS"};
1. How can I get $hash{"TESTID"} = "A\nB", etc.?
2. (Minor) How can I make the hash keys lowercase? The search needs to be
case-sensitive, and the text must be in upper case?
Sorry if this is in the docs (just point me to it). I searched the doc,
Googled, and skimmed a couple O'Reilly books before posting.
Thanks,
Scott
------------------------------
Date: 11 May 2005 07:52:02 GMT
From: Dave Weaver <zen13097@zen.co.uk>
Subject: Re: Loading a hash from a string containing newlines
Message-Id: <4281b9a2$0$4183$da0feed9@news.zen.co.uk>
On Wed, 11 May 2005 16:52:22 +1000, Scott Bass <usenet739_yahoo_com_au> wrote:
>
> my $var = "TESTID: A\nB\nOBJECTIVE: C\nD\nPROCEDURE: E\nF\nRESULTS: G\nH\n";
> my %hash = $var =~ /(TESTID|OBJECTIVE|PROCEDURE|RESULTS):(.+)/mg;
> 1. How can I get $hash{"TESTID"} = "A\nB", etc.?
In order for '.' to match a newline, you'll need the /s modifier, not
/m. However, this then causes .+ to be too greedy, so further checks
are required:
my %hash = $var =~ /(TESTID|OBJECTIVE|PROCEDURE|RESULTS): (.+?)(?=\n\w+:|$)/sg;
> 2. (Minor) How can I make the hash keys lowercase?
while ( $var =~ /(TESTID|OBJECTIVE|PROCEDURE|RESULTS): (.+?)(?=\n\w+:|$)/sg ) {
$hash{ lc $1 } = $2;
}
------------------------------
Date: Wed, 11 May 2005 07:00:23 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Media Lending Library - web based application
Message-Id: <slrnd83sun.gpd.tadmc@magna.augustmail.com>
Michael De Tomaso <mdetomaso@bak.rr.com> wrote:
> Better ???
>
>>
>> People who persist in posting a ten-line .sig after being
>> asked to repair it risk getting killfiled.
Yes, your .sig now follows usenet conventions.
Thank you.
However, people who persist in top-posting also risk getting
killfiled, so please don't do that either.
Have you seen the Posting Guidelines that are posted here frequently?
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 11 May 2005 14:42:02 +1000
From: "Sisyphus" <sisyphus1@nomail.afraid.org>
Subject: Re: Simple error using GD::Graph on Windows
Message-Id: <42818d22$0$4654$afc38c87@news.optusnet.com.au>
"Mr Hyde" <pump_yo_brakes@hotgetridofthisbitmail.com> wrote in message
news:od0381prr6minhdoid2nvjqoaad17o9fvb@4ax.com...
> On Wed, 11 May 2005 12:38:18 +1000, "Sisyphus"
> <sisyphus1@nomail.afraid.org> wrote:
>
> >Does the following run without error for you:
> >
> >use GD;
> >$im = GD::Image->new(800, 800);
> >print "OK\n";
> >__END__
> >
> >If so, then maybe you just need to "use GD;" at the start of your script
> >..... though why that should be necessary eludes me.
> >
> >Cheers,
> >Rob
> >
>
> Problem solved! It appears the problem was that it couldn't find the
> GD library, not sure why. I added
>
> use lib "/Perl/site/lib";
>
> above the "use GD;" line and it worked fine. Now I was under the
> impression it would have added its own library to the path when it
> installed, but I guess not.
>
Odd. I would have thought that "/Perl/site/lib" was already in @INC - in
which case the "use lib ..." line would achieve nothing.
Run "perl -V". That will show you, at the end of the output, the directories
that are in @INC. Any module in any of the specified directories *should* be
found automatically.
Cheers,
Rob
------------------------------
Date: Wed, 11 May 2005 05:54:48 -0700
From: Joe Smith <joe@inwap.com>
Subject: Re: Storable - Win32 to Linux
Message-Id: <XpOdnTtnMJstnR_fRVn-1g@comcast.com>
A. Sinan Unur wrote:
>>I would be more than happy to read it. If you intend to refer me to
>>something called "network order" then it would be nice if this were
>>defined first.
>
> http://www.google.com/search?q=define%3A+network+order
Thanks for posting that link.
Going to http://en.wikipedia.org/wiki/Endianness brought back memories,
resulted in some minor edits, and inspired me to create a new entry for
the VT180 computer-in-a-terminal.
Ob Perl: pack()/unpack() use 'v' and 'V' for VAX format, which is the
antithesis of network byte order ('n' and 'N'). Portability is
increased by writing bytes to the disk in network byte order.
-Joe
------------------------------
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 8069
***************************************