I'm trying to write code to comply with Perl best practices, and I hear everywhere that global variables are evil and should not be used (and I'm even beginning to understand why!) However, I'm not sure what I should do in the following case.
I have a perl script that looks like this:
#!/usr/bin/perl use strict; require "lib.pl"; my $global_variable1='a'; my $global_variable2='b'; my $global_variable3='c'; switch(&subroutine1){ case 0: &subroutine2; case 1: &subroutine3; case 2: &subroutine4; }
The file lib.pl looks like this:
#!/usr/bin/perl use strict; sub subroutine1 { return rand(3); } sub subroutine2 { print $global_variable1; } sub subroutine3 { print $global_variable2; } sub subroutine4 { print $global_variable3; }
What this does, of course, is that Perl tells me that $global_variables 1-3 in the file lib.pl need an explicit package name and compilation fails.
What is the best practice way of doing this? The global variables in the original script are things like paths to various directories, database variables, etc. My question really is: How do I use globals without using globals?
Please explain things slowly, without using any big words, because I'm a Perl noob!
In reply to What to use instead of global variables by tunafish
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |