In the course of writing a bunch of perl code for my firm, I noticed that some subs were used over an over and some variables were most naturally implemented as global variables.

After some experimentation I hit upon a method of making this happen. Let "program.pl" be one of the perl programs which needs to use (a) one of these common subs and (b) needs to share some variables with this sub.

Context: This is in Windows 10 (ugh) using Strawberry perl v5.20.2.

I construct a file "includee.pm" which looks like this:

use vars qw { $SHAREDVARIABLE1 @SHAREDVARIABLE2 %SHAREDVARIABLE3 ... } sub function { ... }
Then in program.pl I put at the top this line:

use includee
Both program.pl and includee.pm are in the same folder, which is also the current folder when the program runs.

This actually produces the desired effect: the shared variables are accessible in both progam.pl and includee.pm, and the sub function is callable from program.pl.

The question is: is this legitimate? Am I depending on a quirk, which might go away in a future version of perl? In particular, will this method work in a linux context also?


In reply to Common subs and Global Variables by Anonymous Monk

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.