×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

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

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

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

(OP)
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 Utils::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

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

Hi

Quote (1DMF)

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

Feherke.
feherke.github.io

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

(OP)

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 {$risks{$b} <=> $risks{$a}} (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

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

Hi

Quote (1DMF)

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 sub declarations ) in a file Utils.pm, then a simple use Utils; is another file did it and the sort worked ( syntactically, of course ).

Feherke.
feherke.github.io

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

(OP)
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

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

Hi

Quote (1DMF)

Can you enlighten me?
Sorry, I can not. Never touched either Moose or namespace::autoclean.

Feherke.
feherke.github.io

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close