Passing Variables to External Subroutines (Perl/Tk)
antioch
created: 2004-06-15 00:54:19
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.
Re: Passing Variables to External Subroutines (Perl/Tk)
created: 2004-06-15 01:18:46
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 [id://148176|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)
created: 2004-06-15 01:22:05
"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)
created: 2004-06-15 01:23:11
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)
created: 2004-06-15 09:08:28
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

perlmonks.org content © perlmonks.org and antioch, matija, NetWallah, Plankton

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03