"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.

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

In reply to Re: Basic Class question by tobyink
in thread Basic Class question by packetstormer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.