[23824] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 6027 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jan 29 21:11:28 2004

Date: Thu, 29 Jan 2004 18:06:00 -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, 29 Jan 2004     Volume: 10 Number: 6027

Today's topics:
    Re: parallel processing (Stuart Kendrick)
        passing changing amounts of variables to subroutine (Mike Solomon)
    Re: passing changing amounts of variables to subroutine <ittyspam@yahoo.com>
    Re: passing changing amounts of variables to subroutine <karlheinz.weindl@oooonlinehome.de>
    Re: passing changing amounts of variables to subroutine <usenet@expires03-2004.tinita.de>
    Re: passing changing amounts of variables to subroutine <ittyspam@yahoo.com>
    Re: passing changing amounts of variables to subroutine <tore@aursand.no>
    Re: passing changing amounts of variables to subroutine <usenet@expires03-2004.tinita.de>
    Re: passing changing amounts of variables to subroutine (Mike Solomon)
    Re: passing changing amounts of variables to subroutine <ittyspam@yahoo.com>
    Re: passing changing amounts of variables to subroutine ctcgag@hotmail.com
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 21 Jan 2004 08:22:50 -0800
From: skendric@fhcrc.org (Stuart Kendrick)
Subject: Re: parallel processing
Message-Id: <62dbf7f1.0401210822.4a377ceb@posting.google.com>

thanx for all the input.  turns out that the parallel processes needed
read/write access to data structures within the main process ... so i
used threads and threads:shared.  thanx also for the stylistic
pointers ... i'm pulling out & and -w from my scripts now.

i'm pleased with the result ...
http://www.skendric.com/device/Cisco/shutdown-network ... a script
which disables the access layer of our network in about a minute,
thanx to the use of threading ... one thread per ethernet switch.  i
hope we'll never use it ... but in the event of a catastrophic worm
infection, i'm going to be real grateful that i have this tool
available to me.

--sk

Stuart Kendrick
FHCRC


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

Date: 21 Jan 2004 09:04:03 -0800
From: mike_solomon@lineone.net (Mike Solomon)
Subject: passing changing amounts of variables to subroutine
Message-Id: <56568be5.0401210904.409d0903@posting.google.com>

The following bit of my script passes variables to a subroutine for
use as placeholders to update a database using DBI
As I have written it it works for 2 placeholders but I want it to work
for any amount

Any ideas

#update Address
my $sql = qq {
            UPDATE ADDRESS
        	SET Address_Line_1 = ?,
                Address_Line_2 = ?,
                State_Province = '$exData{$id}{county}',
                Country = '$exData{$id}{country}',
                Zip_Code = '$exData{$id}{postcode}',
                City = '$exData{$id}{city}'
            WHERE Client_Id = '$Client_Id'
            AND Address_Id = 0
            AND Address_Line_1 is NULL
};

my $error = SqlExecuteP($sql, $exData{$id}{address1},
$exData{$id}{address2});


sub SqlExecuteP {
	#Execute Sql or write error to errr log & return
	#expect sql statement
	my ($sql , $p1, $p2) = @_;

    $g_data = $g_dbh->prepare($sql);
	my $rows = $g_data->execute($p1, $p2);

	#check for error - if error print error & exit
	my $sqlError = DBI::errstr;

	if ( $sqlError ) {
		$sqlError = $sql . "\n" . $sqlError;
		return $sqlError;
	}

	#OEO = 0 rows
	$rows =~ s/0E0/0/;
	return $rows;
} #end SqlExecuteP
#-----------------------------------------------------------------------


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

Date: Wed, 21 Jan 2004 13:11:35 -0500
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: passing changing amounts of variables to subroutine
Message-Id: <20040121130744.R28498@dishwasher.cs.rpi.edu>

On Wed, 21 Jan 2004, Mike Solomon wrote:

> The following bit of my script passes variables to a subroutine for
> use as placeholders to update a database using DBI
> As I have written it it works for 2 placeholders but I want it to work
> for any amount
>
> Any ideas
>
> #update Address
> my $sql = qq {
>             UPDATE ADDRESS
>         	SET Address_Line_1 = ?,
>                 Address_Line_2 = ?,
>                 State_Province = '$exData{$id}{county}',
>                 Country = '$exData{$id}{country}',
>                 Zip_Code = '$exData{$id}{postcode}',
>                 City = '$exData{$id}{city}'
>             WHERE Client_Id = '$Client_Id'
>             AND Address_Id = 0
>             AND Address_Line_1 is NULL
> };
>
> my $error = SqlExecuteP($sql, $exData{$id}{address1},
> $exData{$id}{address2});
>
>
> sub SqlExecuteP {
> 	#Execute Sql or write error to errr log & return
> 	#expect sql statement
> 	my ($sql , $p1, $p2) = @_;
>
>     $g_data = $g_dbh->prepare($sql);
> 	my $rows = $g_data->execute($p1, $p2);
>
> 	#check for error - if error print error & exit
> 	my $sqlError = DBI::errstr;
>
> 	if ( $sqlError ) {
> 		$sqlError = $sql . "\n" . $sqlError;
> 		return $sqlError;
> 	}
>
> 	#OEO = 0 rows
> 	$rows =~ s/0E0/0/;
> 	return $rows;
> } #end SqlExecuteP
> #-----------------------------------------------------------------------
>

I might not be understanding your problem... you want to execute your SQL
statement with whatever values are passed in to the subroutine, not
knowing how many will be passed in.  Any that are not passed in should
default to a specific value.  Is that correct?  If so, I would make all
the values in the SQL statement placeholders, and then in the subroutine
do:

my $sql = shift;
my $p[0] = $_[0] or $default_value1;
my $p[1] = $_[1] or $default_value2;
my $p[2] = $_[2] or $default_value3;
#etc ...

my $rows = $g_data->execute(@p);


Paul Lalli



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

Date: Wed, 21 Jan 2004 19:17:36 +0100
From: Karlheinz Weindl <karlheinz.weindl@oooonlinehome.de>
Subject: Re: passing changing amounts of variables to subroutine
Message-Id: <bumfs1$u5s$1@online.de>

Mike Solomon schrieb:

> The following bit of my script passes variables to a subroutine for
> use as placeholders to update a database using DBI
> As I have written it it works for 2 placeholders but I want it to work
> for any amount

[...]

> sub SqlExecuteP {
> 	#Execute Sql or write error to errr log & return
> 	#expect sql statement
> 	my ($sql , $p1, $p2) = @_;

         my ($sql, @params) = @_;

> 
>     $g_data = $g_dbh->prepare($sql);
> 	my $rows = $g_data->execute($p1, $p2);

         my $rows = $g_data->execute(@params);

Is it that what you're looking for?

Karlheinz


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

Date: 21 Jan 2004 18:19:19 GMT
From: Tina Mueller <usenet@expires03-2004.tinita.de>
Subject: Re: passing changing amounts of variables to subroutine
Message-Id: <bumfr6$ihgqk$1@ID-24002.news.uni-berlin.de>

Mike Solomon wrote:
> The following bit of my script passes variables to a subroutine for
> use as placeholders to update a database using DBI
> As I have written it it works for 2 placeholders but I want it to work
> for any amount

why not just use an array?

> sub SqlExecuteP {
> 	#Execute Sql or write error to errr log & return
> 	#expect sql statement

 	my ($sql , @p) = @_;

> 	my $rows = $g_data->execute($p1, $p2);

 	my $rows = $g_data->execute(@p);

hth, tina
-- 
http://www.tinita.de/ http://www.perlquotes.de/ http://www.darkdance.net/
Enter the Doors of P e  r   c    e     p      t       i        o        n
http://www.perl-community.de/                       http://berlin.pm.org/
- the above mail address expires end of march 2004 -


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

Date: Wed, 21 Jan 2004 13:21:12 -0500
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: passing changing amounts of variables to subroutine
Message-Id: <20040121131510.M28498@dishwasher.cs.rpi.edu>

On Wed, 21 Jan 2004, Paul Lalli wrote:

> On Wed, 21 Jan 2004, Mike Solomon wrote:
>
> > The following bit of my script passes variables to a subroutine for
> > use as placeholders to update a database using DBI
> > As I have written it it works for 2 placeholders but I want it to work
> > for any amount
> >
>
> I might not be understanding your problem... you want to execute your SQL
> statement with whatever values are passed in to the subroutine, not
> knowing how many will be passed in.  Any that are not passed in should
> default to a specific value.  Is that correct?  If so, I would make all
> the values in the SQL statement placeholders, and then in the subroutine
> do:
>
> my $sql = shift;
> my $p[0] = $_[0] or $default_value1;
> my $p[1] = $_[1] or $default_value2;
> my $p[2] = $_[2] or $default_value3;
> #etc ...
>
> my $rows = $g_data->execute(@p);

Better way of doing what I just said, assuming you have all your defaults
in an array: (untested)

my $sql = shift;
my $rows = $g_data->execute(@_, @defaults[@_..$#defaults]);

Paul Lalli


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

Date: Thu, 22 Jan 2004 00:11:32 +0100
From: Tore Aursand <tore@aursand.no>
Subject: Re: passing changing amounts of variables to subroutine
Message-Id: <pan.2004.01.21.23.04.54.601044@aursand.no>

On Wed, 21 Jan 2004 09:04:03 -0800, Mike Solomon wrote:
> The following bit of my script passes variables to a subroutine for
> use as placeholders to update a database using DBI
> As I have written it it works for 2 placeholders but I want it to work
> for any amount

That's because you tell the sub-routine to except only two;

> sub SqlExecuteP {
> 	#Execute Sql or write error to errr log & return
> 	#expect sql statement
> 	my ($sql , $p1, $p2) = @_;

This line could rather have been written like this:

  my ( $sql, @p ) = @_;

> 	my $rows = $g_data->execute($p1, $p2);

And then:

  my $rows = $g_data->execute( @p );

> 	#check for error - if error print error & exit
> 	my $sqlError = DBI::errstr;
> 
> 	if ( $sqlError ) {
> 		$sqlError = $sql . "\n" . $sqlError;
> 		return $sqlError;
> 	}

Why all this fuzz.  Isn't it enough with the following?

  my $rows = $g_data->execute( @p ) or return $sql . "\n" . DBI::errstr;

> 	#OEO = 0 rows
> 	$rows =~ s/0E0/0/;
> 	return $rows;

Does it really return '0E0' when zero rows returned?  Strange.  I would
have written this as one line;

  return ( $rows =~ m,^\d+$, ) ? $rows : 0;

A matter of taste, I guess.


-- 
Tore Aursand <tore@aursand.no>
"Every man usually has something he can do better than anyone else.
 Usually it is reading his own handwriting." -- Unknown


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

Date: 22 Jan 2004 00:09:25 GMT
From: Tina Mueller <usenet@expires03-2004.tinita.de>
Subject: Re: passing changing amounts of variables to subroutine
Message-Id: <bun4bl$jh6r2$1@ID-24002.news.uni-berlin.de>

Tore Aursand wrote:
> Does it really return '0E0' when zero rows returned?  Strange.  I would
> have written this as one line;

>   return ( $rows =~ m,^\d+$, ) ? $rows : 0;

> A matter of taste, I guess.

or just:
  return $rows + 0; # works with warnings enabled

regards, tina
-- 
http://www.tinita.de/ http://www.perlquotes.de/ http://www.darkdance.net/
Enter the Doors of P e  r   c    e     p      t       i        o        n
http://www.perl-community.de/                       http://berlin.pm.org/
- the above mail address expires end of march 2004 -


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

Date: 22 Jan 2004 01:47:07 -0800
From: mike_solomon@lineone.net (Mike Solomon)
Subject: Re: passing changing amounts of variables to subroutine
Message-Id: <56568be5.0401220147.394d3e2@posting.google.com>

Karlheinz Weindl <karlheinz.weindl@oooonlinehome.de> wrote in message news:<bumfs1$u5s$1@online.de>...
> Mike Solomon schrieb:
> 
> > The following bit of my script passes variables to a subroutine for
> > use as placeholders to update a database using DBI
> > As I have written it it works for 2 placeholders but I want it to work
> > for any amount
> 
> [...]
> 
> > sub SqlExecuteP {
> > 	#Execute Sql or write error to errr log & return
> > 	#expect sql statement
> > 	my ($sql , $p1, $p2) = @_;
> 
>          my ($sql, @params) = @_;
> 
> > 
> >     $g_data = $g_dbh->prepare($sql);
> > 	my $rows = $g_data->execute($p1, $p2);
> 
>          my $rows = $g_data->execute(@params);
> 
> Is it that what you're looking for?
> 
> Karlheinz

That's exactly what I wanted

I think I looked at it to hard and missed the obvious

Thanks everyone


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

Date: Thu, 22 Jan 2004 08:01:22 -0500
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: passing changing amounts of variables to subroutine
Message-Id: <20040122075340.J28498@dishwasher.cs.rpi.edu>

On Wed, 22 Jan 2004, Tina Mueller wrote:

> Tore Aursand wrote:
> > Does it really return '0E0' when zero rows returned?  Strange.  I would
> > have written this as one line;
>
> >   return ( $rows =~ m,^\d+$, ) ? $rows : 0;
>
> > A matter of taste, I guess.
>
> or just:
>   return $rows + 0; # works with warnings enabled
>

Why are you futzing with this return value in the first place?  execute()
very intentionally and specifically returns "0E0" if there are zero rows
returned, for very good reasons.  It is so you can make the following
kinds of determinations:


$rows = $g_data->execute(@params);

if (!$rows){
	print "Error with the SQL execution\n";
} elsif ($rows == 0){
	print "No rows were affected\n";
} else {
	print "Updated $rows rows\n";
}


In other words, execute() will return a false value on error only.  It
returns a true value which evaluates to zero in numeric context if the
execute() succeeded, but no rows were updated.  This is what was intended,
and should be how it was used.  The way you've changed it, you have no way
of knowing if $error is false because you have a SQL syntax error or if
there were simply no rows that matched your statement.

Paul Lalli


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

Date: 22 Jan 2004 17:34:17 GMT
From: ctcgag@hotmail.com
Subject: Re: passing changing amounts of variables to subroutine
Message-Id: <20040122123417.866$Ye@newsreader.com>

Paul Lalli <ittyspam@yahoo.com> wrote:
> On Wed, 22 Jan 2004, Tina Mueller wrote:
>
> > Tore Aursand wrote:
> > > Does it really return '0E0' when zero rows returned?  Strange.  I
> > > would have written this as one line;
> >
> > >   return ( $rows =~ m,^\d+$, ) ? $rows : 0;
> >
> > > A matter of taste, I guess.
> >
> > or just:
> >   return $rows + 0; # works with warnings enabled
> >
>
> Why are you futzing with this return value in the first place?  execute()
> very intentionally and specifically returns "0E0" if there are zero rows
> returned, for very good reasons.

Hell, I want to know why he's futzing with a half-assed subroutine that
is nothing but a brain-damaged wrapper around DBI->execute() in the first
place.

Maintaining "abstraction for abstraction's sake" code like this
was the bane of my job, until I just chucked it all.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service              New Rate! $9.95/Month 50GB


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

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 6027
***************************************


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