CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
ref used in classes

 



death_metal
Novice

May 5, 2013, 1:34 PM

Post #1 of 4 (519 views)
ref used in classes Can't Post

Hi members,
I am new to Perl and this forum too. Just started my hands on Perl, objects and classes.

I couldn't understand why ref is used in object blessing.

Code
package Room; 
use strict;
sub new{
my ($caller,@arg)=@_;
my $class=ref(caller);
my $self{
#some code
};
return (#some code);
}


Kindly help me, what is the use of Ref?. Unimpressed
we could have got class name in first line of subroutine new.

Any help shall be appreciated. Smile


(This post was edited by death_metal on May 5, 2013, 1:36 PM)


hwnd
User

May 5, 2013, 1:54 PM

Post #2 of 4 (514 views)
Re: [death_metal] ref used in classes [In reply to] Can't Post

Here's an short example of what you are asking:


Code
package foo;   

sub new{
my $class = shift;
$class = ref($class) || $class;
my $self = {};
bless $self, $class;
return $self;
}



Using ref() in this is simply executing a check to whether what was passed in was a reference or not. If it is, then we can invoke as an object method and extract the package class using ref(). If not, we use the string passed in the package class.


rovf
Veteran

May 7, 2013, 8:37 AM

Post #3 of 4 (496 views)
Re: [death_metal] ref used in classes [In reply to] Can't Post

You didn't provide the context (i.e. how this "new" is supposed to be used), neither you had posted the whole body of the function. Hence, we can only guess. From the code, I assume that this is a function intended to create a new object based on a existing one. For example, if $x is a reference to some object (likely inheriting from Room), $x->new might create a new object of the same type as $x.

Of course, this is only speculation. You need to provide more information on your case.


death_metal
Novice

May 16, 2013, 9:08 AM

Post #4 of 4 (469 views)
Re: [hwnd] ref used in classes [In reply to] Can't Post

@hwnd
Thanks much for clarifying this thing. This explanation fits into my mind frame.
Once again, I would like to thank you. Smile


(This post was edited by death_metal on May 16, 2013, 9:09 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives