[24515] in Perl-Users-Digest
Perl-Users Digest, Issue: 6695 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jun 15 18:10:38 2004
Date: Tue, 15 Jun 2004 15:10:08 -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 Tue, 15 Jun 2004 Volume: 10 Number: 6695
Today's topics:
Perl multidimensional arrays with "use strict" (Franck)
Re: Perl multidimensional arrays with "use strict" <ittyspam@yahoo.com>
Re: Perl multidimensional arrays with "use strict" <ebohlman@omsdev.com>
Re: Perl multidimensional arrays with "use strict" <thundergnat@hotmail.com>
Re: Perl multidimensional arrays with "use strict" <thundergnat@hotmail.com>
Re: Reasons to upgrade to latest version of Perl ctcgag@hotmail.com
Re: Reasons to upgrade to latest version of Perl <Andrew@DeFaria.com>
SQL Injection and DBI placeholders <ulrich.herbst@gmx.de>
Re: SQL Injection and DBI placeholders ctcgag@hotmail.com
Re: Struggle with "simple" replacement (Arne)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 15 Jun 2004 08:30:19 -0700
From: francois.caron@sita.aero (Franck)
Subject: Perl multidimensional arrays with "use strict"
Message-Id: <be773a00.0406150730.224706f8@posting.google.com>
Hi!
Little problem there. Can someone tell me what's the problem with
this? The first test below works while the second doesn't. I have to
use strict so I need to know why it doesn't work and what I have to do
to fix the problem in the second test. It complains when I put items
in the second dimension of the array..."Can't use string ("group 0")
as an ARRAY ref while "strict refs" in use at test.pl line 18."
Thx a lot!
Franck
#################################################
#FIRST TEST
$i = 0;
$j = 0;
$k = 0;
@data = ();
for ($i=0; $i<=3; $i++){
$data[$i] = "group $i";
for ($j=0; $j<=3; $j++){
$temp1 = $j+$i+10;
$data[$i][$j] = "user $temp1";
for ($k=0; $k<=3; $k++){
$temp2 = $k+100;
$data[$i][$j][$k] = " $temp2";
}
}
}
for ($i=0; $i<=3; $i++){
print "\n\n$data[$i] own: ";
for ($j=0; $j<=3; $j++){
print "\n $data[$i][$j] who have privilege ";
for ($k=0; $k<=3; $k++){
print "$data[$i][$j][$k],";
}
}
}
#################################################
#################################################
#SECOND TEST
use strict;
use warnings;
my $i = 0;
my $j = 0;
my $k = 0;
@::data = ();
my $temp1;
my $temp2;
for ($i=0; $i<=3; $i++){
$::data[$i] = "group $i";
for ($j=0; $j<=3; $j++){
$temp1 = $j+$i+10;
$::data[$i][$j] = "user $temp1";
for ($k=0; $k<=3; $k++){
$temp2 = $k+100;
$::data[$i][$j][$k] = " $temp2";
}
}
}
for ($i=0; $i<=3; $i++){
print "\n\n$::data[$i] own: ";
for ($j=0; $j<=3; $j++){
print "\n $::data[$i][$j] who have privilege ";
for ($k=0; $k<=3; $k++){
print "$::data[$i][$j][$k],";
}
}
}
#################################################
------------------------------
Date: Tue, 15 Jun 2004 13:06:42 -0400
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: Perl multidimensional arrays with "use strict"
Message-Id: <20040615130105.T20623@dishwasher.cs.rpi.edu>
On Tue, 15 Jun 2004, Franck wrote:
> Hi!
>
> Little problem there. Can someone tell me what's the problem with
> this? The first test below works while the second doesn't. I have to
> use strict so I need to know why it doesn't work and what I have to do
> to fix the problem in the second test. It complains when I put items
> in the second dimension of the array..."Can't use string ("group 0")
> as an ARRAY ref while "strict refs" in use at test.pl line 18."
>
> Thx a lot!
> Franck
>
> #################################################
> #FIRST TEST
<code without strict snipped>
> #################################################
> #SECOND TEST
> use strict;
> use warnings;
>
>
> my $i = 0;
> my $j = 0;
> my $k = 0;
>
> @::data = ();
Why are you using a global variable? This isn't 'wrong' per say, but it
is unusual. You usually want a lexical variable, especially with
strictures enabled:
my @data;
> my $temp1;
> my $temp2;
>
> for ($i=0; $i<=3; $i++){
> $::data[$i] = "group $i";
Okay, here you are filling the array @data with text strings. Position 0
contains "group 0", position 1 contains "group 1", etc.
>
> for ($j=0; $j<=3; $j++){
> $temp1 = $j+$i+10;
> $::data[$i][$j] = "user $temp1";
Here you're suddenly deciding that @data no longer contains text strings,
and instead now contains references to arrays. Why? The error you're
rightly getting is telling you that $data[$i] contains the string "group
$i", which you are attempting to convert to an array reference.
>
> for ($k=0; $k<=3; $k++){
> $temp2 = $k+100;
> $::data[$i][$j][$k] = " $temp2";
Same problem. Assuming the above worked, $data[$i][$j] would contain the
text string "user 10" (for example). Now you're trying to use that string
as an array reference.
> }
> }
> }
>
> for ($i=0; $i<=3; $i++){
> print "\n\n$::data[$i] own: ";
> for ($j=0; $j<=3; $j++){
> print "\n $::data[$i][$j] who have privilege ";
> for ($k=0; $k<=3; $k++){
> print "$::data[$i][$j][$k],";
> }
> }
> }
> #################################################
>
I think it would be good for you to define what your goal is, rather than
defining what you think you have to do to obtain that goal. Using
symbolic references (which is the official term for what you're trying to
do here) is A Bad Idea, which is why it's disabled by use strict;. So,
what is it you *want* to do? Once you tell us that, we can help you
figure out the correct way of obtaining that goal.
Paul Lalli
------------------------------
Date: 15 Jun 2004 17:28:09 GMT
From: Eric Bohlman <ebohlman@omsdev.com>
Subject: Re: Perl multidimensional arrays with "use strict"
Message-Id: <Xns95097F8C22E5Febohlmanomsdevcom@130.133.1.4>
francois.caron@sita.aero (Franck) wrote in
news:be773a00.0406150730.224706f8@posting.google.com:
> Little problem there. Can someone tell me what's the problem with
> this? The first test below works while the second doesn't. I have to
> use strict so I need to know why it doesn't work and what I have to do
> to fix the problem in the second test. It complains when I put items
> in the second dimension of the array..."Can't use string ("group 0")
> as an ARRAY ref while "strict refs" in use at test.pl line 18."
Because you've inadvertently created a symbolic reference instead of an
array of array references ("AoA") (which is how a multidimensional array is
implemented.
> #FIRST TEST
> $i = 0;
> $j = 0;
> $k = 0;
>
> @data = ();
>
> for ($i=0; $i<=3; $i++){
> $data[$i] = "group $i";
First time around, $data[0] eq 'group 0'
>
> for ($j=0; $j<=3; $j++){
> $temp1 = $j+$i+10;
> $data[$i][$j] = "user $temp1";
And this assignment actually works out to
${'group 0'}[0] = "user 10";
because it's using $data[0] as a symref, which is disallowed under strict.
I suspect you really want a multidimensional *hash* ("HoH") here rather
than an array. Reading perlreftut, perllol, and perldsc should set you in
the right direction.
------------------------------
Date: Tue, 15 Jun 2004 14:49:15 -0400
From: thundergnat <thundergnat@hotmail.com>
Subject: Re: Perl multidimensional arrays with "use strict"
Message-Id: <40cf44a2$0$2993$61fed72c@news.rcn.com>
Franck wrote:
> Hi!
>
> Little problem there. Can someone tell me what's the problem with
> this? The first test below works while the second doesn't. I have to
> use strict so I need to know why it doesn't work and what I have to do
> to fix the problem in the second test. It complains when I put items
> in the second dimension of the array..."Can't use string ("group 0")
> as an ARRAY ref while "strict refs" in use at test.pl line 18."
>
> Thx a lot!
> Franck
>
Well, very much like it is saying, you are trying to use a string as an
array ref. When you create lists of lists: $::data[$i][$j], the $i array
holds references to the arrays indexed by the $j variable. You can't
just arbitrarily assign strings to them, they are already in use as
array references.
> #################################################
> #SECOND TEST
> use strict;
> use warnings;
>
>
> my $i = 0;
> my $j = 0;
> my $k = 0;
>
> @::data = ();
> my $temp1;
> my $temp2;
>
> for ($i=0; $i<=3; $i++){
> $::data[$i] = "group $i";
>
> for ($j=0; $j<=3; $j++){
> $temp1 = $j+$i+10;
> $::data[$i][$j] = "user $temp1";
>
> for ($k=0; $k<=3; $k++){
> $temp2 = $k+100;
> $::data[$i][$j][$k] = " $temp2";
> }
> }
> }
>
> for ($i=0; $i<=3; $i++){
> print "\n\n$::data[$i] own: ";
> for ($j=0; $j<=3; $j++){
> print "\n $::data[$i][$j] who have privilege ";
> for ($k=0; $k<=3; $k++){
> print "$::data[$i][$j][$k],";
> }
> }
> }
> #################################################
I would rewrite this this way:
Since you are just using the indicies to calculate the various
parameters, you can just perform the calculations when you need them,
rather than precalculating them and storing them. Declare your variables
for the smallest scope necessary. Avoid using unnecessary temp
variables. I'm not sure why you were using a package variable (@::data),
it's not really wrong if you need the varible to be global, but you
should probably avoid it unless you definitely need the global scope.
You may be served better by a different data structure. Without knowing
more about what kinds of information you are storing, how much of it
there will be and how you need to retrieve it, it is difficult to say.
You might want to look at the perl docs about lists of lists.
perldoc perllol - or -
http://www.perl.com/doc/manual/html/pod/perllol.html
(Anyway, I doubt if this is what you really /wanted/, but it is
essentially what you were /trying/ to do.)
use strict;
use warnings;
my @data = ();
for my $i (0..3){
for my $j (0..3){
for my $k (0..3){
$data[$i][$j][$k] = $k+100;
}
}
}
for my $i (0..3){
print "\n\ngroup $i own: ";
for my $j (0..3){
print "\n user ",$j+$i+10,' who have privilege';
for my $k (0..3){
print ' ',$k+100,",";
}
}
}
------------------------------
Date: Tue, 15 Jun 2004 14:58:57 -0400
From: thundergnat <thundergnat@hotmail.com>
Subject: Re: Perl multidimensional arrays with "use strict"
Message-Id: <40cf46e9$0$2986$61fed72c@news.rcn.com>
thundergnat wrote:
>
> use strict;
> use warnings;
>
> my @data = ();
>
> for my $i (0..3){
> for my $j (0..3){
> for my $k (0..3){
> $data[$i][$j][$k] = $k+100;
> }
> }
> }
>
> for my $i (0..3){
> print "\n\ngroup $i own: ";
> for my $j (0..3){
> print "\n user ",$j+$i+10,' who have privilege';
> for my $k (0..3){
> print ' ',$k+100,",";
> }
> }
> }
Ummm. Make that:
use strict;
use warnings;
my @data = ();
for my $i (0..3){
for my $j (0..3){
for my $k (0..3){
$data[$i][$j][$k] = $k+100;
}
}
}
for my $i (0..3){
print "\n\ngroup $i own: ";
for my $j (0..3){
print "\n user ",$j+$i+10,' who have privilege';
for my $k (0..3){
print ' ',$data[$i][$j][$k],",";
}
}
}
------------------------------
Date: 15 Jun 2004 16:08:33 GMT
From: ctcgag@hotmail.com
Subject: Re: Reasons to upgrade to latest version of Perl
Message-Id: <20040615120833.850$HC@newsreader.com>
Mothra <rick.cross@btopenworld.com> wrote:
> Can anyone give me some good reasons to upgrade Perl to the lastest
> stable release?
Eventually you will want to upgrade your server hardware, which will
probably mean an OS upgrade, which will probably come with a newer Perl.
It is often troublesome to upgrade 3 things simultaneously, as you don't
know which one to blame any particular problem on. So beat them to the
punch by upgrading perl first.
> Apart from it being the latest stable release?
> Unfortunately that one doesn't seem to impress managers enough to
> upgrade from 5.005_03.
Well, what do you use Perl for? Are you writing a lot of new code or just
maintaining old code?
I've noticed that Perl 5.005 hashing algorithm was much more likely than
later versions to degenerate into a linked list when used on IDs with
built-in check sums.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: Tue, 15 Jun 2004 14:28:17 -0700
From: "Andrew DeFaria" <Andrew@DeFaria.com>
Subject: Re: Reasons to upgrade to latest version of Perl
Message-Id: <7be8b$40cf69d5$c09cfc9$28731@msgid.meganewsservers.com>
"Mothra" <rick.cross@btopenworld.com> wrote in message
news:Fqyzc.6640009$iA2.753837@news.easynews.com...
> Can anyone give me some good reasons to upgrade Perl to the lastest
> stable release? Apart from it being the latest stable release?
> Unfortunately that one doesn't seem to impress managers enough to
> upgrade from 5.005_03.
In a word - support! You always want the latest stable release because it'll
be the best supported (most widely used, etc, etc). If you're managers still
think that that is not a good enough reason then you need new managers!
------------------------------
Date: 15 Jun 2004 17:18:08 +0200
From: Ulrich Herbst <ulrich.herbst@gmx.de>
Subject: SQL Injection and DBI placeholders
Message-Id: <87wu28df4f.fsf@pculi.herbst.fam>
Hi!
I want to insert data from user input into a database (with DBI):
my $sth = $dbh->prepare(q(
INSERT INTO table
(col1,col2,col3)
VALUES (? ,? ,? )
));
my $rc=$sth->execute($data1,$data2,$data3);
Have I to deal with SQL Injection if I use DBI placeholders ?
With other words: Have I to "untaint" $data1, $data2, $data3 ?
(Yes, I know, that bind variables/placeholders can be much faster)
Uli
--
'''
(0 0)
+------oOO----(_)--------------+
| |
| Ulrich Herbst |
| |
+-------------------oOO--------+
|__|__|
|| ||
ooO Ooo
------------------------------
Date: 15 Jun 2004 16:14:35 GMT
From: ctcgag@hotmail.com
Subject: Re: SQL Injection and DBI placeholders
Message-Id: <20040615121435.419$6o@newsreader.com>
Ulrich Herbst <ulrich.herbst@gmx.de> wrote:
> Hi!
>
> I want to insert data from user input into a database (with DBI):
>
> my $sth = $dbh->prepare(q(
> INSERT INTO table
> (col1,col2,col3)
> VALUES (? ,? ,? )
> ));
> my $rc=$sth->execute($data1,$data2,$data3);
>
> Have I to deal with SQL Injection if I use DBI placeholders ?
No. Just by using placeholders you have already dealt with that
problem.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: 15 Jun 2004 12:45:22 -0700
From: kesting@gmx.net (Arne)
Subject: Re: Struggle with "simple" replacement
Message-Id: <ff144788.0406151145.6c14f457@posting.google.com>
Truth is simple:
> perldoc -f quotemeta
is the golden hint. Thank u so much Sam. (was my first posting so that
I cannot back off...)
Arne
------------------------------
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 6695
***************************************