Monks,

Updated: Several typos and omissions fixed.

Here's a weird thing I don't understand. Can another more enlightened share his knowledge? I am working on a large set of scripts and have a 'Utilities' module for simple stuff that's used repeatedly across multiple packages. I wanted to put some sort routines in there, but got tripped up by the fact that the $a and $b used in sort routines are package globals, and these sort routines are used in packages different from the one they are defined in. So I thought, hey, simple answer: export $a and $b from 'Utilities' so the package globals are aliased. That didn't work. I checked and made sure that $a and $b in the Utilities package and the use-ing package were really aliases of e/o, and they were; I even printed out $a inside the sorting sub and the correct value was being printed.

What did turn out to work was exporting *a and *b. I don't understand why that worked and exporting $a/b didn't. Anybody got a clue as to why exporting $a/b doesn't work, but exporting the corresponding type globs does?

Here's some example code:

file1: main.pl

#!/usr/bin/perl use lib '.'; use Sorts; $, = "\n"; print sort caseless ('a','s','d','f');
file 2: Sorts.pm

version 1 -- b0rken, a/b not exported:

use Exporter; package Sorts; use base 'Exporter'; @Sorts::EXPORT = qw(caseless); sub caseless { lc $a cmp lc $b }
version 2 -- still b0rken, $a/b exported:
use Exporter; package Sorts; use base 'Exporter'; @Sorts::EXPORT = qw(caseless $a $b); sub caseless { lc $a cmp lc $b }
version 3 -- works!, *a/b exported:
use Exporter; package Sorts; use base 'Exporter'; @Sorts::EXPORT = qw(caseless *a *b); sub caseless { lc $a cmp lc $b }
running main.pl with the first two versions of Sorts.pm produces:
a s d f
but with the third version outputs:
a d f s
--DrWhy

"If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."


In reply to sort routines crossing package lines by DrWhy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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, details, 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, summary, 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.