CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [waldauf] How to use global variable in subroutine: Edit Log



FishMonger
Veteran / Moderator

Nov 1, 2012, 7:59 AM


Views: 1983
Re: [waldauf] How to use global variable in subroutine

Here's a short example showing the direction I'd take it.


Code
#!/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:

Quote
$VAR1 = '/home/me';
$VAR2 = 'script.pl';
4


Run 2 example:
example.pl -p /home/me -f script.pl -d 1

Output:

Quote
$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)


Edit Log:
Post edited by FishMonger (Veteran) on Nov 1, 2012, 8:00 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives