Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Sharing the scalar love?

by jcpunk (Friar)
on Dec 22, 2003 at 20:35 UTC ( [id://316473]=perlquestion: print w/replies, xml ) Need Help??

jcpunk has asked for the wisdom of the Perl Monks concerning the following question:

Forgive the title, but it does actually apply. I am cleaning up some code for a friend who is deathly afraid of global scalars (even though he does really need one of his several dozen scalars been passed to each function to be global)But I haven't ever worked with this type of thing in perl
So I was looking for some advice on sharing a scalar by the name of $love across 3 different files (require './file1'; require './file2';)
Idealy I would like to put $love in file1 and share it from there, but given my complete lack of expirence in this area I will defer to you guys.
I have really no idea how even to address the scalar once it is placed inside of the file (I guessed a bit and put our $love; inside of file1, but program.pl cannot access it via the name $love)

I would also be interested in a stern talking to if the idea of global scalars for all 12 of the scalars used frequently in file1 would be considered an bad idea.


jcpunk
all code is tested, and doesn't work so there :p (varient on common PM sig for my own ammusment)

Replies are listed 'Best First'.
Re: Sharing the scalar love?
by jeffa (Bishop) on Dec 22, 2003 at 22:27 UTC

    "I would also be interested in a stern talking to if the idea of global scalars for all 12 of the scalars used frequently in file1 would be considered an bad idea."

    Global variables are indeed something to be avoided, but i think it is okay to break this rule as long as you understand why first. When i was an undergraduate, my classmates and myself were forced to avoid them. Relying on global variables is a bad habit theorectically, but realistically, they have been proven to be prone to producing pasta code.

    Now, in your case i would turn those 12 scalars into one global array:
    # file1.pl our @angry_men = qw( Balsam Fiedler Cobb Marshall Klugman Binns Warden Fonda Sweeney Begley Voskovec Webber );
    or one global hash:
    our %diamond = ( 'Koh-i-Noor' => 186, Cullinan => 3106, Excelsior => 995.2, 'Star of Africa' => 520.20, 'Great Mogul' => 793, Orloff => 300, "Idol's Eye" => 70.2, Regent => 410, 'Blue Hope' => 45.52, Sancy => 55, 'Taylor-Burton' => 69.42, Hortensia => 20, );
    And then you need only declare that variable once in each script that needs it:
    require '/full/path/to/file1.pl'; our @angry_men; our %diamond; # then later ... print $diamond{Cullinan}; $some_var = $angry_men[7];
    More than likely you will want a hash (a good name is %conf), but sometimes an array makes more sense. Good luck. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Sharing the scalar love?
by TVSET (Chaplain) on Dec 22, 2003 at 21:05 UTC
    Here is how I usually do it:

    1. Create a separate module with all the shared staff:

    package Common; use constant GLOB_DEBUG_MODE = 1; use constant GLOB_SITE_NAME = 'My site'; 1;
    2. use Common; from anywhere that I need. For example:
    package Main; use Common; sub print_message { my $msg = shift; # Use default site name if no message specified. unless ($msg) { $msg = Common::GLOB_SITE_NAME; } # Prepend with "DEBUG: " if in debug mode. $msg = "DEBUG: $msg" if (Common::GLOB_DEBUG_MODE); print $msg; } 1;

    Update: I've answered a bit a wrong question. :) You were asking about global variables, while I described constants. :)

    Update: Fixed two misplaced 'Common's. :)

      I'm assuming that the file in "1. Create a separate module with all the shared staff:" is called Config in part 2 which strikes me as odd.... perhaps a typo in "(Config::GLOB_DEBUG_MODE)"? I'm still sorta trying to figure this out. But my confusion says that perhaps "(Common::GLOB_DEBUG_MODE)" is what you meant?
      If that is the case you have helped me out greatly!
      Update: After looking over the rest of the replies I think that probably is the case, but massive brownie points for responding 99% perfectly in under 2 minutes of me posting

      jcpunk
      all code is tested, and doesn't work so there :p (varient on common PM sig for my own ammusment)
Re: Sharing the scalar love?
by talexb (Chancellor) on Dec 22, 2003 at 21:05 UTC

    When you use a module in a script, any variables defined in that module with our (instead of my) are automatically exported.

    Alternatively, just declare the variable in one module (package) and refer to it using the package name, double colon, variable name.

    --t. alex
    Life is short: get busy!
      When you use a module in a script, any variables defined in that module with our (instead of my) are automatically exported.

      Can you post some code that demonstrates this?

        S.pm...

        package S; use strict; use warnings; our $x = 1;

        And here is a script using S.pm

        use strict; use warnings; use S; print $S::x; print "\n";

        Play that funky music white boy..

Log In?
Username:
Password:

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

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

    No recent polls found