Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by AKSHUN (Novice)
on Jul 30, 2008 at 19:04 UTC ( [id://701238]=note: print w/replies, xml ) Need Help??


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

Creating a proper module isn't an option. I have to work within the constraints of my employer's dev environment.

The whole purpose of the code is to assign values to variables. I over-simplified in my examples.

Basically, the code is to take ARGV and convert it into a date, and if there is no ARGV it creates the date based upon localtime.

Replies are listed 'Best First'.
Re^3: use, require, do or what?
by massa (Hermit) on Jul 30, 2008 at 20:18 UTC
    Could you please explain to me exactly _how_ your employer's dev env precludes you from making proper modules and subroutines instead of loose .pl files? Modules can be put in the current dir, just like your .pl file in your example...
    []s, HTH, Massa (κς,πμ,πλ)
      We only use modules that are a part of the core distribution of Perl.
        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 (κς,πμ,πλ)
Re^3: use, require, do or what?
by actualize (Monk) on Jul 30, 2008 at 19:13 UTC
    Wait, so then why are you trying to put the variables in another file?
    -Actualize
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-03-28 16:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found