Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

exported routine won't work without package namspace when used as a sort subroutine?

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
0
0
GB
Hi,

I have a Utils module, it exports two hash sort routines
Code:
sub hashValueAsc {
   $sortHash{$a} <=> $sortHash{$b};
}

sub hashValueDesc {
   $sortHash{$b} <=> $sortHash{$a};
}

When I use it in my script as follows....
Code:
foreach my $key (sort hashValueDesc (keys(%sortHash)))

I get an undefined sort routine error

If I use it like this...
Code:
foreach my $key (sort [b]Utils::[/b]hashValueDesc (keys(%sortHash)))

It works fine?

All other routines I export don't require the package namespace prefix to work, so why does the bespoke sort routine?


"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
Code:
package Utils;

# Invoke Exporter
use Exporter;

# Set Variables
our @ISA = qw(Exporter);

our %EXPORT_TAGS = ( 
    
    'SMTP' => [ qw(SMTP_DOMAIN) ],
    
    'RAG' => [ qw(RAG_RATING RAG_COLORS) ], 
    
    'INPUT' => [ qw(cleanInput buildUPD buildADD searchRecs) ],   
    
);

our @EXPORT = qw( ISDate UKDate setSPArgs getDate seconds2Time convertDateTime isLeap commas dateDiff hashValueAsc hashValueDesc );

our @EXPORT_OK = ( @{ $EXPORT_TAGS{'SMTP'} }, @{ $EXPORT_TAGS{'RAG'} }, @{ $EXPORT_TAGS{'INPUT'} } );

sub import {
    push(@_, qw/ ISDate UKDate setSPArgs getDate seconds2Time convertDateTime isLeap commas dateDiff hashValueAsc hashValueDesc /); 
    goto &Exporter::import;
}
All the other functions work and as you can see I also have grouping for certain qw// exports so I get all the default functions I want and can also select specific ones.

But it doesn't seem to work when using it with sort.

However, even when I had a global hash that I also exported (%sortHash) , and assigned that in my code to the hash I wanted sorting, I found the sort still didn't work. yes the UDF ran when using the package namespace 'Utils::hashValueDesc' however, the result was not sorted by the hash keys.

I believe it is to do with the scope and that fact the sort takes a codeblock, I found some ways of prototyping or passing in the hash to be sorted on stack overflow, which returns a sorted array of keys, but it all seemed a bit overkill for something simple, and many argue using a UDF for the sort command kills the performance of the sort and is not that helpful.

In the end I simply used..
Code:
    foreach my $key (sort [b]{$risks{$b} <=> $risks{$a}}[/b] (keys(%risks)))

But still curious as to why the exported sort UDF's were considered undefined in the package I tried to use them?

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
Hi

1DMF said:
But still curious as to why the exported sort UDF's were considered undefined in the package I tried to use them?
Well, that is the part which puzzles me too. I put the code you posted earlier ( the exporting followed by the [tt]sub[/tt] declarations ) in a file Utils.pm, then a simple [tt]use Utils;[/tt] is another file did it and the [tt]sort[/tt] worked ( syntactically, of course ).
[kbd][/kbd]

Feherke.
feherke.github.io
 
Well the subclass I was using it in has the following in the header..
Code:
package NBCSReporting::CaseChecker_Risk_Breakdown;
use Moose;
use LookUp;
use Utils;
use Data::Dumper;
use namespace::autoclean;

BEGIN { extends 'NBCSReporting'; }

Not sure if Moose could be the issue, or if namespace::autoclean is.

Moose is the OO framework module which the Catalyst (MVC Framework) is built on, but I'm not sure what namespace::autoclean does, it seems to be in all the Catalyst modules, so I include it in my own modules, I guess I should take a closer look as to what it does, as I'm currently using it in a 'Cargo-Cult' manner!

Can you enlighten me?

Cheers,
1DMF.

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top