Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^4: use, require, do or what?

by AKSHUN (Novice)
on Jul 31, 2008 at 14:13 UTC ( [id://701426]=note: print w/replies, xml ) Need Help??


in reply to Re^3: use, require, do or what?
in thread use, require, do or what?

We only use modules that are a part of the core distribution of Perl.

Replies are listed 'Best First'.
Re^5: use, require, do or what?
by massa (Hermit) on Jul 31, 2008 at 15:37 UTC
    I think we are in different pages here.
    I understand (at least sometimes) the need not to use anything outside perl core modules.
    But my suggestion was for you to make (not install! -- no root access needed) your own modules. It's basically the same thing as making another ".pl" file, but with many, many advantages (no namespace pollution, etc).
    So, I'll repeat what I think you should do: instead of making a file called complex_calculation.pl, make a file called ComplexCalculation.pm, with the following contents:
    use strict; package ComplexCalculation; sub do_calc { # put your calculation here. return 4 + 8 * 15 / 16 - 23 * 42; } 1;
    Now, in your scripts, everywhere where you had require 'complex_calculation.pl' (*), you will put:
    $variable1 = ComplexCalculation::do_calc();
    And, at the top of those scripts, you will put:
    use ComplexCalculation;
    I am assuming that ComplexCalculation.pm and your scripts are in the same directory. Otherwise, before the use line above, you have to put:
    use lib '/some/path/to/the/calc/module';
    (just the dir).
    (*) Anyway, this would NOT work unless the require line was used only once each script, because require does not read the same file again, when it alredy did.
    In another note, always use strict; use warnings; ...


    Update:
    You can still assign to multiple variables (even if you use only one calculation to figure them all) -- in ComplexCalculation.pm:
    sub do_calc { my $v1 = ##... something my $v2 = ##... other thing my $v3 = ##... a third thing return $v1, $v2, $v3 }
    and in your scripts:
    my ($r1, $r2, $r3) = ComplexCalculation::do_calc()
    Notice that each of your scripts may use its own relevant name for each variable, and they can even ignore one or more results:
    my ($weight, undef, $depth) = ComplexCalculation::do_calc()
    []s, HTH, Massa (κς,πμ,πλ)

      Given that OP & company use only the Perl core modules, them making their own module would disqualify it from usage causing unnecessary waste of resources. Now, pulling in another Perl program (aka "*.pl" ones) is all together different matter.

      ;-[

        Are you serious or tongue-in-cheek??
        Update:
        Never mind, I hadn't noticed the smiley the first time...
        []s, HTH, Massa (κς,πμ,πλ)
        I don't get to make the rules. If it were my call, it would have been a .pm. In the end I had to do it as a .pl and require it.

Log In?
Username:
Password:

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

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

    No recent polls found