Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write perl function 1

Status
Not open for further replies.

JackTheRussel

Programmer
Joined
Aug 22, 2006
Messages
110
Location
FI
Hi.

I have program where I have lots of SQL-commands.
Now I would like to write some functions because then I should't write so much code, but I have few problems.

Example. I have function like this.
Code:
sub delete_id {
 $sth = $db_conn->prepare($sql_string);
 $sth->execute($id); 
 $sth->finish();
}

And I call it here
Code:
    $sql_string="DELETE from table where id=?";
    &delete_id($id);

And It works perfectly.

But how I can use this function again when I have two values like here:

Code:
 $sql_string="DELETE from table where id=? AND name=?";
 &delete_id($id, $name);

THIS FUNCTION IS NOT OK ANYMORE
Code:
sub delete_id {
  $sth = $db_conn->prepare($sql_string);
  $sth->execute($id); #Missing $name
  $sth->finish();
}

So what can I do ?
Can I use this function again or do I have to
give up for using functions in this case?
 
Even from the first example you posted, the $id variable being used in your subroutine isn't because it's passed into the subroutine call. In other words, if you just called "delete_id()" (or of course "&delete_id"), you'd get the same effect.

When you pass variables into a subroutine, they're contained in the special variable @_. For this kind of sub, I would suggest passing in your SQL string as your first argument and any other parameters after that. Something like this:
Code:
delete_id( $first_sql_string, $first_id );
delete_id( $second_sql_string, $second_id, $name );

sub delete_id {
   my ( $sql_string, @parameters ) = @_;
   $sth = $db_conn->prepare($sql_string);
   $sth->execute( @parameters );
   $sth->finish();
}
 
Thank you very much ishnid!

This was very useful information for me.
 
ishnid,
Excellent information about how to dynamically execute queries. Have a star.


--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Good info ishnid. There are two enchancements that I would strongly suggest that anyone using this type of utility function employ: error checking and passing return values.

Code:
[COLOR=blue]use Carp ();[/color]

delete_id( $first_sql_string, $first_id );
delete_id( $second_sql_string, $second_id, $name );

sub delete_id {
   my ( $sql_string, @parameters ) = @_;
   $sth = $db_conn->prepare($sql_string);
   [COLOR=blue]my $count = [/color]$sth->execute( @parameters )[COLOR=blue] or Carp::croak $db_conn->errstr[/color];
   $sth->finish(); [COLOR=blue]undef $sth;[/color]
   [COLOR=blue]return $count;[/color]
}

This will die on any errors from the caller's perspective, and this will also return the number of rows deleted, updated, or inserted depending on the type of query that you perform.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top