For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > May 2004 > Sending a parameter to a subroutine









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Sending a parameter to a subroutine
J. Alejandro Ceballos Z.

2004-05-22, 11:31 am


I've created a subrutine to acces DB, but while sending the Query it get =
an=20
error.

My function looks like:

sub DbSqlQuery
{
local $str_query =3D @_ if @_;
print $str_query;
<... blah, blah ..>
}

And my program call it like:

<... blah, blah ..>
$str_Query =3D "SELECT admin_ID, admin_name FROM admins WHERE (admin_logi=
n =3D=20
'$str_Login');
print "Content-type: text/plain\n\n TEST: $str_Query :: \n";
&DbSqlQuery($str_Query);
<... blah, blah ..>


When I run, I get the following output:

SELECT admin_ID, admin_name FROM admins WHERE (admin_login =3D 'my value'=
) :: 1
ERROR: 1
1064 (You have an error in your SQL syntax near '1 ' at line 1)




Where it comes the '1'?

I must use pointers or similar?



--=20


saludos,


J. Alejandro Ceballos Z. |
---------------------------+---------------------------
http://alejandro.ceballos.info |
buzon@alejandro.ceballos.info | "Las batallas contra las mujeres
-------------------------------+ son las =FAnicas que se ganan huyendo=
".
|
| -- Napoleon Bonaparte.

Paul Archer

2004-05-22, 11:31 am

> I've created a subrutine to acces DB, but while sending the Query it get =
an
> error.
>
> My function looks like:
>
> sub DbSqlQuery
> {
> local $str_query =3D @_ if @_;


(Why are you using local? 'local' is deprecated in favor of 'my'.)

$str_query is a scalar, @_ is an array. Putting a scalar on the left of an
assignment forces the assignment to be scalar context--and in scalar contex=
t
the array returns the number of elements (1 in this case).
Try something more like:
my $str_query =3D $_[0];
-- or --
my $str_query =3D shift @_; # grabs first element of @_ array


> print $str_query;
> <... blah, blah ..>
> }
>
> And my program call it like:
>
> <... blah, blah ..>
> $str_Query =3D "SELECT admin_ID, admin_name FROM admins WHERE (admin_logi=

n =3D
> '$str_Login');
> print "Content-type: text/plain\n\n TEST: $str_Query :: \n";
> &DbSqlQuery($str_Query);
> <... blah, blah ..>
>
>
> When I run, I get the following output:
>
> SELECT admin_ID, admin_name FROM admins WHERE (admin_login =3D 'my value'=

) :: 1
> ERROR: 1
> =091064 (You have an error in your SQL syntax near '1 ' at line 1)
>
>
>
>
> Where it comes the '1'?
>
> I must use pointers or similar?
>
>
>
> --
>
>
> saludos,
>
>
> J. Alejandro Ceballos Z. |
> ---------------------------+---------------------------
> http://alejandro.ceballos.info |
> buzon@alejandro.ceballos.info | "Las batallas contra las mujeres
> -------------------------------+ son las =FAnicas que se ganan huyendo=

".
> |
> | -- Napoleon Bonaparte.
>
>
> --
> To unsubscribe, e-mail: beginners-cgi-unsubscribe@perl.org
> For additional commands, e-mail: beginners-cgi-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>


--------------------------------------------------------------
"I'll say this about Linux: it's the first time I've seen Unix
on the right platform."--Steve Ballmer, president of Microsoft
(NB: Microsoft used to own SCO, which did, and still does,
produce a Unix for the Intel platform.)
--------------------------------------------------------------
Brad Lhotsky

2004-05-22, 11:31 am

Ok, there's a few points I'd like to make.

read: perldoc perlvar AND perldoc perlsub

before continuing ;)





















#1 local() is for perl's Variables, my() is for your variables.

#2 an array in scalar context evaluates to the number of elements in the
list, hence the 1.

I believe what you're looking for is something like this:

sub dbQuery {
my $query = shift; # shifts from the beginning of @_ if no
# other array is provided.
# do stuff with query;
}

OR

sub dbQuery {
my ($query) = @_;
# do stuff with query;
}

In the second example, the () introduces list context to the left side of
the operand, which then causes @_ to be evaluated as you expected. It
seems like an annoyance, but context is one of perl's most powerful
features as its more like natural language.

Enjoy,

On Tue, Apr 13, 2004 at 02:25:32PM -0500, J. Alejandro Ceballos Z. wrote:
>
> I've created a subrutine to acces DB, but while sending the Query it get an
> error.
>
> My function looks like:
>
> sub DbSqlQuery
> {
> local $str_query = @_ if @_;
> print $str_query;
> <... blah, blah ..>
> }
>
> And my program call it like:
>
> <... blah, blah ..>
> $str_Query = "SELECT admin_ID, admin_name FROM admins WHERE (admin_login =
> '$str_Login');
> print "Content-type: text/plain\n\n TEST: $str_Query :: \n";
> &DbSqlQuery($str_Query);
> <... blah, blah ..>
>
>
> When I run, I get the following output:
>
> SELECT admin_ID, admin_name FROM admins WHERE (admin_login = 'my value') ::
> 1
> ERROR: 1
> 1064 (You have an error in your SQL syntax near '1 ' at line 1)
>
>
>
>
> Where it comes the '1'?
>
> I must use pointers or similar?
>
>
>
> --
>
>
> saludos,
>
>
> J. Alejandro Ceballos Z. |
> ---------------------------+---------------------------
> http://alejandro.ceballos.info |
> buzon@alejandro.ceballos.info | "Las batallas contra las mujeres
> -------------------------------+ son las ?nicas que se ganan huyendo".
> |
> | -- Napoleon Bonaparte.
>
>
> --
> To unsubscribe, e-mail: beginners-cgi-unsubscribe@perl.org
> For additional commands, e-mail: beginners-cgi-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>


--
Brad Lhotsky <brad@divisionbyzero.net>
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com