[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details

Programming practices when passing back references

Asked by mock5c in Perl Programming Language

Tags: Perl

This may be more of a programming concept question.  I have been wondering about calling functions and returning values.

In my example, I have a package called Conn.pm that contains the code for connecting to a database.  I can call this with:

# Get handle to the database.
my $dbh = getConn(); # Conn.pm is use'd


So here's the thing.  In the package, $conn is declared and returned.  This works.  But I am puzzled how I can return a reference to $conn when $conn is declared inside the package function and should be destroyed after leaving the getConn() function?

In C++ programming, if I were to do something like that, then I would be returning a reference to a destroyed object unless I had created the object on the heap (using new).  In this getConn() function, I don't see the "new" operator anywhere but yet I've seen the use of "new" in other perl packages.


Similarily, let's say I want to call a function called mkHeadersHash() (I just stuck this in the same Conn.pm package).  I pass in an array and I get back out a reference to a hash with indexing.

my @ary = qw(red green blue);
my $hash = mkHeadersHash(@ary);
print "$_ : $hash->{$_}\n" for keys %$hash;


Again, the mkHeadersHash() function creates the %HeadersHash inside the function and returns a reference to it.  But the %HeadersHash hash was not destroyed after leaving that function (because my return value pointed to valid data).  I would have expected my hash reference to be pointing to nothing.

So in terms of this specific question, how is this $conn and %HeadersHash able to hang around?  In terms of programming concepts and practices, what is the appropriate way to deal with calling functions and returning values using a standard practice similar to many other languages?  Is it better to declare $conn or $hash in the main program and pass these as references to the functions in the package?  I'm sure there are many ways to do this so I'm interested in best practices.

This is a 300 point question because I am hoping to receive multiple, valuable answers.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
package Conn; 
use strict;
use warnings;
use Exporter; 
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);
$VERSION = 1.00;
@ISA = qw(Exporter);
@EXPORT = qw(&getConn &mkHeadersHash);  # Symbols to autoexport
@EXPORT_OK = qw();  # Symbols to export or request
%EXPORT_TAGS = ();  # Define names for sets of symbols 

sub getConn {
   # Get a connection to the database.  This subroutine returns a working
   # connection or causes the app to exit, there is no return value. 
   my $sHost = "myHost";
   my $sPort = "myPort";
   my $sDBName = "myDB"; 
   my ($sUser,$sPasswd) = getUsernamePassword(); 
   my $conn;
   my $error_found = 0 ; 

   $conn = DBI->connect("dbi:Pg:dbname=$sDBName;host=$sHost;port=$sPort;" .  "", "$sUser", "$sPasswd", {
		RaiseError => 1,
		PrintError => 0,
		AutoCommit => 0,
           }); 

   return $conn;
} 
sub getUsernamePassword {
   # Some code in here for handling username/password
} 
sub mkHeadersHash {
   my @headers = @_; 
   my %HeadersHash;
   $HeadersHash{$headers[$_]}=$_ for (0 .. $#headers); 
   return {%HeadersHash};
} 

1;
      
[+][-]11/07/09 09:56 AM, ID: 25767264Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/07/09 09:59 AM, ID: 25767276Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/09/09 08:18 AM, ID: 25777266Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/10/09 01:20 PM, ID: 25789987Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81 - Hierarchy / EE_QW_3_20080625