For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2005 > Passing an object to a subroutine via hash?









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 Passing an object to a subroutine via hash?
Chris

2005-02-20, 8:55 am

Greetings,

I don't know what the proper terminology is for what I want to do, so if
somebody can tell me what it is, that would be great.

Basically I have my core code, then lots of subroutines. Right now I pass data
to subroutines via a "parameter" hash. How can I pass an object that was
created in the core code?

To put what Im thinking into code somewhat:

use CGI;

my $q = new CGI;

my %param_hash = (
cgi => $q,
);

some_sub(\%param_hash);

sub some_sub {
my $params = shift;

my $q = $params->{cgi};
}


The above is just a basic example of what im looking to do. Im only looking
for the terminology for what this is called, but any code pointers is great
too!

Thanks
Alctraz

2005-02-20, 8:55 am

If you are only looking for the terminology, this is called 'Pass by Reference':

http://www.perlpod.com/5.8.4/pod/pe...0by%20Reference


On Sun, 20 Feb 2005 02:30:34 -0500, Chris <chrisp3623@comcast.net> wrote:
> Greetings,
>
> I don't know what the proper terminology is for what I want to do, so if
> somebody can tell me what it is, that would be great.
>
> Basically I have my core code, then lots of subroutines. Right now I pass data
> to subroutines via a "parameter" hash. How can I pass an object that was
> created in the core code?
>
> To put what Im thinking into code somewhat:
>
> use CGI;
>
> my $q = new CGI;
>
> my %param_hash = (
> cgi => $q,
> );
>
> some_sub(\%param_hash);
>
> sub some_sub {
> my $params = shift;
>
> my $q = $params->{cgi};
> }
>
> The above is just a basic example of what im looking to do. Im only looking
> for the terminology for what this is called, but any code pointers is great
> too!
>
> Thanks
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>

Wiggins d'Anconia

2005-02-21, 3:57 pm

Chris wrote:
> Greetings,
>
> I don't know what the proper terminology is for what I want to do, so if
> somebody can tell me what it is, that would be great.
>
> Basically I have my core code, then lots of subroutines. Right now I pass data
> to subroutines via a "parameter" hash. How can I pass an object that was
> created in the core code?
>
> To put what Im thinking into code somewhat:
>
> use CGI;
>
> my $q = new CGI;
>
> my %param_hash = (
> cgi => $q,
> );
>
> some_sub(\%param_hash);
>
> sub some_sub {
> my $params = shift;
>
> my $q = $params->{cgi};
> }
>
>
> The above is just a basic example of what im looking to do. Im only looking
> for the terminology for what this is called, but any code pointers is great
> too!
>
> Thanks
>


The terminology as the other poster said is "Pass by Reference"
specifically, in general you want more info on references themselves as
they have much larger uses besides just "Pass by reference", see:

perldoc perlreftut
perldoc perlref
perldoc perllol
perldoc perldsc

http://danconia.org
Sponsored Links







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

Copyright 2008 codecomments.com