G'day writch,

"Is what I'm thinking of outside of the current box, ...?"

Not outside the current box, or even outside an old box: the builtin module File::Spec, for instance, has been doing this sort of thing for years. [I'm fairly sure it's more than a decade, but I don't have specific information to hand.]

"... or am I just doing the right thing the wrong way?"

If you're getting errors, or it's not working as expected, then probably "the wrong way". :-)

I would consider reordering the elements of your namespace such that the least specific element is first and the most specific last. It's your module, you can call it whatever you like, and there may be aspects of which I'm not aware; however, I probably would have chosen:

Formula::State::$state

You can keep generic code in Formula::State, perhaps something like:

sub calculate_xyz { my ($x, $y, $x, $state_variance) = @_; $state_variance //= 1; return (($x + $y) / $z) * $state_variance; }

Then in Formula::State::$state:

... calculate_xyz($x, $y, $x) ... # Use standard value ... calculate_xyz($x, $y, $x, 1.5) ... # Add 50% to standard value ... calculate_xyz($x, $y, $x, 2) ... # Double standard value

That also keeps your state-related formulae separate from Formula::Molecular, Formula::Secret, and so on.

You're showing use, which suggests that you want to load your module at compile time. You can do that with something like this:

BEGIN { my $state = ... get state from somewhere (e.g. $ARGV[0]) ... require "Formula/State/$state.pm"; "Formula::State::$state"->import(@optional_arguments); }

I ran a quick command line test (mainly to check the syntax I'd given you):

$ alias perle alias perle='perl -Mstrict -Mwarnings -Mautodie=:all -E' $ perle 'BEGIN { my $x = $ARGV[0]; require "List/$x.pm"; "List::$x"->i +mport("uniq") } my @u = uniq (1,1,2,3,3,3); say "@u"' Util 1 2 3 $ perle 'BEGIN { my $x = $ARGV[0]; require "List/$x.pm"; "List::$x"->i +mport("uniq") } my @u = uniq (1,1,2,3,3,3); say "@u"' MoreUtils 1 2 3

I initially used the first() function, which I thought was in both of those modules. It's not, so I got an error; however, that's also useful feedback.

$ perle 'BEGIN { my $x = $ARGV[0]; require "List/$x.pm"; "List::$x"->i +mport("first") } say first { not $_ % 2 } 1, 2, 3' Util 2 $ perle 'BEGIN { my $x = $ARGV[0]; require "List/$x.pm"; "List::$x"->i +mport("first") } say first { not $_ % 2 } 1, 2, 3' MoreUtils Could not find sub 'first' exported by List::MoreUtils at -e line 1. BEGIN failed--compilation aborted at -e line 1.

— Ken


In reply to Re: Imagination greater than reality? by kcott
in thread Imagination greater than reality? by writch

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.