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:
[SOLVED] How to use global variable in subroutine

 



waldauf
Novice

Nov 1, 2012, 3:08 AM

Post #1 of 6 (1539 views)
[SOLVED] How to use global variable in subroutine Can't Post

Hello perl's folk,

I would like to use global variable in subroutine. It is simply script which enables me turn on/out debug mode.



Code
#!/usr/local/bin/perl 

# watchdog for declaration of variables. At first you have to declare variable via "my $variable"
use strict;
# Print warnings messages
use warnings;

sub print_s {
if ( $DEBUG eq "1" ) {
print "$_";
}
else {
print "$DEBUG";
}
}


my $DEBUG = "0";

print_s "Today is good day to write good script in Perl.";



And output:

Code
 
$ ./debug.pl
Global symbol "$DEBUG" requires explicit package name at ./debug.pl line 11.
Global symbol "$DEBUG" requires explicit package name at ./debug.pl line 15.
Execution of ./debug.pl aborted due to compilation errors.



Or do you know better way how implement debug mode of script? :]

Thx,

Waldauf


(This post was edited by waldauf on Nov 2, 2012, 8:32 AM)


BillKSmith
Veteran

Nov 1, 2012, 5:19 AM

Post #2 of 6 (1535 views)
Re: [waldauf] How to use global variable in subroutine [In reply to] Can't Post

The short answer is to move the statement "my $DEBUG;" to the beginning of the program (before the subroutine)

Variables declared with 'my' are not global variables. They are called lexical variables. One of the advantages of 'use strict' is that it prevents the accidental use of global variables. In your case the $DEBUG in your subroutine is not in the scope of the other $DEBUG so perl thinks it is a true global variable, but that is not allowed by strict. The fix I described above increases the scope of $DEBUG to include the subroutine so everything works as you intend.

Read the section "VARIABLE SCOPING" in the document perlintro. (at the command line, Type: perldoc perlintro)
Good Luck,
Bill


waldauf
Novice

Nov 1, 2012, 7:32 AM

Post #3 of 6 (1529 views)
Re: [BillKSmith] How to use global variable in subroutine [In reply to] Can't Post

Hello BillKSmith,

thx for your answer. I read perlintro but I didn't find how to solve my problem. I would like to manage debug level of printed messages by command line arguments. If $DEBUG=0 so script will print only short messages. In case $DEBUG=1 so script will print more detailed information. For this reason I need load variable $DEBUG in subroutine print_s. Is there any way how to reach it?


Walda


FishMonger
Veteran / Moderator

Nov 1, 2012, 7:40 AM

Post #4 of 6 (1527 views)
Re: [waldauf] How to use global variable in subroutine [In reply to] Can't Post


Quote
I would like to manage debug level of printed messages by command line arguments.

That would be best accomplished by using the Getopt::Long module.
http://search.cpan.org/~jv/Getopt-Long-2.38/lib/Getopt/Long.pm


Quote
For this reason I need load variable $DEBUG in subroutine print_s. Is there any way how to reach it?

Yes, by following Bill's suggestion of putting the $DEBUG declaration prior to the sub declaration. In fact, it's best practice to put all subroutine declarations at the end of the script.


FishMonger
Veteran / Moderator

Nov 1, 2012, 7:59 AM

Post #5 of 6 (1522 views)
Re: [waldauf] How to use global variable in subroutine [In reply to] Can't Post

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)


waldauf
Novice

Nov 2, 2012, 8:32 AM

Post #6 of 6 (1512 views)
Re: [FishMonger] How to use global variable in subroutine [In reply to] Can't Post

Hi FishMonger,

this is exaxtly what I need! Thx for your example scritp :].


My good day for writing scripts continue ;].

 
 


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

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