Hi Monks, I need some help with OO Perl.
Here by_id function in SORT is not working since object type (for $a and $b) is not defined in the first place.
Can I define them ??
In Module SORT.pm the Function by_id has no idea what's 'id' function we are talking about
My assumption is that SORT.pm need to know at compile time that what is 'id' function and where it's coming from when applied to 'sorting variables' $a and $b
package main; use X; use SORT; my $p = new X(10); my$q = new X(20); my $r = new X(15); foreach my $obj (sort by_id ($p,$q,$r)){ print $obj->id; } package X; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(id); sub new { my $class =shift; my $self = { _id => shift }; bless $self, $class; } sub id { my $self = shift; return $self->{_id}; } 1; package SORT; use X; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(by_id); sub by_id { $a->id <=> $b->id; } 1;
Above Code when run gives the error
Can't call method "id" without a package or object reference at SORT.pm

How do I make it working???
Thanks,
Artist

In reply to OO and sorting by artist

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.