in reply to Basic Class question
"What is the reason for the first argument that is passed to the routine in a class being the class name? Is this useful in certain ways?"
You need the class name so that you can call other methods from within your method. For example:
{ package Processor; sub process_one_thing { my $class = shift; my ($thing) = @_; print "Processing '$thing'...\n"; } sub process_many_things { my $class = shift; my (@things) = @_; for my $thing (@things) { # we need $class so we can call this # other method! $class->process_one_thing($thing); } } } Processor->process_many_things("Foo", "Bar", "Baz");
"I noticed that if I export my functions, from the class, and then, in my calling script call it by its name (e.g &function("data") ) then the first argument isn't the class name"
That's because you're not supposed to do that. Don't export methods *.
Basically methods in Perl are just syntactic sugar. Foo->bar(...) is a shorthand for Foo::bar("Foo", ...), but with inheritance thrown in, so that if there is no such function Foo::bar(), the parent classes of Foo will be consulted.
* it is sometimes possible to use exported functions and OO programming in the same package to good effect, but until you're comfortable using OO, don't try it.
|
|---|