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:
Unused assignment

 



amsmota
New User

Feb 12, 2008, 6:33 AM

Post #1 of 4 (829 views)
Unused assignment Can't Post

Hello:

I'm completelly new to Perl (almost 24 hours by now Angelic) and it seems very nice...

Basically, I have to get up to speed with a app someone wrote sometime in the past because it's probable that need some changes. I come across with some code like this:


Code
sub NewItem{ 
my $self=shift;
return(STF::Entity->new);
}


and I don't have a clue why the my $self=shift; is doing there if there are no references after that to $self. I could think that's just something the author forgot, but I saw the exact same thing in a Perl related book. So my question is, what's the meaning of that?

Thanks.


KevinR
Veteran


Feb 12, 2008, 1:04 PM

Post #2 of 4 (824 views)
Re: [amsmota] Unused assignment [In reply to] Can't Post

read up on OO perl programming and you will/should understand what the code is doing. There are several here:

http://perldoc.perl.org/index-tutorials.html
-------------------------------------------------


amsmota
New User

Feb 12, 2008, 1:17 PM

Post #3 of 4 (820 views)
Re: [KevinR] Unused assignment [In reply to] Can't Post

Thanks for your answer. Actually I do saw that page, it was one of the firsts that I saw after making a Google search by "perl tutorials".

I do understand what the line
my $self=shift;

does, and I also understand what
return(STF::Entity->new);

does, what I don't understand is the necessity of the first of these lines considering $self is never used. I mean, doesn't this be the same?


Code
sub NewItem{  
return(STF::Entity->new);
}


Or is there some obscure meaning on the first line that I fail to see while browsing the docs you just pointed me to?

Cheers.


KevinR
Veteran


Feb 12, 2008, 1:30 PM

Post #4 of 4 (819 views)
Re: [amsmota] Unused assignment [In reply to] Can't Post

print the scalar to the screen:


Code
sub NewItem{  
my $self=shift;
print $self;
return(STF::Entity->new);
}


The first argument (shift in this case) is the name of the class the object belongs too. I am far from an expert OO programmer, but it looks like the code is ignoring the object class and creating a new STF::Entity. So in essence it seems like it is destroying an old object to make a new one, which probably has all the default values of the object class redefined instead of any old values the previous object may have picked up along the way. But I could be wrong. A second opinion would be a good idea. Try www.tek-tips.com perl forum or devshed.com perl forum.
-------------------------------------------------

 
 


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

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