Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Sort::Maker not making sort

by macrobat (Beadle)
on Apr 18, 2007 at 20:23 UTC ( [id://610837]=perlquestion: print w/replies, xml ) Need Help??

macrobat has asked for the wisdom of the Perl Monks concerning the following question:

Folks,

Are there any particular invocations or environment variables that I need to get Sort::Maker to work?

I have this (mostly just copied from Perl Best Practices):

#!/usr/bin/perl # foo.pl - This is just a test use strict; use warnings; use Sort::Maker; make_sorter( name => 'sort_len', code => sub { length }, ST => 1 ); my ( $foo, $bar ) = (qw/very_long short/); my @order = sort_len( $foo, $bar ); print @order;

But when I run it, I get this error:

Undefined subroutine &main::sort_len called at foo.pl line 10.

I've also changed the make_sort() call to this:

my $sf = make_sorter( name => 'sort_len', code => sub { length }, ST = +> 1 ); die "No sort made\n" if (! $sf);

--but then I get the message "No sort made".

Anyone have any ideas why that should be? The code passes syntax checking, and the module installed without errors.

Oddness.

Replies are listed 'Best First'.
Re: Sort::Maker not making sort
by Corion (Patriarch) on Apr 18, 2007 at 20:44 UTC

    Looking at the source code of Sort::Maker makes my eyes bleed, but it also seems that Sort::Maker uses a weird way of communicating errors - it sets $@ but doesn't die. So you could maybe change your error checking as follows:

    die "No sort made: $@" if ! $sf;

    When I do that with your code, I get the following "error":

    make_sorter: Unknown option or key 'code'

    which I consider to be fatal instead of to be the kind of warning people would prefer to hide. Also code is documented to be valid - maybe it depends on the order of arguments, as Sort::Maker really employs some nonstandard argument parsing. Maybe this also is another Perl "Best Practice" that is more a TheDamian Best Practice and should be avoided in general use.

    Reading the documentation some more, it seems you have to give it some more arguments for sorting strings and/or numbers. The following code works for me:

    #!/usr/bin/perl # foo.pl - This is just a test use strict; use warnings; use Sort::Maker; make_sorter( string => { code => sub { length } }, ST => 1, name => 's +ort_len', ) or die "$@"; my ( $foo, $bar ) = (qw/very_long short/); my @order = sort_len( $foo, $bar ); print @order;

    Here I've wrapped the code => sub { length } in string => {...} - you should read the documentation if that is what you need or not, while I wash my eyes from looking at the code, the interface and the documentation.

      Ah, and I thought the obfuscation of the doc was only in my head :)
Re: Sort::Maker not making sort
by polettix (Vicar) on Apr 18, 2007 at 22:16 UTC
    Probably a little "side-topic", but I've seen an enthusiastic comment to Sort::Key, so you could give it a try even tough the interface is pretty different, and could not adapt to what you really aim to.

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
      With Sort::Key it can be done as follows:
      use Sort::Key qw(ikeysort); my @unordered = (qw/very_long short/); my @ordered = ikeysort { length } @unordered;
      or also...
      use Sort::Key::Maker sort_len => sub { length } , qw(integer); my @unordered = (qw/very_long short/); my @ordered = sort_len @unordered;
Re: Sort::Maker not making sort
by thezip (Vicar) on Apr 18, 2007 at 20:56 UTC
    Regarding your first point, the subroutine sort_len() is not exported by the Sort::Maker module. When I looked at the module's source code, I also failed to find an occurrence of the string "sort_len". Perhaps you are using sort_len in the wrong context?

    Update: Please ignore my uninformed statement... Just trying to be helpful with a module I haven't used as of yet...

    Where do you want *them* to go today?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://610837]
Approved by friedo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-04-19 10:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found