
FishMonger
Veteran
/ Moderator
Nov 1, 2012, 7:59 AM
Views: 6219
|
Re: [waldauf] How to use global variable in subroutine
|
|
|
Here's a short example showing the direction I'd take it.
#!/usr/bin/perl use strict; use warnings; use Getopt::Long; use Data::Dumper; my $DEBUG = 0; # set default debug level my ($dir, $file); # these are just addons to demonstrate the useage of Getopt::Long GetOptions ( 'debug|d=i' => \$DEBUG, 'path|p=s' => \$dir, 'file|f=s' => \$file, ); print Dumper $dir, $file; print_s("Today is good day to write good script in Perl."); ##### subroutine declarations ##### sub print_s { if ( $DEBUG == 1 ) { print $_[0]; } else { print $DEBUG; } } Execute as: example.pl -p /home/me -f script.pl -d 4 Output:
$VAR1 = '/home/me'; $VAR2 = 'script.pl'; 4 Run 2 example: example.pl -p /home/me -f script.pl -d 1 Output:
$VAR1 = '/home/me'; $VAR2 = 'script.pl'; Today is good day to write good script in Perl.
(This post was edited by FishMonger on Nov 1, 2012, 8:00 AM)
|