Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Imagination greater than reality?

by kcott (Archbishop)
on Jul 11, 2017 at 06:42 UTC ( [id://1194779]=note: print w/replies, xml ) Need Help??


in reply to Imagination greater than reality?

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-25 23:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found