homepage Welcome to WebmasterWorld Guest from 23.22.194.120
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Simple Syntax Question
vol7ron




msg:3801458
 6:03 am on Dec 6, 2008 (gmt 0)

Let's say your CGI file is something like:
---
#!/usr/bin/perl
use strict;
use pkg;

print pkg::connect();
---

This is a question someone asked me, that I didn't really have the answer, other than, "this is the way I've always done it." The question is: What is the difference between using pkg::connect() and pkg->connect()

 

vol7ron




msg:3801465
 6:43 am on Dec 6, 2008 (gmt 0)

As I've come to understand it, the -> operator is used to dereference the reference and is vital for object orientation.

So in the above example the pkg::connect() would just call connect() in pkg scope, but pkg->connect() would call connect() of the pkg object? I'm not totally clear on this and I guess after all these years of not questioning DBI->connect(), I'm also curious as to why we don't just do DBI::connect()

Dabrowski




msg:3801533
 12:43 pm on Dec 6, 2008 (gmt 0)

I'd never thought about it before.

I always use :: for my own packages.

janharders




msg:3802477
 12:05 pm on Dec 8, 2008 (gmt 0)

you're right, vol7ron.
pkg::connect() will just call the sub connect from the pkg namespace, doesn't require an object and is actually not OO at all.

pkg->connect() on the other hand calls the connect-method as a pseudo-object (at least I believe...) of pkg-class, working just as $pkg->connect() would, possibly returning the object (like DBI does for initialization).

In perls OO-style, the methods are actually just functions which expect the object they belong to as the first argument. if you call a subroutine in a different namespace using pkg->subr(), the class will be passed as the first argument (for object initialization), if you call it with pkg::subr(), it won't, which is why DBI::connect() does not work with the same arguments as DBI->connect(), while DBI::connect('DBI', $dsn...) should.

here's a test to show the difference:

#!/usr/bin/perl
package main;
test1::test(); #prints "no first arg"
test1->test(); #prints "first arg: test1"

package test1;

sub test {
print 'first arg: ' . $_[0] . "\n";
}

Dabrowski




msg:3802513
 12:59 pm on Dec 8, 2008 (gmt 0)

Thanks for the explanation Jan, are there any pros/cons for each method?

How would you go about returning an object like DBI does?

janharders




msg:3802527
 1:52 pm on Dec 8, 2008 (gmt 0)

Well, they're two different things, so I don't think you can really find pros and cons. the ::-call would, in my mind, be for helper-routines you don't need objects for, while the other is strictly for OO.

as for the way DBI does it: I think the DBI-way is fine, allthough it might be more obvious to create the object with something like
my $obj = pkg->new();
before telling it to connect with
$obj->connect();
That'd keep the whole confusion out of the way. DBI takes ->connect calls, builds its object within that sub routine and returns it, that's why you use DBI->connect and work with $dbh->prepare after the initial connect. DBI doesn't have a new-routine so ->connect is the only way available.

phranque




msg:3803009
 12:35 am on Dec 9, 2008 (gmt 0)

the standard practice for oo perl is to name the constructor method "new", but there is no requirement or limitation for that.
think of DBI's connect method as a constructor that does a little bit more than simply create the object and return a handle.
practically speaking, having a handle to a database prior to making the connection is worthless.

v7r: good question!
i never thought about it much before you asked.

jh: i like your answers.
i think the simplistic answer to the original question is that :: indicates a class method and -> indicates an object method but that doesn't really answer the when or why question.

D: the returned object is usually a handle or reference to the object.
in most cases the handle is a reference to a hash.

vol7ron




msg:3803389
 2:54 pm on Dec 9, 2008 (gmt 0)

Jan did a good job explaining this. I tried to make my own module for a blog-like system and it worked with the (::) but not with (->). This is because the subroutine did need some parameters.

Granted, I'm not a Perl expert, but I hope to be one day. With all of your help, I might actually get there :) I'll try to see how it works if I evaluate the first parameter.

Thank you for all your comments.

[edited by: phranque at 12:12 am (utc) on Dec. 10, 2008]
[edit reason] fixed smileys [/edit]

janharders




msg:3803448
 3:44 pm on Dec 9, 2008 (gmt 0)

I think I can speak for everyone by saying: glad I could help.
It's always a great thing to know that someone is still learning perl, much more so because I know how rewarding it is if you get through the confusing parts.
If you have any questions or problems that would bring some action into the perl-section, I'm sure I'm not the only one who'd be happy about it ;)

vol7ron




msg:3803610
 6:40 pm on Dec 9, 2008 (gmt 0)

In case the smilies confused anyone, the first and second two were put in against my will. They should be replaced with "::" and "->" respectively.

phranque




msg:3803855
 12:21 am on Dec 10, 2008 (gmt 0)

jh: what he said!

v7r: accidental smileys fixed!

make sure you have read the perltoot and perlobj man pages.
also i hope i'm not the first person to direct you to the cpan site for useful resources.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved