Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

How do I use a sort routine from a different package/module?

by bobf (Monsignor)
on Jun 09, 2005 at 07:33 UTC ( #464989=categorized question: print w/replies, xml ) Need Help??
Contributed by bobf on Jun 09, 2005 at 07:33 UTC
Q&A  > sorting

Answer: How do I use a sort routine from a different package/module?
contributed by bobf

There are (at least) two ways to access sort routines in a different package:

  • Prototype the sort subfunction and pass the values as arguments, which can then be accessed by directly indexing @_
  • Fully qualifying $a and $b in the sort routine with the package name of the caller (this works because prior to Perl 6 the elements to be compared are passed to the sort routine in the package global variables $a and $b)

use strict; use warnings; use my_sort_routines; package myprogram; my @data = ( 4, 2, 1, 3 ); my @sorted_proto = sort my_sort_routines::prototyped @data; my @sorted_qual = sort my_sort_routines::qualify_pkg @data; print @sorted_proto, "\n"; #1234 print @sorted_qual, "\n"; #1234 package my_sort_routines; sub prototyped ($$) { # Access the arguments directly by indexing @_ $_[0] <=> $_[1]; } sub qualify_pkg { # Qualify $a and $b with the name of the calling package # Symbolic refs require "no strict refs" here # Symbol::qualify could simplify this, but this illustrates # how to do it without an additional module my $pkg = caller; no strict 'refs'; ${"${pkg}::a"} <=> ${"${pkg}::b"}; }


Please (register and) log in if you wish to add an answer

  • 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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (4)
    As of 2018-09-24 01:26 GMT
    Find Nodes?
      Voting Booth?
      Eventually, "covfefe" will come to mean:

      Results (191 votes). Check out past polls.

      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!