Global variables from a module, while using strict

PERL

    Next

  • 1. How to pass input from perl to shell programming?
    I have a perl program which executes a shell program using system call. This shell program needs user input for several questions it asks. How can I automate the user input so that user don't have to key in the input? thanks Swamy
  • 2. How to acces all of the archieves of google using perl and NNTP protocal? help
    hi, I want to download the archieves of one newsgroup and analyse them? Does anybody know how to get all the news? my local server just store the late news. IF anybody know the google server name, that could help. And thanks.
  • 3. How to detect $CO:GLOBAL:: overrides?
    Greetings! Does anyone know a way that I can detect whether *CORE::GLOBAL::die has been overridden? \&CORE::GLOBAL::die ne \&CORE::die, so I'm not sure how to handle it. The reason I ask is that I'm trying to workaround some bizarre behavior in the following code: my $OLD_DIE; BEGIN { # Store any previously overwritten die $OLD_DIE = \&CORE::GLOBAL::die; *CORE::GLOBAL::die = \&my_die; } sub my_die { # do some stuff &$OLD_DIE(@_); } If CORE::GLOBAL::die has already been overwritten, it works as expected. However, if it hasn't, then my_die gets called instead of CORE::die. (Somehow $OLD_DIE is being set to my_die?!) I figured if I could detect that it wasn't overridden, then I can directly call CORE::die and avoid the infinite recursion. Thanks, David

Global variables from a module, while using strict

Postby Shea Martin » Sat, 02 Jul 2005 02:58:35 GMT

his is a multi-part message in MIME format.


I have a module which is basically a about 50 global variables and about
2 methods. Because I am 'using' strict, all of the variables are
declared with 'my'.



I in my scripts which use the module, I can't access any of the
variables, though I can access the sub routines.



I looked at Exporter, but there are two problems with the examples I
see. One, they export the globals into the 'main' namespace, which I
don't want. I want them to be accessable like this: Module::myGlobal.
The other examples show the importing script explicity importing every
variable. This would be very tedious.



Here is an example of what I want:



<Sam.pm>

#!perl -w



use strict;

package Sam;



my $string = "sam";



sub Str

{

print "string = $string\n";

}





1;

</Sam.pm>



<samtest.pl>

#!perl -w



use strict;

use lib ".";

use Sam;



Sam::Str();

print "Sam::string = $Sam::string\n"; #can't access $Sam::string
here????



exit 0;

</samtest.pl>



Thanks,



~Shea M.


<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">

<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 11 (filtered medium)">
<style>
<!--
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman";}
a:link, span.MsoHyperlink
{color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{color:purple;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:Arial;
color:windowtext;}
@page Section1
{size:8.5in 11.0in;
margin:1.0in 1.25in 1.0in 1.25in;}
div.Section1
{page:Section1;}
-->
</style>

</head>

<body lang=EN-US link=blue vlink=purple>

<div class=Section1>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>I have a module which is basically a about 50 global
variables and about 2 methods.  Because I am ‘using’ strict, all of
the variables are declared with ‘my’.<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><o:p> </o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>I in my scripts which use  the module, I can’t access
any of the variables, though I can access the sub routines.<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><o:p> </o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>I looked at Exporter, but there are two problems with the
examples I see.  One, they export the globals into the ‘main’
namespace, which I don’t want.  I want them

Re: Global variables from a module, while using strict

Postby Robert Sedlacek » Sat, 02 Jul 2005 03:04:27 GMT

Shea Martin < XXXX@XXXXX.COM > wrote

Try: perldoc -f our

hth,p

-- 
There's a storm on the horizon. It's gonna be everywhere.
                        -- Michael Moynihan

Re: Global variables from a module, while using strict

Postby Paul Lalli » Sat, 02 Jul 2005 03:32:02 GMT



These two statements form a contradition.  Variables declared with 'my'
are, by definition, not global.  It is a common misperception that
"strict forces you to declare all variables with 'my'".  It does not.
What it does do is force you to fully qualify all package (ie, global)
variables.  That means if you have a package variable $bar in package
Foo, you must refer to it as $Foo::bar.

There is a way around this restriction.  Any package variable
"declared" (which is probably not the right word to use here) with the
keyword 'our' can be used for the duration of the lexical scope of the
declaration:

package Foo;
use strict;

$Foo::bar = 'Hello';

{
  our ($bar, $baz);
  $baz = 'World';
  print "$bar $baz";  # Prints  Hello World
}

print $bar;  # error!  Variable must be fully qualified.


That's because your 'my' variables went out of scope.  This happened
either 1) At the beginning of the next package statement; 2) At the end
of the innermost { } block; 3) At the end of the file in which they
were declared.  They don't exist anywhere else.


subroutines are always "global".  That is, they belong to a package.
And they are not held to the same "strict" restrictions as variables.


Exporter simply exports the variables.  It does not export them "into"
anything.  For that, you must have the corresponding "import" (which is
usually called automatically for you by the "use" statement).  If you
'use' a module Foo from within a package Bar, the variables you
exported will be imported into the package Bar, not package main.


Assuming you meant $Module::myGlobal, that is a package variable.  If
you want package variables, use package variables, not lexical
variables.  Either change all your 'my's to 'our's, or get rid of all
the 'my's and fully qualify the variables.


Have you considered creating one global configuration hash, and simply
exporting that?

package Foo;
use base 'Exporter';
our @EXPORT_OK = qw/%cfg/;
our %cfg;
$cfg{tmpdir} = "/tmp/";
$cfg{name} = "Paul";
# etc
1;

---------------------
#!/usr/bin/perl
use strict;
use warnings;
use Foo qw/%cfg/;

print "Name: $cfg{name}\n";

__END__


See also the descriptions of scoping available in
perldoc -f my
perldoc -f our
perldoc perlsub


Hope this helps,
Paul Lalli


Re: Global variables from a module, while using strict

Postby John W. Krahn » Sat, 02 Jul 2005 09:15:21 GMT





my() doesn't create package scoped variables.

$ perl -le'
my $c = 1;
our $d = 2;
$e = 3;
print for $c, $d, $e;

package foo;
print for $c, $d, $e;

package main;
print for $c, $d, $e;
'
1
2
3
1
2

1
2
3



John
-- 
use Perl;
program
fulfillment

Re: Global variables from a module, while using strict

Postby Paul Lalli » Sat, 02 Jul 2005 20:25:20 GMT





Huh.  How about that.  My mistake.  Thanks for the correction.

Paul Lalli


Similar Threads:

1.use strict, global variables, modules

Hi!

I'm writing a rather complicated script, reading bibtex (my code) and
xml (XML::Grove) files etc. 

I have to "use strict" and "my $variable" in order to keep references in
check, as there are lots of them.

I want to define some "global" variables in the main script instead of
passing them arround by reference to subs defined in modules.

Is there another, better, way to define them except like this: 

my $::blabla = "somevalue";

and then reading the values 

my $anotherone = $::blabla;


?


thank you,

emil per.




P.S.: RTFM is a magic word, and I tell it to myself often, but this time
I couldn't get it; do you recomend some reading on this very topic? man perl* did not help too much.




2."Global" Variable in Strict Mode?

Hi,

I'm attempting to write a script in safe mode that loops with a
variable/flag that needs to flip from 0 to 1 after the first run
through.  As an example:

use strict;

my $forever = 0;
my $magicVariable = 0;

while ($forever <= 0){
###Need some magic to check $magicVariable
if (#magic test = 0) {
 print "this is the first run \n";
  }

if (#magic test > 0) {
  print "this is the second or later run \n";
  }

sleep 10;
} # ends forever loop

I would love to be able to check the status of $magicVariable in other
loops/subroutines too.  Is there an elegant way of doing this?

3.Global variables / special variables / variable identifier with just one character

Expected error message: "Global symbol "$b" requires explicit package name".
However, I get result: "test".
I've checked the special variables and it looks like $b is one of these.

use warnings;
use strict;
zzz ($b);
print "$b\n";
sub zzz{
    $_[0] = "test";
}

Does it mean - that in general - we should not use one character variables
in Perl?
thanks
adam


4.Modules, global variables and such

Hello,

I have the following problem.  I had a script that contained only one
global variable which was called $DEBUG. I just used it to print out
various think or do some checks in debugging mode

  print "number of objects $n_obj\n" if $DEBUG ;
  check_sanity if $DEBUG ;

The program grew and now I splitted it up into several modules.

Question: what to do with the debug variable?  

* I could make it local to all of the functions, but I would really hate
  passing it through to each of the functions separately. Firstly, IMO this
  is one of the few cases where global variables are justified, and second,
  this would require me to make hundreds of modifications in the code.

* I could make it a global variable in each of the modules, and create a
  function that switches on the debugging for one module only. This would
  have the advantage that I could specify which module to debug.
  Disadvantage is that I need to remember to switch it on in every module
  if I want to switch it on globally.

* Finally, I could make a separate module with generic tests for sanity and
  printing warning messages, and that module would hold the only instance
  of the $DEBUG variable. Again, many modifications in the code required.

Of course, I can imagine that there are gazillions of other ways of doing
that, and that there are specific modules that do precisely what I want.
What route would you recommend?

Regards,

January


5.perl compilation issue - using global variable

Hi

Can someone please tell me how to compile a perl script which is
calling global variables and then how should the executable be used?

I have created a AIX shell script (tool.sh) that parses a couple of
files to export few global variables and then shows the end result.
Now i call these variables in an ftp script (ftp.pl) which i have
written in perl. When I run the master script (tool.sh) with ftp.pl,
it runs perfectly fine...but I wanted to compile ftp.pl to create and
ftp.out and then use ftp.out in the tool.sh script.
When I compile ftp.pl with perlcc, I am getting an executable which is
throwing the following error on execution.

# ./ftp.out
Can't use an undefined value as a HASH reference at ftp.pl line 3.

My ftp.pl is:

#!/usr/bin/perl

$ftpip=$ENV{'IP'};
$ftpserver=$ENV{'MNAME'};
$ftpusername=$ENV{'USER'};
$ftppass=$ENV{'PASS'};
$ftploc=$ENV{'LOC'};

use Net::FTP;

if($ftp = Net::FTP->new("$ftpip",Debug => 0)){print "Logging into
server $ftpserver \n";}else{print "could not login \n";exit;}

if ($ftp->login("$ftpusername","$ftppass")) { print "Username and
Password ACCEPTED\n"; } else { print "could not log into server with
Username and Password.
 Exiting!\n"; exit; }

if ($ftp->pasv) { print "Passive mode entered successfully\n"; } else
{print "Could not switch to pasive mode.\n"; }

if ($ftp->cwd("$loc")) { print "Changed to the directory $loc without
any trouble \n"; } else { print "Could not change to $loc directory
\n"; }

if ($ftp->put("/home/ashish/perl/touch.tst","$ftploc/touch.test"))
{print "file transferred properly \n"; } else { print "could not
transfer file \n";}

$ftp->quit;


i think the error is because of global variables. Can someone please
tell me how to compile a perl script which is calling global variable?

6. use Strict and using module

7. newbie question about scope, variables, declarations of variables and option strict (as in perl)

8. Use Strict, Perl 5.10 and Global Symbol requires explicit package name



Return to PERL

 

Who is online

Users browsing this forum: No registered users and 2 guest