perl: globals and a package.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl: globals and a package.
# 1  
Old 12-20-2004
perl: a global and a package.

I am still learning perl and confused on this script I am revising at work. The original author uses a package, which I have left in the code; however, I cannot seem to access the global variable $dir.

Code snippet:

Quote:
require 5.004;
package RRD_MG;
use strict;

my $dir = 'compdata';

sub sav_pg_data
{
get_env();
my $pg = $X->get_var("%p4");
my $sa = $X->get_text(17,"l");
open SA, ">$dir/$pg";
print SA $sa;
close SA;
}
I have also tried using $RRD_MG::dir to no avail.

Thank you.

Last edited by effigy; 12-20-2004 at 04:45 PM..
# 2  
Old 12-20-2004
Is $dir really global? Try removing the my definition. I may be wrong, but doesnt "my" reduce the scope of a variable? You may be inadvertantly reducing the scope of your $dir to a file (or sub routine) by using the "my" keyword. Try removing it.
# 3  
Old 12-20-2004
google,

When omitting the "my" I get the following:

Global symbol "$dir" requires explicit package name at mg_perl_routines.pl line 9.

I have tried using the package notation, as well as the "our" keyword. I am looking at a few books which have examples, but I have yet to understand this package stuff with perl Smilie
# 4  
Old 12-20-2004
Either you use

(1)

my $dir = 'compdata';

Or (2)

$RRD_MG::dir = 'compdata';
[...]
open SA, ">${RRD_MG::dir}/$pg";


There is NO such thing as

my $RRD_MG::dir

as package global is a separate concept from lexicals.

Of course, because "use strict" exists in the package, you need to make sure all references to package globals are qualified by the package name, so we need to put the package name in the open() statement above also, even though we refer to package globals in the same package we are currently in, which is implicitly assumed if "no strict" is in effect.

our() is a nice way to avoid this qualification in strict mode, though. But IMO I prefer qualifying package names explicitly always (if I want to go "strict") to make a visually clear distinction from lexicals (declared with "my"), which do not take any package qualifiers. Also, I think our() was only available since Perl 5.6 so servers whose perl version is out of date may not use this declaration.
# 5  
Old 12-21-2004
Thanks cbk, but I'm still having trouble Smilie

If I change the variable declaration to $RRD_MG::dir = 'compdata'; and leave the variables as $dir within the sub, I get
Variable "$dir" is not imported at (eval 661) line 26.

Now, if I change the $dir within the functions to ${RRD_MG::dir}, as you suggested, I do not get any errors, but I also do not get any output. The code works properly if I place my $dir = 'compdata'; inside of each sub.

I'll get this package stuff yet...
# 6  
Old 12-21-2004
I've narrowed the problem down. If I modify the script to be standalone, it functions correctly. The problem lies in the fact that it is being called within a typesetting program.

Problem solved. The way the script had to be called from the system was on a subroutine basis. Once I grasped this, I copied what the original programmer had done and I put $dir into the get_env sub like so:

Quote:
require 5.004;
package RRD_MG;
use strict;

my $X = undef;
my $dir = undef;

sub get_env { $X = XPPcompo::new(); $dir = 'compdata'; }

sub sav_state_data
{
get_env();
# $dir now works.
}
Thank you for your help google and cbk.Smilie

Last edited by effigy; 12-21-2004 at 04:32 PM..
# 7  
Old 12-22-2004
What a great feeling it is to fix a program on your own albeit with a little direction from your friends! Good work.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Php server globals REQUEST_URI or HTTP_SERVER_VARS

Hi all, recently I found that $_SERVER does not deliver the complete url with query anymore on my server. http://www.example.org/search.php?stichwort=wiki echo $_SERVER; /search.phpHowever $GLOBALS works in this case. http://www.example.org/search.php?stichwort=wiki echo $GLOBALS;... (1 Reply)
Discussion started by: lowmaster
1 Replies

2. Shell Programming and Scripting

Perl : Global symbol requires explicit package name Error while executing

I have executed the below perl script for copying the file from one server to another server using scp. #!/usr/bin/perl -w use Net::SCP::Expect; use strict; $server= "x.x.x.x"; my $source = "/mypath/mypath"; my $destination = "/home/"; print "Login...Starting scp..."; $user="admin";... (1 Reply)
Discussion started by: scriptscript
1 Replies

3. UNIX for Advanced & Expert Users

How to find dependancies of .dstream package (Solaris) & .rpm package( linux)

Friends, Please let meknow, How we can find the dependancies of .dstream package & .rpm package before installation ? For AIX, We can use the inutoc . command to create the .toc file for the bff package, What about Solaris & Linux ? (0 Replies)
Discussion started by: yb4779
0 Replies

4. Shell Programming and Scripting

Using localisation of a Perl package

I have a Perl package, which is widely used and tested, but recently a version in another language was added. The relevant po-files have been reviewed for this and are ready for testing. What are the steps to select the language by the user? From my understanding the language environment variable... (3 Replies)
Discussion started by: figaro
3 Replies

5. Shell Programming and Scripting

Java package restructuring using shell/sed/perl

Hi we are having more than 10k file source code clutterred across different directories. 1. we want to find duplicate file name 2. all java files and having having below content in various folder under /home/raxit/src --------- /*comment etc for few line */ package hello.a.b.c; ... (0 Replies)
Discussion started by: raxitsheth
0 Replies

6. Shell Programming and Scripting

perl for dbms_stats package

Hi, i am new to perl and i used to manullay gathering statistics since our database servers are 8i, could any body help to get the perl script or any link to automate this work. Thanks Prakash (1 Reply)
Discussion started by: prakash.gr
1 Replies

7. Shell Programming and Scripting

how to access globals in a function

I know any globals can be directly modified in a function without passing/returning parameters- provided the function is written within the same file. However, when I wrote functions in a saparate file (so hopefully they can be re-used by multiple script programs), and then call them from the main,... (11 Replies)
Discussion started by: bluemoon1
11 Replies

8. Shell Programming and Scripting

perl package question

can someone tell me how below package command worked? I understand how global1.pl works.. but i don't see how global3.pl is working.. Is package Fred command having first output look into $main:name??? # cat global3.pl #!/usr/bin/perl -w #use strict; $main::name = "Your name Here";... (1 Reply)
Discussion started by: hankooknara
1 Replies

9. Shell Programming and Scripting

perl package directories - what if the script is diff DIR to the one contain *.pm?

Hi there, say the package is in the ~/ and it's ~/packageFoo.pm I can use usePackage.pl in ~/ (~/usePackage.pl). Now, if I move it to ~/subDIR/usePackage.pl, the script won't work because it's not in the same DIR with packageFoo.pm How can i fix it? Thanks Gusla (1 Reply)
Discussion started by: gusla
1 Replies
Login or Register to Ask a Question