[29870] in Perl-Users-Digest
Perl-Users Digest, Issue: 1113 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Dec 13 14:09:44 2007
Date: Thu, 13 Dec 2007 11:09:12 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 13 Dec 2007 Volume: 11 Number: 1113
Today's topics:
Re: (1)[0] ok but not 1[0] <sensorflo@gmail.com>
Re: (1)[0] ok but not 1[0] <uri@stemsystems.com>
Re: Can't locate object method "readMessage" ... (Inher <njc@cookie.uucp>
Re: FAQ 4.58 How can I know how many entries are in a h <xueweizhong@gmail.com>
Re: FAQ 4.58 How can I know how many entries are in a h <xueweizhong@gmail.com>
Re: FAQ 4.58 How can I know how many entries are in a h xhoster@gmail.com
Re: FAQ 4.58 How can I know how many entries are in a h <joost@zeekat.nl>
Re: FAQ 4.58 How can I know how many entries are in a h <uri@stemsystems.com>
Re: FAQ 4.58 How can I know how many entries are in a h <jurgenex@hotmail.com>
Re: FAQ 4.58 How can I know how many entries are in a h <joost@zeekat.nl>
Re: FAQ 4.58 How can I know how many entries are in a h <uri@stemsystems.com>
Re: Need help integrating a perl command into a shell s <krahnj@telus.net>
Re: Need help integrating a perl command into a shell s <m@rtij.nl.invlalid>
Net::SSH::Perl - How to set remote default file permiss <zhilianghu@gmail.com>
Re: Next Page gets downloaded in Perl <visitprakashindia@gmail.com>
Re: not sure why I am getting the following warning "Us <glennj@ncf.ca>
Re: not sure why I am getting the following warning "Us <krahnj@telus.net>
Re: not sure why I am getting the following warning "Us <ben@morrow.me.uk>
Re: split is not convinient xhoster@gmail.com
Re: split is not convinient xhoster@gmail.com
Re: split is not convinient <Mark.Seger@hp.com>
Re: split is not convinient <Mark.Seger@hp.com>
Re: split is not convinient <krahnj@telus.net>
Re: Using the DBI to connect to an Oracle server w/o co xhoster@gmail.com
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 13 Dec 2007 08:41:15 -0800 (PST)
From: Florian Kaufmann <sensorflo@gmail.com>
Subject: Re: (1)[0] ok but not 1[0]
Message-Id: <4f841449-c9dd-4a33-83f5-5fda498589c4@i29g2000prf.googlegroups.com>
> as useful as you expect. there are dozens of perl tutorials on the net
> and almost all are total crap and full of mistakes and bad writing. in
> many cases the authors also were self taught and thought they can create
> a better doc. turns out that writing technical docs (especially
> tutorials) needs much more programming skill and tons of experience
> which most of those authors don't have (or ever will have). some even
I think you and Charlton Wilbur are perfectly right. By no means will
the document I am writing be better than "Programming Perl" or
http://perldoc.perl.org/ or other established documentation from an
objective point of view. Of course it will have a lot of mistakes and
errors in it, since I will never be a Perl professional but at most an
experience Perl user.
However it will be better from my subjective point of view. Its closer
to the way I am thinking. Its the way I am learning stuff - by
writting things down. Order stuff on paper until I can fully
understand it and have a concise view on it.
I don't know exactly why - "The C++ Programming Language" is a book
that is much more clear to me than "Programming Perl" - and I don't
think its just because I already know C++ better. Its because of the
way things are explained, specified.
Maybe you are right and I shouldn't make it public, because it will
definitely have much more mistakes in it than any official
documentation. So there is a concern that it would only confuse
people. Then again maybe there are people who can profit from it.
Maybe I have to make it very very clear in the abstract and/or
introduction that it's a book written by a non-professional and thus
will contain tons of mistakes.
Flo
------------------------------
Date: Thu, 13 Dec 2007 16:52:42 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: (1)[0] ok but not 1[0]
Message-Id: <x7lk7yk04m.fsf@mail.sysarch.com>
>>>>> "FK" == Florian Kaufmann <sensorflo@gmail.com> writes:
>> as useful as you expect. there are dozens of perl tutorials on the net
>> and almost all are total crap and full of mistakes and bad writing. in
>> many cases the authors also were self taught and thought they can create
>> a better doc. turns out that writing technical docs (especially
>> tutorials) needs much more programming skill and tons of experience
>> which most of those authors don't have (or ever will have). some even
FK> I think you and Charlton Wilbur are perfectly right. By no means will
FK> the document I am writing be better than "Programming Perl" or
FK> http://perldoc.perl.org/ or other established documentation from an
FK> objective point of view. Of course it will have a lot of mistakes and
FK> errors in it, since I will never be a Perl professional but at most an
FK> experience Perl user.
FK> Maybe you are right and I shouldn't make it public, because it will
FK> definitely have much more mistakes in it than any official
FK> documentation. So there is a concern that it would only confuse
FK> people. Then again maybe there are people who can profit from it.
FK> Maybe I have to make it very very clear in the abstract and/or
FK> introduction that it's a book written by a non-professional and thus
FK> will contain tons of mistakes.
then why even do it if it will have so many mistakes? it won't even help
you in your goal to learn perl. and publishing it with a disclaimer that
it has mistakes is even worse. just consider it a private document for
yourself and keep it that way. if you want to get feedback you could
publish the url but make sure it says this is not considered a
professional work. just check out the many tutorials i said exist on the
web. the mistakes and bad writing (and hubris of all thise authors) is
amazing.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Thu, 13 Dec 2007 12:02:42 -0600
From: Neil Cherry <njc@cookie.uucp>
Subject: Re: Can't locate object method "readMessage" ... (Inheritance question)
Message-Id: <slrnfm2su2.rk5.njc@cookie.uucp>
On Wed, 12 Dec 2007 14:14:59 +0000, Ben Morrow wrote:
>
> Quoth ncherry@comcast.net:
>> I'm trying to create a package that overrides the 'new' method in the
>> package: ElkM1::Control but use the rest of the methods as is. Here's
>> my initial portion of my code (without the indent):
>>
>> package ElkM1::Control::Serial;
>> use ElkM1::Control; # inherit from ElkM1::Control
>>
>> #@ISA = qw( "ElkM1::Control" );
>
> our @ISA = qw( ElkM1::Control );
>
> Globals need to be declared with 'our' or 'use vars' (the former is
> preferred, unless you need 5.005 compatibility) under 'use strict'.
> Using extra quotes inside qw// will not do what you want.
Thanks that fixed the problem, I ended up getting a new problem as it
now reports back this (and I know why):
$ perl -w ~/elk_serial.pl
at /home/njc/elk_serial.pl line 14
This is because readMessage (which was not found before) is returning
undef as a value. I'm just a little surprised by the message.
> You don't appear to be overriding the ->new method at all.
> ElkM1::Control does not appear to be on CPAN, so I can't tell if it
> defines one. If you are hoping to inherit methods from
> Device::SerialPort you would need to add that to @ISA, however since
> Device::SerialPort uses hash-based objects, this would not work.
The ElkM1::Control code is not on CPAN, it's on sourceforge but I'm
posting the code here:
http://www.linuxha.com/common/ElkM1/elk_serial.pl
http://www.linuxha.com/common/ElkM1/Serial.pm (goes in ElkM1/Control)
http://www.linuxha.com/common/ElkM1/ElkM1-Control-0.02.tar.gz
The tar.gz file is what I'm trying to inherit.
--
Linux Home Automation Neil Cherry ncherry@linuxha.com
http://www.linuxha.com/ Main site
http://linuxha.blogspot.com/ My HA Blog
Author of: Linux Smart Homes For Dummies
------------------------------
Date: Thu, 13 Dec 2007 08:48:12 -0800 (PST)
From: Todd <xueweizhong@gmail.com>
Subject: Re: FAQ 4.58 How can I know how many entries are in a hash?
Message-Id: <d0974888-4737-4596-8c68-d41a2d33582a@a35g2000prf.googlegroups.com>
brian d foy wrote:
> You're relying on undefined behaviour instead of the documented way to
> do it.
This is from official Perl5.8.8 manual:
perldoc perldata
If you evaluate a hash in scalar context, it returns false if the hash
is empty. If there are any key/value pairs, it returns true; more
precisely, the value returned is a string consisting of the number of
used buckets and the number of allocated buckets, separated by a slash.
Is it still accident or undefined behavior?
-Todd
------------------------------
Date: Thu, 13 Dec 2007 08:57:45 -0800 (PST)
From: Todd <xueweizhong@gmail.com>
Subject: Re: FAQ 4.58 How can I know how many entries are in a hash?
Message-Id: <186d29af-17b8-4eac-8644-be1cb1199617@e23g2000prf.googlegroups.com>
Uri Guttman wrote:
> no, it is not a feature nor is it good. please don't think you will
> discover such a neat thing given how little perl you know. what a hash
> returns in scalar context is documented and it is the a string with the
> number of entries/the number of buckets. it will numerify to the same
> value as scalar keys but it is not warning safe. this is typical newbie
> mistake of not studying the docs and not enabling warnings.
?
-Todd
------------------------------
Date: 13 Dec 2007 17:20:03 GMT
From: xhoster@gmail.com
Subject: Re: FAQ 4.58 How can I know how many entries are in a hash?
Message-Id: <20071213122004.799$R8@newsreader.com>
Joost Diepenmaat <joost@zeekat.nl> wrote:
> On Thu, 13 Dec 2007 05:01:22 -0800, Todd wrote:
>
> >> $num_keys = keys %hash;
> >
> > Seems there is a better way to do it:
> >
> > #! /bin/perl -l
> >
> > %hash = (a=>1, b=>2, c=>3, d=>4, e);
> > $num_keys = %hash+0;
>
> I fail to see how this qualifies as "better".
>
> [..]
>
> > So it means when in numeric(not string) context, %hash return numbers
> > of keys accidently. But anyway, it's a good feature.
>
> This is indeed accidental, and as far as I know not guaranteed to work in
> any future version of perl.
Um, it doesn't even work in current versions of Perl. Unless the number
of used buckets just accidentally happens to be the same as the number of
keys (i.e. if no collisions have occurred)
$ perl -le '$x{$_}=1 foreach 1..1e6; print scalar %x; print scalar keys %x'
644020/1048576
1000000
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: 13 Dec 2007 17:39:51 GMT
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: FAQ 4.58 How can I know how many entries are in a hash?
Message-Id: <47616e67$0$16792$e4fe514c@dreader27.news.xs4all.nl>
On Thu, 13 Dec 2007 17:20:03 +0000, xhoster wrote:
> Um, it doesn't even work in current versions of Perl. Unless the number
> of used buckets just accidentally happens to be the same as the number
> of keys (i.e. if no collisions have occurred)
>
> $ perl -le '$x{$_}=1 foreach 1..1e6; print scalar %x; print scalar keys
Ah right, I was under the impression that the first number was the # of
items, not the number of used buckets. I wasn't sure myself, but I
couldn't find the documentation for this feature.
Thanks for clearing that up.
Joost.
------------------------------
Date: Thu, 13 Dec 2007 17:54:46 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: FAQ 4.58 How can I know how many entries are in a hash?
Message-Id: <x7prxaiioq.fsf@mail.sysarch.com>
>>>>> "T" == Todd <xueweizhong@gmail.com> writes:
T> brian d foy wrote:
>> You're relying on undefined behaviour instead of the documented way to
>> do it.
T> This is from official Perl5.8.8 manual:
T> perldoc perldata
T> If you evaluate a hash in scalar context, it returns false if the hash
T> is empty. If there are any key/value pairs, it returns true; more
T> precisely, the value returned is a string consisting of the number of
T> used buckets and the number of allocated buckets, separated by a slash.
T> Is it still accident or undefined behavior?
it is documented and defined behavior. but YOU DON'T GET THE ISSUE WITH
THE WARNING!!!!!
it is wrong to use that as a number and it will warn (do you enable
warnings? if not, your code and opinions here are not worth as much).
and as other have pointed out and you seemed to have stumbled upon, it
is slower to do +0 besides being dumbass code.
is that clear enough? we know it does that and it has its uses. but it
is not a good way to get the number of keys in a hash.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Thu, 13 Dec 2007 18:07:15 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: FAQ 4.58 How can I know how many entries are in a hash?
Message-Id: <nxe8j.6759$va7.2922@trndny08>
Uri Guttman wrote:
> we know it does that and it has its uses. but it
> is not a good way to get the number of keys in a hash.
s/good//;
because the number of buckets and the number of keys in a hash are only
loosely related.
jue
------------------------------
Date: 13 Dec 2007 18:08:24 GMT
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: FAQ 4.58 How can I know how many entries are in a hash?
Message-Id: <47617518$0$16792$e4fe514c@dreader27.news.xs4all.nl>
On Thu, 13 Dec 2007 08:48:12 -0800, Todd wrote:
> brian d foy wrote:
>> You're relying on undefined behaviour instead of the documented way to
>> do it.
>
> This is from official Perl5.8.8 manual:
>
> perldoc perldata
>
> If you evaluate a hash in scalar context, it returns false if the hash
> is empty. If there are any key/value pairs, it returns true; more
> precisely, the value returned is a string consisting of the number of
> used buckets and the number of allocated buckets, separated by a slash.
Thanks for pointing that out, I couldn't find the page.
> Is it still accident or undefined behavior?
Fine, it's documented behaviour. Still only works for real hashes, and
even worse: the number of buckets in use is not in general equal to the
number of items in a hash (see xhoster's post above)
Joost.
------------------------------
Date: Thu, 13 Dec 2007 18:34:18 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: FAQ 4.58 How can I know how many entries are in a hash?
Message-Id: <x7bq8uiguu.fsf@mail.sysarch.com>
>>>>> "JE" == Jürgen Exner <jurgenex@hotmail.com> writes:
JE> Uri Guttman wrote:
>> we know it does that and it has its uses. but it
>> is not a good way to get the number of keys in a hash.
JE> s/good//;
JE> because the number of buckets and the number of keys in a hash are only
JE> loosely related.
good point which i hadn't noticed before. regardless, i never use %hash
in a scalar context. hell, i can't recall using keys either in a scalar
context. how often do you need to know the number of keys in a hash? i
can think of reporting the number elements in a set and a few similar
reasons. it is not something which needed a thread of this size (other
than to force the OP to learn something).
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Thu, 13 Dec 2007 16:27:25 GMT
From: "John W. Krahn" <krahnj@telus.net>
Subject: Re: Need help integrating a perl command into a shell script.
Message-Id: <47615D6B.9C22D7A9@telus.net>
Ben Morrow wrote:
>
> I would use something more like (untested)
>
> find . -type f | xargs perl -0377 -pi -e'/<string here>/ and
> s,(FILEDIR)="/opt/ ([^/]+) / ([^"]+) ",$1="/new/$2/$3",gx'
>
> (actually I'd probably do the whole thing in Perl, using
> File::Find::Rule, but this is a more shellish solution). The -0377 says
> to read the whole file in one go, which allows you to omit the grep
> altogether while still using perl -p.
That may not work correctly as 0377 is a possible valid character.
perldoc perlrun
[ SNIP ]
-0[digits]
specifies the input record separator (`$/') as an
octal number. If there are no digits, the null
character is the separator. Other switches may
precede or follow the digits. For example, if you
have a version of find which can print filenames
terminated by the null character, you can say this:
find . -name '*.orig' -print0 | perl -n0e unlink
The special value 00 will cause Perl to slurp files
in paragraph mode. The value 0777 will cause Perl to
slurp files whole because there is no legal character
with that value.
John
--
use Perl;
program
fulfillment
------------------------------
Date: Thu, 13 Dec 2007 20:01:54 +0100
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: Need help integrating a perl command into a shell script.
Message-Id: <pan.2007.12.13.19.01.54@rtij.nl.invlalid>
On Thu, 13 Dec 2007 15:16:31 +0000, Ben Morrow wrote:
> Quoth Daryl Rose <rosede12@gmail.com>:
>> The way that I was planning on doing this was something like:
>>
>> find . -exec grep -l <string here> {} ;/ |while read line
>
> You need to learn about xargs. find without xargs is practically
> useless. Something like
>
> find . | xargs grep -l <string> | xargs sed -i -Ee'...'
>
> (-r instead of -E for GNU sed, or omit if you can stand BREs :) ).
Not so. The while construct reads lines, so filenames with spaces get
interpreted correctly. Xargs has the nasty behaviour to seperate on
whitespace. Obviously to handle filenames with newlines you need find -
exec or the gnu extensions find -print0 | xargs -0.
In short, use find -0 | xargs -0 if available and portability is not an
issue, use -exec otherwise. Only use while read if really needed. Never
use find | xargs unless you are absolutely positive there is no
whitespace in any filename.
HTH,
M4
------------------------------
Date: Thu, 13 Dec 2007 10:53:05 -0800 (PST)
From: Joe <zhilianghu@gmail.com>
Subject: Net::SSH::Perl - How to set remote default file permissions
Message-Id: <4e68f1cf-06ed-4be4-a124-4ad074ea6eea@e23g2000prf.googlegroups.com>
When I manually execute a script with Net::SSH::Perl to run a remote
program, the output file has a permission "660"; When I use a web
server to execute the script (the script uses my user account identity/
pass), the remote output file has a permission "600".
How can I make the script to set a default remote file permission
(like "644") so the web server can read the results back? I tried:
($stdout, $stderr, $exit) = $ssh->cmd("umask 022; script");
and it didn't work.
Thanks in advance!
Joe
------------------------------
Date: Thu, 13 Dec 2007 10:56:05 -0800 (PST)
From: Praki <visitprakashindia@gmail.com>
Subject: Re: Next Page gets downloaded in Perl
Message-Id: <85251065-4caf-4d98-84e4-7291b225cdd7@i12g2000prf.googlegroups.com>
On Dec 13, 6:44 pm, Paul Lalli <mri...@gmail.com> wrote:
> On Dec 13, 1:19 am, Praki <visitprakashin...@gmail.com> wrote:
>
>
>
> > Greetings All
>
> > I have a Perl file in which i m doing all the operaions in one file
> > based on the command line arguments.
> > login.cgi
> > .
> > .
> > .
> > $query = new CGI;
> > $sid = $query->cookie('CGISESSID') || $query->param('CGISESSID') ||
> > undef;
> > $submit_value=$query->param("submit");
>
> > if ($sid ne ""){
> > print $query->header( -cookie=>$cookie );}
> > else{
> > print "Content-type: text/html";}
>
> > if ($sid eq "" && $submit_value eq "") {
> > &auth_page(" Login Authentication"," Login Authentication");
> > print "Session id: ";
> > print $sid;
> > print "\n Submit value:";
> > print $query->param("submit");
>
> > print $FORM{'uid'};
> > print $query->header( -cookie=>$cookie );
> > &print_trailer();
> > exit(0);}
>
> > elsif ($sid eq "" && $submit_valu ne "") {
> > &print_header(" Login Authentication"," Login Authentication");
> > &print_trailer();
> > exit(0);}
>
> > .
> > .
> > .
> > when i open the for the first time it should go to
>
> > if ($sid eq "" && $submit_value eq "") this condition as both will be
> > empty. and here i m getting the login information and again i m
> > calling the same file(login.cgi). now as the sumbit value will not be
> > empty it has to go the next condtion
>
> > elsif ($sid eq "" && $submit_valu ne "") .i m validating the
> > credentials here. but the problem here i face is the page info which
> > is to be displayed in the browser is automaticaly asks for File
> > Download. when downloaded that file and check by open in the new
> > window it displays the ouput. i could not understand the problem so
> > plz can u help me in clearing this problem...
>
> > where i m going wrong..
>
> Your content-type header is wrong. It's missing newlines. You're
> using the $query->header() method in the if statement. Why not in the
> else statement?
>
> if ($sid ne ""){
> print $query->header( -cookie=>$cookie );}
>
> else{
> print $query->header( );
>
> }
>
> (A completely separate problem is that you're later trying to print
> the header with some cookies, after you've already printed both header
> and content. That's not going to work. You need to rework your
> logic).
>
> Paul Lalli
Thanks for that info i corected and its working fine....
------------------------------
Date: 13 Dec 2007 16:33:18 GMT
From: Glenn Jackman <glennj@ncf.ca>
Subject: Re: not sure why I am getting the following warning "Useless use of a constant in void context"
Message-Id: <slrnfm2nmf.743.glennj@smeagol.ncf.ca>
At 2007-12-13 09:52AM, "mcardeiro@yahoo.com" wrote:
[...]
> It works exactly as it is supposed to but I keep getting a warning
> from perl "Useless use of a constant in void context at /var/www/web1/
> lib/TS.pm line 1258."
[...]
> $sth->execute( map {$newUser->{$_->{'name'}}} @userTableOrder)
> || error_page("cant execute insert statement at userEntry: $dbh-
> >errstr");
the execute() routine returns a value that you're ignoring.
my $retval = $sth->execute(...);
See perldoc DBI
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
------------------------------
Date: Thu, 13 Dec 2007 16:42:37 GMT
From: "John W. Krahn" <krahnj@telus.net>
Subject: Re: not sure why I am getting the following warning "Useless use of a constant in void context"
Message-Id: <476160FB.C72A98D3@telus.net>
"mcardeiro@yahoo.com" wrote:
>
> I have a line of code that is creating an entry in a mysql table using
> DBI. I have the record values for the insert in a hash reference. A
> separate array holds information about the table structure so that
> when I do my insert I map the hash ref using the array so only
> relevant values are entered into the execute statement and they are in
> the proper order.
>
> It works exactly as it is supposed to but I keep getting a warning
> from perl "Useless use of a constant in void context at /var/www/web1/
> lib/TS.pm line 1258."
>
> I am ignoring it because the code works, but figure I'll throw it out
> there in case I am doing something boneheaded.
>
> heres some code fragments:
>
> ############################
> ### DBI stuff
> ##########################
> $sth = $dbh->prepare("INSERT INTO users VALUES (?,?,?")
> || error_page("cant prepare insert statement at userEntry: $dbh-
> >errstr");
The assignment operator '=' has lower precedence than the OR '||'
operator so $sth is assigned the value returned from $dbh->prepare() or
if that is false it is assigned the value returned from error_page().
You need to either enclose the assignment in parentheses:
( $sth = $dbh->prepare() ) || error_page();
or use the lower precedence 'or' operator.
$sth = $dbh->prepare() or error_page();
John
--
use Perl;
program
fulfillment
------------------------------
Date: Thu, 13 Dec 2007 18:52:29 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: not sure why I am getting the following warning "Useless use of a constant in void context"
Message-Id: <dkb835-90l2.ln1@osiris.mauzo.dyndns.org>
Quoth "mcardeiro@yahoo.com" <mcardeiro@yahoo.com>:
>
> I have a line of code that is creating an entry in a mysql table using
> DBI. I have the record values for the insert in a hash reference. A
> separate array holds information about the table structure so that
> when I do my insert I map the hash ref using the array so only
> relevant values are entered into the execute statement and they are in
> the proper order.
>
> It works exactly as it is supposed to but I keep getting a warning
> from perl "Useless use of a constant in void context at /var/www/web1/
> lib/TS.pm line 1258."
>
> I am ignoring it because the code works, but figure I'll throw it out
> there in case I am doing something boneheaded.
Ignoring warnings is a very bad idea. If the warning really is in error,
enclose the statement in question in a block with an appropriate 'no
warnings' statement and a comment as to why the warning is wrong in this
case. This particular warning usually means you have precedence
problems: for instance, my @x = 1, 2; will warn as the 2 isn't actually
assigned to the array.
> heres some code fragments:
'Code fragments' is not the same as a minimal example we can all run.
> ######################################################################
> ###holds info about the structure of the table and is used to verify
> data before entry
> #######################################################################
This style of block comment is not helpful. It just makes it impossible
to get any overview of the structure of the code. Posting code with more
than 80 columns is rude.
> $sth = $dbh->prepare("INSERT INTO users VALUES (?,?,?") ||
> error_page("cant prepare insert statement at userEntry: $dbh-
> >errstr");
As John said, this is a precedence error: get into the habit of always
using 'and' and 'or' for flow control, not && and ||.
> $sth->execute( map {$newUser->{$_->{'name'}}} @userTableOrder)
> || error_page("cant execute insert statement at userEntry: $dbh-
> >errstr");
One of the annoying things about the -> syntax is that it doesn't
interpolate into strings. This needs to be
... or error_page('can't execute insert statement at userEntry:'
. $dbh->errstr);
or the equivalent with sprintf.
This line doesn't give that warning, in a minimal example I just
constructed. Please give us a whole example.
Ben
------------------------------
Date: 13 Dec 2007 17:05:44 GMT
From: xhoster@gmail.com
Subject: Re: split is not convinient
Message-Id: <20071213120545.559$Zl@newsreader.com>
Todd <xueweizhong@gmail.com> wrote:
> Hi all,
>
> Seems nobody here mentioned the magic that
> split ' ', expr
> is same as
> split /\s+/, expr
We haven't mentioned it because it isn't true. Read the docs more
carefully.
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: 13 Dec 2007 17:12:45 GMT
From: xhoster@gmail.com
Subject: Re: split is not convinient
Message-Id: <20071213121247.032$GC@newsreader.com>
Mark Seger <Mark.Seger@hp.com> wrote:
> Todd wrote:
> > Hi,
> >
> >
> > When I use split, there are serveral case frequently occured:
> >
> > case1:
> >
> > split # default to split ' ', $_
> >
> > case2:
> >
> > split /xxx/ # default to split /xxx/, $_
> >
> > case3:
> >
> > split ' ', expr # ????
> >
> >
> > I do think case3 is most frequently used, but why i've to type ' '
> > every time, why there are no simpler syntax to use in this case?
>
> Maybe I've just been programming for too many years and looked at too
> much code written by people who like to take shortcuts, but I think your
> cases 1/2 should never be used! In fact I avoid any syntax that has a
> default such as "while (<>)" which is why I always use "while
> ($var=<>)".
You are missing a defined, and maybe a my, I would think.
> After all, you never know when someone is going to insert a
> line of code that blows away your default with a different one. In
> other words it's all about supportability!
I write thousands of Perl programs every year. I make no pretense that I
will even be able to find 99% of them six months from now, much less
support them. When writing code I know will be in use for a very long time,
I do make an effort to make is sustainable. But avoiding reasonable
defaults is not among them.
> It's also common for someone to pick up a piece of code in a language
> they're not familiar with and try to figure out what it does with
> minimal effort and having something that is not obvious is a real pain.
If you want people who don't know Perl to be understand your programs,
then don't program in Perl in the first place. Anyway, it isn't like
"split ' ', $_;" is somehow more obvious than "split;". ' ' is the same
as / /, right? Oh, you mean it isn't?
> Are people that lazy that typing a few extra characters to make what
> they're doing more explicit too much of a burden? How is this any
> different from commenting code? Or don't people do that any more either?
I always love commented code, like this:
## increment the variable named "foo"
$foo++;
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: Thu, 13 Dec 2007 13:35:16 -0500
From: Mark Seger <Mark.Seger@hp.com>
To: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: split is not convinient
Message-Id: <47617B64.6020209@hp.com>
A. Sinan Unur wrote:
> Joost Diepenmaat <joost@zeekat.nl> wrote in
> news:4761409b$0$16792$e4fe514c@dreader27.news.xs4all.nl:
>
>> On Thu, 13 Dec 2007 06:09:38 -0600, Tad J McClellan wrote:
>>
>>> Jürgen Exner <jurgenex@hotmail.com> wrote:
>>>> Mark Seger wrote:
>>>>> In fact I avoid any syntax that
>>>>> has a default such as "while (<>)" which is why I always use "while
>>>>> ($var=<>)".
>>>
>>> I agree completely with this one.
>>>
>>> Actually, it should most likely be:
>>>
>>> while ( my $var = <> )
>>>
>> As has been mentioned somewhere above, that's simply incorrect code in
>> most circumstances. while (<handle>) { ... } is a special case and if
>> you really must use a named variable the construct should be
>>
>> while (defined(my $var = <>)) { ... }
>
> As far as I know, the magic test for defined in a while loop is the same
> whether you are reading into a $_ or any lexical variable. Here is a
> short test:
>
> C:\DOCUME~1\asu1\LOCALS~1\Temp\m> cat m.txt
> 0
> 1
>
> 2
> 3
>
> 4
> C:\DOCUME~1\asu1\LOCALS~1\Temp\m> xxd m.txt
> 0000000: 300a 310a 0a32 0a33 0a0a 340a 0a 0.1..2.3..4..
>
> C:\DOCUME~1\asu1\LOCALS~1\Temp\m> cat m.pl
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $name = 'm.txt';
>
> my %dispatch = (
> without_defined => \&without_defined,
> with_defined => \&with_defined,
> without_var => \&without_var,
> );
>
> for my $case ( sort keys %dispatch ) {
> print "$case:\n";
>
> open my $f, '<', $name
> or die "Cannot open '$name': $!";
> $dispatch{ $case }->( $f );
> close $f
> or die "Cannot close '$name': $!";
> }
>
> sub without_defined {
> my $f = shift;
>
> while ( my $line = <$f> ) {
> print decorate($line);
> }
> }
>
> sub with_defined {
> my $f = shift;
>
> while ( defined( my $line = <$f> ) ) {
> print decorate($line);
> }
> }
>
> sub without_var {
> my $f = shift;
>
> while ( <$f> ) {
> print decorate($_);
> }
> }
>
> sub decorate {
> my ($s) = @_;
> $s =~ s/\n/</;
> return "'$s'\n";
> }
>
> __END__
>
> C:\DOCUME~1\asu1\LOCALS~1\Temp\m> m
> Without var assignment in while:
> '0<'
> '1<'
> '<'
> '2<'
> '3<'
> '<'
> '4<'
> '<'
> With 'defined' in while:
> '0<'
> '1<'
> '<'
> '2<'
> '3<'
> '<'
> '4<'
> '<'
> Without 'defined' in while:
> '0<'
> '1<'
> '<'
> '2<'
> '3<'
> '<'
> '4<'
> '<'
Am I going blind are is the output from all 3 examples above the same?
In any event this discussion caused me to go back and read the
documentation since nobody actually said why you needed 'defined' and
the only explanation I could find is that you can't distinguish between
an undef, 0 or '' in a boolean!
I guess the reason I've been lucky up until now is that when leaving off
the 'defined' and reading a file I can never read in a 0 or '' since
each string will have a \n at the end, but I can also appreciate why
including the 'defined' would be good form even if you know it won't
effect your particular situation - or is there something else I'm
missing here? In any event I would continue to argue that's why I think
it's always good form to include the control variable but I'm also sure
I'll never win that argument with this crowd. 8-)
-mark
------------------------------
Date: Thu, 13 Dec 2007 13:47:41 -0500
From: Mark Seger <Mark.Seger@hp.com>
To: xhoster@gmail.com
Subject: Re: split is not convinient
Message-Id: <47617E4D.4000401@hp.com>
>> It's also common for someone to pick up a piece of code in a language
>> they're not familiar with and try to figure out what it does with
>> minimal effort and having something that is not obvious is a real pain.
>
> If you want people who don't know Perl to be understand your programs,
> then don't program in Perl in the first place.
I hope you're not serious. I would suspect many people find themselves
periodically having to look at code in languages they're not familiar
with when there is a problem that needs to be addressed. Sometimes that
code is easy to follow and other times it is not and it can be a
nightmare trying to figure out what's going on. I've had people look at
my perl code who aren't perl programmers yet they're usually able to
figure out what it's doing and in some cases submit patches.
> I always love commented code, like this:
>
> ## increment the variable named "foo"
> $foo++;
at least we can agree on that. 9-)
-mark
------------------------------
Date: Thu, 13 Dec 2007 18:52:13 GMT
From: "John W. Krahn" <krahnj@telus.net>
Subject: Re: split is not convinient
Message-Id: <47617F5B.2644EC69@telus.net>
Mark Seger wrote:
>
> Am I going blind are is the output from all 3 examples above the same?
Yes, because modern versions of perl automagically check for undefined
values.
> In any event this discussion caused me to go back and read the
> documentation since nobody actually said why you needed 'defined' and
> the only explanation I could find is that you can't distinguish between
> an undef, 0 or '' in a boolean!
>
> I guess the reason I've been lucky up until now is that when leaving off
> the 'defined' and reading a file I can never read in a 0 or '' since
> each string will have a \n at the end, but I can also appreciate why
> including the 'defined' would be good form even if you know it won't
> effect your particular situation - or is there something else I'm
> missing here?
Some "text" files may not contain a final newline and if the last
character is "0" the last line read will be false but defined. If the
Input Record Separator is set to the length 1 ($/ = \1;) then any "0"
character read in will be false but defined.
John
--
use Perl;
program
fulfillment
------------------------------
Date: 13 Dec 2007 18:04:48 GMT
From: xhoster@gmail.com
Subject: Re: Using the DBI to connect to an Oracle server w/o connecting to a database
Message-Id: <20071213130449.868$EB@newsreader.com>
Tim <google@hoodfamily.org> wrote:
> I've searched for the answer to this problem and have not seen it
> (which suggests what the answer might be ;-) ). Is it possible to use
> DBI (and DBD::Oracle) to connect to an Oracle server without
> connecting to a database? This would be the DBI equivalent of:
>
> sqlplus /nolog
> SQL> connect user/passwd@database;
/nolog turns sqlplus into something unusual and highly specific
to Oracle. I doubt DBI has an interest in trying to reproduce that.
> or, more similarly:
>
> sqlplus /nolog @my.sql
>
> where my.sql might be:
>
> connect user/passwd@database;
> select ...
> etc.
>
> I'm thinking this is not possible, but I hope it is. Thanks.
Why would you want that? The point of DBI is that it provides a perl
interface for interactively communicating with Oracle. If you just
want to feed a text file through sqlplus with no interaction, then why
bother trying to do it with DBI?
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: 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 1113
***************************************