in reply to Help: Constructors and all things OO.

This works OK. I've dropped in the sort for you to show you how you do it. $a and $b are the elements from the @array that we are sorting. In this case they are hash refs so we deref them to get the values we want to do our (numeric) sort on. You use 'cmp' rather than the spaceship '<=>' if you want a alphabetic sort. Note that reversing $a and $b reverses the sort order.

#!/usr/bin/perl -w my $obj = iXML::TheConstruct->new(); # add some stuff for testing my $html = "A"; for $index( reverse 0..10) { $obj->add($html,$index); $html++; } # print it print "Here is our object\n", $obj->output; # do some sorts $obj->sort_asc; print "\n\nSort ascending\n", $obj->output; $obj->sort_dsc; print "\n\nSort descending\n", $obj->output; exit; ########### here is the module so we need no use ########### ############################################################ package iXML::TheConstruct; $VERSION = 0.01; use strict; sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = []; bless($self, $class); return $self; } sub add { my ($_self, $_obj_html, $_ind) = @_; my $_obj_hash = { OBJ_HTML => $_obj_html, OBJ_IND => $_ind}; push @{$_self}, $_obj_hash; } sub sort_asc { my $_self = shift || die "No object to sort!"; @$_self = sort { $a->{'OBJ_IND'} <=> $b->{'OBJ_IND'} } @$_self; } sub sort_dsc { my $_self = shift || die "No object to sort!"; @$_self = sort { $b->{'OBJ_IND'} <=> $a->{'OBJ_IND'} } @$_self; } sub output { my ($_self) = @_; my $_html_data; foreach my $_obj ( @{$_self} ) { my $index = $_obj->{'OBJ_IND'}; my $html = $_obj->{'OBJ_HTML'}; $_html_data .= "$index => $html\n"; } return $_html_data; } 1;

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: Help: Constructors and all things OO.
by IOrdy (Friar) on Aug 28, 2001 at 17:49 UTC
    Thats ++great tachyon... thanks heaps, I had read about the sort in another node on perlmonks but all the examples where using spaceships :) but hadn't tried to use anything I read yet (though I had it all bookmarked) but thanks heaps.

    As stated in one of my re:'s above, after more testing the problem seems to come when I try and load the module at the same time as Apache::Request; ( not the use itself but when I do "my $r = Apache->request" or "my $r = Apache::Request->new()" )

    I was thinking it might have something to do with the "use lib '';" statement. (I will add the site address to @INC another way and see if that makes any difference)

      See my Simple Module Tutorial for they whyfors and whatfors of @INC. In a nutshell your module must be in a directory specified in @INC. @INC usually contains these three directories:

      C:/Perl/lib 
      C:/Perl/site/lib 
      .

      This is a Windows example, just drop the C: for *nix. Now when you use "MyModule" Perl searches the @INC directories in order for "MyModule.pm". Note that the . dir is the current working directory of the script. There is a difference between module names and package names. If you have the line:

      use iXML::TheConstruct

      Then Perl will search for a module called "TheConstruct.pm" in a subdir called "iXML" in the @INC dirs. This is your problem. Using the module allows you to use the subs in the module by using the PACKAGE::SUB notation. Your package name could be package WhatsThatGotToDoWithThePriceOfFish;. You would then use iXML::TheConstruct; and instantiate your object via a call $obj = Whatsthatgottodowiththepriceoffish->new

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        Nice try but I read about @INC and have the module in iXML/TheConstruct.pm ("/home/iordy/perl.iordy.net/iXML/TheConstruct.pm")

        I even went to the trouble of using something like:

        use lib "/home/iordy/perl.iordy.net"; in my script that calls iXML/TheConstruct.pm

        interesting: I just found that if I put Apache->request; in a sepereate sub it works. wtf?? It could be painfull but it works.