Professional Web Applications Themes

How to pass global variables between files - PERL Miscellaneous

Hello, I am new to Perl. I want to do something simple but seem to be unable to do this. I want to call a subroutine and have it access the global variables in the caller program, similar to below where index.cgi is the calling program and called.cgi is the called program. How do I do this since there is no 'include' type of statement in Perl? -Monday index.cgi ========= #!c:/perl/bin/perl.exe our $globalVar = "Chicago"; callNow(); return 1; called.cgi ========== callNow() { print "global variable is $globalVar"; }...

  1. #1

    Default How to pass global variables between files

    Hello, I am new to Perl. I want to do something simple but seem to be
    unable to do this. I want to call a subroutine and have it access the
    global variables in the caller program, similar to below where
    index.cgi is the calling program and called.cgi is the called program.
    How do I do this since there is no 'include' type of statement in
    Perl? -Monday

    index.cgi
    =========
    #!c:/perl/bin/perl.exe
    our $globalVar = "Chicago";
    callNow();
    return 1;

    called.cgi
    ==========
    callNow() {
    print "global variable is $globalVar";
    }
    Monday Guest

  2. #2

    Default Re: How to pass global variables between files

    Monday wrote:
    > Hello, I am new to Perl. I want to do something simple but seem to be
    > unable to do this. I want to call a subroutine and have it access the
    > global variables in the caller program, similar to below where
    > index.cgi is the calling program and called.cgi is the called program.
    > How do I do this since there is no 'include' type of statement in
    > Perl? -Monday
    >
    > index.cgi
    > =========
    > #!c:/perl/bin/perl.exe
    > our $globalVar = "Chicago";
    > callNow();
    > return 1;
    >
    > called.cgi
    > ==========
    > callNow() {
    > print "global variable is $globalVar";
    > }
    you can use "do" as a simple include-ish sort of thing

    in index.cgi:

    do "called.cgi";

    "use" is better on the whole but you need to get into the idea of packages /
    modules and name spaces for that. Use "do" if you want dead simple code,
    use "use" if you want to import a "package" or "module".

    Chris
    --
    Real address: chris at lowth dot sea oh em.
    World's first wrist-watch PDA with Palm OS, available June 30
    from Amazon.com. Order now to beat the rush!
    [url]http://www.lowth.com/shop/wrist_pda[/url]
    Chris Lowth Guest

  3. #3

    Default Re: How to pass global variables between files

    [email]monday94301[/email] (Monday) writes:
    > Hello, I am new to Perl. I want to do something simple but seem to be
    > unable to do this. I want to call a subroutine and have it access the
    > global variables in the caller program, similar to below where
    > index.cgi is the calling program and called.cgi is the called program.
    > How do I do this since there is no 'include' type of statement in
    > Perl?
    I don't know, since you must be living in a paralell universe. In my
    universe Perl has require, use and do.
    > index.cgi
    > =========
    > #!c:/perl/bin/perl.exe
    > our $globalVar = "Chicago";
    > callNow();
    > return 1;
    >
    > called.cgi
    > ==========
    > callNow() {
    > print "global variable is $globalVar";
    > }
    It is confusing to use the file suffix ".cgi" for something that is
    not a CGI script.

    If you use a Perl4-style library then you can just do it.

    Perl4-style libraries are simply required and don't contain a package
    directive thus they are compiled in the same namespace a the file that
    required them. Perl4-style libraries should not be required by
    Perl5-style modules unless you really understand what you are doing.
    In fact, I would go so far as to say, you sould not write Perl4-style
    libraries unless you really understand why you are doing it.

    Conventionally a Perl4-style library has .pl extension but it doesn't
    actually matter.

    Perl5-style modules can export package scoped symbols (including
    variables) into the namespace of the package that use()s them. Also
    calling programs can directly manipulatate a module's package scoped
    symbols using a qualified name. This is the opposite of what you
    asked but probably achieves the effect you are looking for better than
    what you thought you wanted to do.

    You can learn about Perl5 modules in "perldoc perlmod".

    If you really want to access variables in the callers package
    namespace then use symbolic references and the caller subroutine. It
    is very rare that this is the right thing to do. Symbolic preferences
    are doented in "perldoc perlref". The caller function is
    doented in "perldoc -f caller".

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\
    . l___\\
    # ll l\\
    ###LL LL\\
    Brian McCauley Guest

  4. #4

    Default Re: How to pass global variables between files

    Monday <monday94301> wrote:
    > I want to call a subroutine and have it access the
    > global variables in the caller program,

    Why do you want to call a subroutine and have it access the
    global variables in the caller program?

    That will open you up to action-at-a-distance bugs.

    Why not pass the variables as subroutine arguments instead?

    > How do I do this since there is no 'include' type of statement in
    > Perl?

    What leads you to believe that there is no "include type" of
    statement in Perl?

    (a rhetorical question, since you do not need an "include"
    mechanism to get What You Want.
    )

    > index.cgi
    >=========
    > #!c:/perl/bin/perl.exe

    require '/path/to/called.cgi'; # so perl will know where sub callNow() is

    > our $globalVar = "Chicago";
    > callNow();
    > return 1;
    >
    > called.cgi
    >==========
    > callNow() {
    > print "global variable is $globalVar";

    print "global variable is $main::globalVar";

    > }

    1; # make 'require' happy



    See also:

    "Coping with Scoping":

    [url]http://perl.plover.com/FAQs/Namespaces.html[/url]


    --
    Tad McClellan SGML consulting
    [email]tadmcaugustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

Similar Threads

  1. CF pass variables to Flash
    By lvbao in forum Coldfusion Flash Integration
    Replies: 4
    Last Post: May 8th, 10:26 PM
  2. Global variables in AS3
    By doug777 in forum Macromedia Flex General Discussion
    Replies: 2
    Last Post: June 28th, 09:48 AM
  3. Can one pass variables through a selector?
    By Andrew Hunter in forum Mac Programming
    Replies: 5
    Last Post: September 10th, 03:18 PM
  4. Replies: 1
    Last Post: September 3rd, 01:30 PM
  5. How to pass global variables in shockwave movies?
    By WMC in forum Macromedia Director Basics
    Replies: 2
    Last Post: July 30th, 12:10 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139