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

Hi,

I'm working on a script and at one point the script calls up a subroutine located in an external file. In that external subroutine, it tries to print a variable that is located in the original script file. I can do this fine but I'm working in Perl Tk where it always asks you to use the "my" function to declare variables. That little function required in Perl/Tk causes my problem and I was wondering if anyone knew a way around it. Heres some code example to make this a little easier to understand:
require "external_subs.txt"; my $a = 1; my $b = blue; if($a == 1) { &numbers } else{ die;
That would be the main script file and this would be the external file, "external_subs.txt" :
sub numbers { print "$b"; } 1;
If you take out the 'my' fuctions, the whole thing works fine as you can see. But put them in and your stuck in my situation. =\

Also I'm using ActiveState 5.6.1 on Windows XP. Any help would be greatly appreciated! Thanks.

Replies are listed 'Best First'.
Re: Passing Variables to External Subroutines (Perl/Tk)
by NetWallah (Canon) on Jun 15, 2004 at 05:22 UTC
    "use warnings;" gives a clue as to the cause of this behaviour.

    The problem is fixed by using
    our $b = 'blue';
    instead of "my".

    "An our declaration declares a global variable that will be visible across its entire lexical scope, even across package boundaries. "

    Offense, like beauty, is in the eye of the beholder, and a fantasy.
    By guaranteeing freedom of expression, the First Amendment also guarantees offense.

Re: Passing Variables to External Subroutines (Perl/Tk)
by Plankton (Vicar) on Jun 15, 2004 at 05:18 UTC
    Oooh! Ohhh! I always wanted to say this ...

    You shoud use strict.

    Also you might not want to use the variable names $a and $b because of how sort works.
    Update with Example:
    Plankton@Chum_Bucket:~/perl/perlmonks> cat scritp.pl #!/usr/local/bin/perl use strict; require "ex.txt"; my $bb = "blue\n"; &numbers ( $bb ); Plankton@Chum_Bucket:~/perl/perlmonks> cat ex.txt sub numbers { my $v = shift; print "$v" } 1;

    Plankton: 1% Evil, 99% Hot Gas.
Re: Passing Variables to External Subroutines (Perl/Tk)
by matija (Priest) on Jun 15, 2004 at 05:23 UTC
    Maybe I'm missing something, but what's wrong with saying:
    &numbers($b);
    in the main script and
    sub numbers { my $b=shift @_; print "$b"; }
    in your external_subs.txt? That way, it will work with my, and use strict and all the rest of the stuff that will save you from having your brain fried once your script gets a bit more complex.
Re: Passing Variables to External Subroutines (Perl/Tk)
by antioch (Sexton) on Jun 15, 2004 at 13:08 UTC
    Ah I see, all of your guys solutions work. I guess it just comes down to preference in some situations. Also I was just using 'a' and 'b' as an example so there wouldnt be a problem, but thanks for lookin out Plankton.

    I didn't think it would be that easy but I guess it was, thanks a lot for all the help! =)

    -antioch