Welcome to WebmasterWorld Guest from 54.160.177.33

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Simple Syntax Question

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

Junior Member

5+ Year Member

joined:Feb 27, 2008
posts:144
votes: 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()

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

Junior Member

5+ Year Member

joined:Feb 27, 2008
posts:144
votes: 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()

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

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Mar 29, 2007
posts:1147
votes: 0


I'd never thought about it before.

I always use :: for my own packages.

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

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:May 31, 2008
posts: 661
votes: 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";
}

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

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Mar 29, 2007
posts:1147
votes: 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?

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

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:May 31, 2008
posts:661
votes: 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.

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

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10542
votes: 8


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.

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

Junior Member

5+ Year Member

joined:Feb 27, 2008
posts:144
votes: 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]

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

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:May 31, 2008
posts:661
votes: 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 ;)
6:40 pm on Dec 9, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 27, 2008
posts:144
votes: 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.
12:21 am on Dec 10, 2008 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10542
votes: 8


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.