Global variables

2002-08-20 Thread Dan Fish

What would be the preferred method of creating global variables that can be
used in several different cgi scripts.  For example, I'd like to create the
variables $database, $user, $pass that I only have to change in one place,
but can use in many different scripts.

In C of course I'd just use #INCLUDE, but I'm not very "perliterate"
(yet)...:-)

Thanks,
-Dan
---
"Old programmers never die... Unless of course they refuse to accept a few
extra CPU cycles over months of efficiency tuning... [Have times changed or
WHAT!]"


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




global variables

2001-09-24 Thread Ruth Albocher

Hi.
I would like to use a global variable in my perl application, but since
everything in perl is in a package, it will always belong to some
package. what can I do?

thanks,
ruthie


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




'global' variables

2001-09-27 Thread Martijn van Exel

Hi all, 

I stuff all subroutines for my current perl experiment in a separate 
file, which i 'require' when needed. I would like to set a number of 
global variables in this file, which would then be imported together 
with the subroutines. How would I go about doing this?

-- 
martijn van exel -+- [EMAIL PROTECTED] -+- http://huizen.dds.nl/~mvexel/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




global variables

2006-01-10 Thread David Gama Rodrí­guez

Hello everyone !!

I'm really a newbie developing perl modules. I have and issue

I made a module that loads when Apache starts, in my module I declare a 
hash  as a global in order to be accesed in any place in my module,


my $hash;


sub x{
$foo = shift;
..
..
$hash->{x} = $foo;
}


sub y{


print $hash->{x};
}


in a webapageX I call the x sub and another webpageY I call y sub, I 
access webpageX once to put some value to the $hash so the trouble 
starts when I'm access the webpageY  from diferent locations and $hash 
lost its value declare in webpageX


I suppose that when I initialice the module when Apache starts it will 
hold that $hash variable available for every page that calls that 
module, is that right ? or ervrytime that it calls webpageY is a new 
instance of the module???


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Global variables

2002-08-20 Thread david

use package. example:

name the following DBString.pm:
#!/usr/bin/perl;
package DBString;
@EXPORT = qw($db $user $psw);
$db = 'db';
$user = 'user';
$psw = 'psw';
1;

then in another test script:

#!/usr/bin/perl
use DBString;
print $DBString::db,"\n"; #-- prints db
print $DBString::user,"\n"; #-- prints user
print $DBString::psw,"\n"; #- -prints psw

for more info, check out perldoc

david

Dan Fish wrote:

> What would be the preferred method of creating global variables that can
> be
> used in several different cgi scripts.  For example, I'd like to create
> the variables $database, $user, $pass that I only have to change in one
> place, but can use in many different scripts.
> 
> In C of course I'd just use #INCLUDE, but I'm not very "perliterate"
> (yet)...:-)
> 
> Thanks,
> -Dan
> ---
> "Old programmers never die... Unless of course they refuse to accept a few
> extra CPU cycles over months of efficiency tuning... [Have times changed
> or WHAT!]"


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Global variables

2002-08-21 Thread Connie Chan

I learnt something about this for days only, so don't
know if there are bugs here :

# Create a package glob_vars.pm #
package glob_vars;
require Exporter;
our @ISA = qw (Exporter);
our @EXPORT = qw (%VARS)
our %VARS = ();

$VARS{sth_a} = 'sth_a';
$VARS{sth_b} = 'sth_b';

1;
# End of package #


# A Code #

#! perl
use strict;
use glob_vars;

print $VARS{sth_a}; # sth_a

# End Code #


This example was not tested, but the idea is like this...

Rgds,
Connie






- Original Message - 
From: "Dan Fish" <[EMAIL PROTECTED]>
To: "Perl List" <[EMAIL PROTECTED]>
Sent: Wednesday, August 21, 2002 6:34 AM
Subject: Global variables


> What would be the preferred method of creating global variables that can be
> used in several different cgi scripts.  For example, I'd like to create the
> variables $database, $user, $pass that I only have to change in one place,
> but can use in many different scripts.
> 
> In C of course I'd just use #INCLUDE, but I'm not very "perliterate"
> (yet)...:-)
> 
> Thanks,
> -Dan
> ---
> "Old programmers never die... Unless of course they refuse to accept a few
> extra CPU cycles over months of efficiency tuning... [Have times changed or
> WHAT!]"
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: global variables

2001-09-24 Thread Sascha Kersken

Hi!

Perl 5.6 provides the 'our' statement as opposite to 'my': it makes a
variable global to a file in which it's used.

HTH

Sascha

- Original Message -
From: "Ruth Albocher" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, September 24, 2001 1:01 PM
Subject: global variables


> Hi.
> I would like to use a global variable in my perl application, but since
> everything in perl is in a package, it will always belong to some
> package. what can I do?
>
> thanks,
> ruthie
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>




Re: global variables

2001-09-24 Thread _brian_d_foy

In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] 
(Ruth Albocher) wrote:

> I would like to use a global variable in my perl application, but since
> everything in perl is in a package, it will always belong to some
> package. what can I do?

stay away from global variables. :)

what are you trying to do?
-- 
brian d foy <[EMAIL PROTECTED]> - Perl services for hire
CGI Meta FAQ - http://www.perl.org/CGI_MetaFAQ.html
Troubleshooting CGI scripts - http://www.perl.org/troubleshooting_CGI.html

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: global variables

2001-09-24 Thread _brian_d_foy

In article <001201c144fb$4a1ba3d0$ec00a8c0@boxx>, [EMAIL PROTECTED] 
(Sascha Kersken) wrote:

> Perl 5.6 provides the 'our' statement as opposite to 'my': it makes a
> variable global to a file in which it's used.

it declares a package variable, actually.  if you aren't in its
package, then you have to use the full package specification to
get to it.  it's not a global variable in the sense that something
like $_ is global (even though it lives in main::).

my() is not really the opposite of our(), either.  it limits the
the variable to whatever scope it is in, and a file is a type of
scope. :)
-- 
brian d foy <[EMAIL PROTECTED]> - Perl services for hire
CGI Meta FAQ - http://www.perl.org/CGI_MetaFAQ.html
Troubleshooting CGI scripts - http://www.perl.org/troubleshooting_CGI.html

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: global variables

2006-01-10 Thread vmalik
I think that a fresh new copy of the module is loaded into memory everytime it
is called. That's why your module loses its value. If I want to do something
like that, I'd consider storing the value on the hard drive somewhere.

Vishal


Quoting David Gama Rodrí­guez <[EMAIL PROTECTED]>:

> Hello everyone !!
> 
> I'm really a newbie developing perl modules. I have and issue
> 
> I made a module that loads when Apache starts, in my module I declare a 
> hash  as a global in order to be accesed in any place in my module,
> 
> my $hash;
> 
> 
> sub x{
>  $foo = shift;
> ..
> ..
>  $hash->{x} = $foo;
> }
> 
> 
> sub y{
> 
> 
> print $hash->{x};
> }
> 
> 
> in a webapageX I call the x sub and another webpageY I call y sub, I 
> access webpageX once to put some value to the $hash so the trouble 
> starts when I'm access the webpageY  from diferent locations and $hash 
> lost its value declare in webpageX
> 
> I suppose that when I initialice the module when Apache starts it will 
> hold that $hash variable available for every page that calls that 
> module, is that right ? or ervrytime that it calls webpageY is a new 
> instance of the module???
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>  
> 
> 





This mail sent through www.mywaterloo.ca

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: global variables

2006-01-10 Thread Tom Phoenix
On 1/10/06, David Gama Rodrí­guez <[EMAIL PROTECTED]> wrote:
> I made a module that loads when Apache starts, in my module I declare a
> hash  as a global in order to be accesed in any place in my module,
>
> my $hash;

Not to be picky about terminology, but a "my" variable is a lexical
variable, not a global variable. I think what you're talking about
here is a file-scoped lexical variable: It's accessible by name from
the rest of its file, but not from code in any other files.

But because Apache will fork many separate server processes,
persistent variables, whether global or not, may show surprising
behavior. Here's some documentation that may help, or you can search
other mod_perl resources:

http://modperlbook.org/html/ch06_04.html#pmodperl-CHP-6-SECT-4.3

Good luck with it!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: global variables

2006-01-11 Thread David Gama Rodrí­guez

David Gama Rodrí­guez wrote:


Tom Phoenix wrote:


On 1/10/06, David Gama Rodrí­guez <[EMAIL PROTECTED]> wrote:
 


I made a module that loads when Apache starts, in my module I declare a
hash  as a global in order to be accesed in any place in my module,

my $hash;
   



Not to be picky about terminology, but a "my" variable is a lexical
variable, not a global variable. I think what you're talking about
here is a file-scoped lexical variable: It's accessible by name from
the rest of its file, but not from code in any other files.

But because Apache will fork many separate server processes,
persistent variables, whether global or not, may show surprising
behavior. Here's some documentation that may help, or you can search
other mod_perl resources:

   http://modperlbook.org/html/ch06_04.html#pmodperl-CHP-6-SECT-4.3

Good luck with it!

--Tom Phoenix
Stonehenge Perl Training
 

Thank you so much for your help I read the link and I think the way 
I'm tryin to use the $hash is incorrect
I will do it as Vishal said maybe storing the value in some place to 
be available in any request. Perhaps chapter 10 will help me more





Re: Global Variables Question

2001-04-25 Thread Paul


--- "Warren D. Johnson" <[EMAIL PROTECTED]> wrote:
> 
> Test.pl uses variables (via use vars) from config.pl.  These
> variables are defined and given values in config.pl.  AFter the first
> usage of test.pl, the variables we are pulling in from config.pl no
> longer have values.  So it's something like this:
> 
> config.pl>>>>>>>
> 
> use vars ($G_ROOTPATH);
> $G_ROOTPATH = "/home/warren";
>
> <<<<< 
> 
> 
> test.pl>>>>>>>
> 
> require config.pl;
> 
> use vars ($G_ROOTPATH);
> # at this point, G_ROOTPATH is inherited from config.pl and has the
> value of
> /home/warren
> 
> <<<<<<< 
> On the first run of the script G_ROOTPATH in test.pl has the value of
> home/warren...  subsequent runs, it does not.
> 
> I'm a little confused as to exact ways modperl handles global
> variables.
> Anyone have a simple tweak for this? Or do I have to make a config
> package?
> 
> Thanks in advance!

Try putting the "use vars" statement *before* the require, and se if it
helps. Personally, this one's new to mee, too, so I'll be watching your
responses myself!

__
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/



global variables in subroutines..?

2001-07-18 Thread Stephanie Stiavetti

I'm writing a script to check for validity of form elements.  here's what I
have so far:

my ($job) = $cgi->param("job");

bla bla bla more script bla bla bla


sub fixFailed
{   my (@failedFields);
my ($validForm)="1";

#validate job selection
if (!$job) {
$job="job ";
push (@failedFields, $job);

}



my question is, I remember someone a while back mentioning that you should
NOT redefine global variables in your subroutines... what can I do as an
alternative?  what do I risk by doing it anyways?





`°º¤,ô¿ô,¤º°``°º¤,ô¿ô,¤º°``°º¤,ô¿ô,¤º°``°º

Stephanie J. Stiavetti
Production Tools
Industrial Light + Magic
415-448-3213 <-|-> [EMAIL PROTECTED]

`°º¤,ô¿ô,¤º°``°º¤,ô¿ô,¤º°``°º¤,ô¿ô,¤º°``°º

And then the day came when the risk to 
stay tight in a bud became more painful 
than the risk it took to blossom.  
   --Anais Nin

`°º¤,ô¿ô,¤º°``°º¤,ô¿ô,¤º°``°º¤,ô¿ô,¤º°``°º

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




[Q]Setting global variables.

2002-02-22 Thread Bhanu Prakash

Perl Gurus,
   How can I set global variables in perl.?
I'm trying to do something like..

if(param())
{
my $myvar=param("myvar");
if(param("myvar2"){
use $myvar here..
Has some script to show a form..

}
else {
On submitting the form, user enters here..
someother use of $myvar..
Here, my script is showing value of $myvar as null.
I wanted to use the same value of myvar which I had
before submitting the form.
}

Am I missing something? Or should I set a global
variable for myvar?!
Thanks
Bhanu.

=
Bhanu Prakash G V S

__
Do You Yahoo!?
Yahoo! Sports - Coverage of the 2002 Olympic Games
http://sports.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Local and global variables

2005-06-28 Thread Andrew Black

Can someone explain the difference between
  use vars qw/$defined_by_vars/ ;
  our $defined_by_our ;
  $not_explicily_defined = 1 ;

Are these all global to the package they are defined it.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Inheritance of package global variables

2001-06-28 Thread Richard J. Barbalace

Hi.

I feel like I'm asking a lot of questions lately, but this list has
been extremely helpful. :)

I'm writing some packages that inherit from a base class, which has
some fields and some global variables that I want inherited.  I have
code like:
 # La/De/Da/MyBase.pm
 package La::De::Da::MyBase;
 .
 use fields qw( MyField );
 use vars qw( %Attributes );
 %Attributes = ( 'a' => 1, 'b' => 2);

 # La/De/Da/MyPackage.pm
 package La::De::Da::MyPackage
 .
 use base qw( La::De::Da::MyBase ); # Takes care of @ISA and %FIELDS.
 use vars qw( %Attributes );# But also need vars.
 %Attributes = %La::De::Da::MyBase::Attributes;
 $Attributes{'c'} = 3;  # Extend the attributes hash

The 'use base' pragma nicely takes care of the @ISA and %FIELDS
variables for me, but I also need to have the package global variable
%Attributes inherited.  The 'use vars' and assignment in MyPackage is
rather verbose, and I'm wondering if there is a better or terser way
of doing that.  What's the recommended way of inheriting package
global variables?

+ Richard J. Barbalace



Re: global variables in subroutines..?

2001-07-18 Thread Brett W. McCoy

On Wed, 18 Jul 2001, Stephanie Stiavetti wrote:

> I'm writing a script to check for validity of form elements.  here's what I
> have so far:
>
> my ($job) = $cgi->param("job");
>
> bla bla bla more script bla bla bla
>
>
> sub fixFailed
> { my (@failedFields);
>   my ($validForm)="1";
>
>   #validate job selection
>   if (!$job) {
>   $job="job ";
>   push (@failedFields, $job);
>
>   }
>
> my question is, I remember someone a while back mentioning that you should
> NOT redefine global variables in your subroutines... what can I do as an
> alternative?  what do I risk by doing it anyways?

You should pass the global variable into your subroutine as an argument.
Functions that modify global variables that other functions are depending
on can lead to some hard to find bugs.  A function should be like a black
box, data in and data out, decoupled, if possible, from other functions.
Sometimes this rule has to be broken, but it is a basic premise of good
software design.

I came across some code I had to do some bug fixes last week that realyl
had me shuddering.  It was structured like this (mind you, they original
author was not using -w or strict, so the script had all kinds of things
that even Matt Wright would shudder at):

function1 {

  $stuff = "something";
  $junk = "more stuff";

  #code
  #code

  function2();

  #code
  #code
}

function2 {

  $otherjunk = $junk;
  $stuff = $junk + $something;

  #code
  #code
}

-- Brett
   http://www.chapelperilous.net/btfwk/

Zero Mostel: That's it baby!  When you got it, flaunt it!  Flaunt it!
-- Mel Brooks, "The Producers"


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




use strict, global variables, modules

2003-09-04 Thread Emil Perhinschi
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.





-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: [Q]Setting global variables.

2002-02-22 Thread Jonathan E. Paton

> Perl Gurus,
>
> How can I set global variables in perl.?  I'm trying
> to do something like..

The logic is broken, I believe this is more appropriate:

my $myvar = param("myvar");

unless ($myvar) {
# Show form
}
else {
# Process form
}

Jonathan Paton

__
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Global Variables and use strict;

2001-11-08 Thread Tomasi, Chuck

Thanks a lot to Jeff 'japhy' Pinyan and Etienne Marcotte for helping me
understand packages and variable references today!


It's starting to become clear to me now.  Thank you.

I have to options.  Use "require Exporter" and export the variables I would
like to, which could be cumbersome if the variable list gets long, and could
cause conflicts between exported package variables (globals) and locals of
the same name.

--or--

Reference the package (global) variables as $pkgname::variable, typing a
little more, but avoiding abmiguity.

I've used these from other's examples in the past, but now I understand them
well enough to construct and use them myself.  My Perl knowledge just went
up another knotch!

Thank you again!

--Chuck

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Inheritance of package global variables

2001-06-28 Thread Michael Fowler

On Thu, Jun 28, 2001 at 03:05:17PM -0400, Richard J. Barbalace wrote:
> The 'use base' pragma nicely takes care of the @ISA and %FIELDS
> variables for me, but I also need to have the package global variable
> %Attributes inherited.  The 'use vars' and assignment in MyPackage is
> rather verbose, and I'm wondering if there is a better or terser way
> of doing that.  What's the recommended way of inheriting package
> global variables?

Exporting is usually a bad idea when dealing with classes, it breaks
encapsulation.  You should probably setup a class method for this.

That being said, you can export variables just like you export any other
data type, with Exporter; perldoc Exporter or
http://www.perldoc.com/perl5.6/lib/Exporter.html.


Michael
--
Administrator  www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--



Re: Inheritance of package global variables

2001-06-28 Thread Richard J. Barbalace

Michael Fowler <[EMAIL PROTECTED]> replies:
> Exporting is usually a bad idea when dealing with classes, it breaks
> encapsulation.  You should probably setup a class method for this.
> 
> That being said, you can export variables just like you export any other
> data type, with Exporter; perldoc Exporter or
> http://www.perldoc.com/perl5.6/lib/Exporter.html.

I don't really want to export the variables; I'm not modifying them in
the parent package, just copying and expanding them in the inheriting
package.

Here's what I'm really trying to do.  I have a set of packages that
closely inherit from a base class.  The base class and all subclasses
have some common attributes; the subclasses may add more attributes to
this set.  Some attributes may be modified in certain ways and some
may not be modified.  As such, I'm using separate accessor methods
get() and set() for setting the attributes, as in:
$attribute_value = $object->get('attribute_name');
$object->set('attribute_name', $attribute_value);

The 'set' method needs to know what are legal attributes and what are
legal values for a given attribute.  I'm using a hash of attribute
names and regexes to define what are legal values for the allowed
attributes:
# Object attributes are changeable only if their values match the regexes
%Attributes = (
   # Immutable attributes have regexp undefined
   'id' => undef,
   'creator'=> undef,
   'creation_time'  => undef,
   # Mutable attributes have regexp specified
   'name'  => '^.{1,30}$',
   'description'   => '^.{0,255}$',
   .
   );

The 'set' method then uses this hash to verify the names and values of
attributes are acceptable before setting them.  The simplified code
looks something like:
sub set {
# $self->set($name, $value);

# Get parameters and initialize variables
my ($self, $name, $value) = @_;

# Check attribute
die "Invalid attribute $name provided"
unless exists $Attributes{$name};
die "Cannot change attribute $name"
unless defined $Attributes{$name};
die "The provided value for attribute $name is not acceptable"
unless ($value =~ /$Attributes{$name}/);
.
# Set attribute
$self->{$name} = $value;
}

(The real code is more complicated since it allows setting multiple
attributes at once.)

The package inheriting from this parent class might add more attributes:
# La/De/Da/MyPackage.pm
package La::De::Da::MyPackage
.
use base qw( La::De::Da::MyBase ); # Takes care of @ISA and %FIELDS.
use vars qw( %Attributes );# But also need to extend %Attributes.
%Attributes = (
   # MyPackage inherits attributes from MyBase
   %La::De::Da::MyBase::Attributes,
   # Mutable attributes have regexp specified
   'color' => '^(red|green|blue)$',
   'size   => '^(small|medium|large)$',
   'price' => '^\$\d+\.\d\d$',
   );

Another package might define different additional attributes.  For a
'MyPackage' object:
my $object = La::De::Da::MyPackage->new();
$object->set('name', 'Pickle');   # OK
$object->set('description', 'dill');  # OK
$object->set('color', 'green');   # OK
$object->set('size', 'giant');# dies, since not an acceptable value
$object->set('weight', 'heavy');  # dies, since not an acceptable name
$object->set('id', 50);   # dies, since id is unchangeable

This has the advantage of having a single 'set' method in the parent
package that is smart enough to do the necessary error validation for
all inheriting packages, instead of having a separate specialized
'set' method for each package.  This is a real savings since I have a
large number of inheriting packages.

I have not been able to find a perl module that would allow this sort
of inheritance and data validation; does anyone know of one that does
this?  Or can anyone suggest a better way of doing what I want?

+ Richard J. Barbalace



Re: Inheritance of package global variables

2001-06-28 Thread Me

> [inherited attributes]
> [get/set accessor methods]
> [regex validation of set]
> [better way?]

I'm not sure about there being a *better* way,
but I'm sure there are a lot of *other* ways.

Various thoughts...

Perl has the concept of tied data items. The basic
operations on those data items, like setting the
value, are done via arbitrary procedures you write.
I can see one implementing what you describe, as
a tied hash. To see a little more of what people
have done with tie's in general:
http://search.cpan.org/search?mode=module&query=tie

Of course, there may be other tied hash implementations
not on cpan.

If you don't have Damian Conway's book, get it.

You might be interested in considering how CLOS
(common lisp object system) notions relate to what
you are doing. With CLOS, firing a method (which
could be an Accessor method), meant the method
dispatcher first looked to see if any Before methods
matched the argument signature. If so, it fired the
matching Before method(s). Those Before methods
could then modify the dispatch process, eg abort the
dispatch. At least, this is how it worked when I last
looked at CLOS, about 10 years ago.

This is a very general framework with what you are
doing being one of many things that are naturally
expressed using it. You could have a Before method
that validates the argument to an Accessor method 
against a regex, and accepts/aborts the dispatch. 
But to get this to work with Perl, you'd have to play
with the guts of method dispatch, something I would
guess is a relatively non-trivial undertaking.




Re: Inheritance of package global variables

2001-06-29 Thread Michael Fowler

On Fri, Jun 29, 2001 at 12:14:23AM -0400, Richard J. Barbalace wrote:
> I don't really want to export the variables; I'm not modifying them in
> the parent package, just copying and expanding them in the inheriting
> package.

Well then, you are copying and modifying as simply as it can be done. 
Again, though, I would avoid using variables like that, and instead go for a
class method approach:

package Foo;

sub attributes { return (foo => undef) }

sub set {
my($self, $attribute, $value) = (shift, shift, shift);
my %attr = $self->attributes;
if (defined $attr{$attribute}) {
...
} else {
...
}
}


package Foo::Bar

use base qw(Foo);

sub attributes {
my $self = shift;
return ($self->SUPER::attributes(), foo_bar => '.*')
}


This will get costly, so perhaps some caching is in order.  The point is,
classes shouldn't be getting to class data except through class methods.


> I have not been able to find a perl module that would allow this sort
> of inheritance and data validation; does anyone know of one that does
> this?  Or can anyone suggest a better way of doing what I want?

If you haven't already, you should check the Class:: modules on CPAN to see
if something already does this.  However, data validation is so arbitrary
that the closest you can get is probably something that sets up your
inherited accessors and then calls back to your code, if you provide it, for
verifying the data.


Michael
--
Administrator  www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--



Global variables and a forked process

2003-08-14 Thread Ahmed Moustafa
Does a forked process share the memory locations of the global variables 
from the parent process?

Thanks in advance!

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: use strict, global variables, modules

2003-09-04 Thread John W. Krahn
Emil Perhinschi wrote:
> 
> Hi!

Hello,

> 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;

our $blabla;


perldoc -f our



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Global Variables: 'my' vs 'use vars'

2002-03-28 Thread eric-perl

Hello, All:

I've never been very good at scoping so it it's no surprise that this 
confuses me:

  When declaring variables at the beginning of a script, what is the 
  difference between 'my' and 'use vars'?

-- 
Eric P.
Los Gatos, CA


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Global variables in forked processes

2002-05-09 Thread Ahmed Moustafa

Thanks a lot. I understand that.

I was thinking that variable 'x' of a forked process 'p2' would point at 
the same memory location of variable 'x' of a parent process 'p1'. That 
can't be true. If that was true, 'p1' and 'p2' would be identitcal (no 
need to fork!).

-- 
Ahmed Moustafa
http://pobox.com/~amoustafa


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Global variables in forked processes

2002-05-10 Thread Chas Owens

On Fri, 2002-05-10 at 00:12, Ahmed Moustafa wrote:
> Thanks a lot. I understand that.
> 
> I was thinking that variable 'x' of a forked process 'p2' would point at 
> the same memory location of variable 'x' of a parent process 'p1'. That 
> can't be true. If that was true, 'p1' and 'p2' would be identitcal (no 
> need to fork!).
> 
> -- 
> Ahmed Moustafa
> http://pobox.com/~amoustafa


N.B. This email is full of unimportant hairsplitting.

In many operating systems fork is implemented using copy-on-write.  This
means that when you fork both processes refer to the same memory
locations until one of them tries to change the bytes stored there.  At
that time the change bytes are copied to a new memory space for the
child.
 
-- 
Today is Setting Orange the 57th day of Discord in the YOLD 3168
Or not.

Missile Address: 33:48:3.521N  84:23:34.786W


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Global variables in forked processes

2002-05-10 Thread drieux


On Friday, May 10, 2002, at 09:20 , Chas Owens wrote:
[..]
> In many operating systems fork is implemented using copy-on-write.  This
> means that when you fork both processes refer to the same memory
> locations until one of them tries to change the bytes stored there.  At
> that time the change bytes are copied to a new memory space for the
> child.
[..]

I saw this and it reminded me that you had proposed threads
as a 'shared memory' solution - but you can get into as much
of a problem in the thread space just as quickly without managing
who does the synchronization on any globally accessible memory...

We got to the point where we started to just say,

"Oh don't worry about that, they are
planning to just fork another thread..."

at one gig - as the 'designers' and 'management' displayed
a complete lack of awareness about the consequences of their
blithe optimism that 'threads are the wave'

Let's just say they nailed a sun E1 to the bulk head
as they bloated their one Process

So folks may wish to understand the problems of 'process
management', shared memory, and the various types and
forms of Interprocess communications, before they try to
avoid the problems of process management, shared memory,
and Interprocess communications, by

'well we can do it in threads -
they are lightweight .'

and learn that a camel can only carry so much hay
and it ain't any better with fresh brewed java or

WayCoolHotNewBuzzGeneratedTechNoirWaveOfTheFuture

ciao
drieux

---


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




using global variables in other files

2001-10-19 Thread Васильченко Наталья Анатольевна

Hello, all!

I have some variables defined in file 


$user_ip = $ENV{'REMOTE_ADDR'};
$user_name = $ENV{'AUTH_USER'};
.
Now I want to use these values in other files.
  

unless ($user_ip =~ /^10\./)... # 
#etc.

What should I do? use 'require userinfo.pl;' or use 'package' or something
else?  

Thank you!

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Defining Global Variables w/use strict;

2001-11-08 Thread Tomasi, Chuck

System: Sun Ultra 2, Solaris 7 (11/99)
Perl: 5.6.0

I have a series of related programs that need global definitions ($DOMAIN,
$ADMIN, $DBNAME, etc).  My code looks something like this:

--
#!/usr/local/bin/perl -w

use strict;
use DBI;

require "defs.pl";

print "Welcome to $DOMAIN, $ADMIN\n";



#!/usr/local/bin/perl -w

use strict; 

my $DOMAIN="plexus.com";
my $ADMIN="Chuck";



Of course, when I run g.pl I get the infamous,

Global symbol "$DOMAIN" requires explicit package name at ./g.pl line 7.
Global symbol "$ADMIN" requires explicit package name at ./g.pl line 7. 
Execution of ./g.pl aborted due to compilation errors.

I've tried "my" and "local", but local just changes the error to complain
about an explicit package being required.  I'm missing something basic.

How can I define these variables to be known in g.pl, h.pl, and other
programs that require defs.pl?

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Global variables and a forked process

2003-08-10 Thread Steve Grazzini
On Sat, Aug 09, 2003 at 05:14:34PM -0700, Ahmed Moustafa wrote:
> Steve Grazzini wrote:
>> Changes made after the fork() won't be visible in the other
>> process, if that's what you're wondering.
> 
> Yes, that's exactly what I am wondering. So, is there a way to
> assign values to the global variables in the parent process?

No.  You'd need to switch to threads or keep the fork and
use some kind of IPC.

% perldoc perlipc

What is your application doing?

-- 
Steve

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Global variables and a forked process

2003-08-10 Thread Ahmed Moustafa
Steve Grazzini wrote:
On Sat, Aug 09, 2003 at 05:14:34PM -0700, Ahmed Moustafa wrote:

Steve Grazzini wrote:

Changes made after the fork() won't be visible in the other
process, if that's what you're wondering.
Yes, that's exactly what I am wondering. So, is there a way to
assign values to the global variables in the parent process?


No.  You'd need to switch to threads or keep the fork and
use some kind of IPC.
% perldoc perlipc
Are there threads in Perl?

What is your application doing?
It scans FTP home directories, once it finds a file, it validates the 
file name, if it is expected or not and then encrypts the file.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: Global variables and a forked process

2003-08-11 Thread Wiggins d'Anconia
Ahmed Moustafa wrote:
Steve Grazzini wrote:

On Sat, Aug 09, 2003 at 05:14:34PM -0700, Ahmed Moustafa wrote:

Steve Grazzini wrote:

Changes made after the fork() won't be visible in the other
process, if that's what you're wondering.


Yes, that's exactly what I am wondering. So, is there a way to
assign values to the global variables in the parent process?


No.  You'd need to switch to threads or keep the fork and
use some kind of IPC.
% perldoc perlipc


Are there threads in Perl?
Yes and no. There are multiple different implementations of threads all 
of which have some good and bad points. You will need to look more into 
them depending on your version of Perl and how complex the task is that 
you wish to accomplish. (There is also a separate mailing list for perl 
threading).


What is your application doing?


It scans FTP home directories, once it finds a file, it validates the 
file name, if it is expected or not and then encrypts the file.


I have written a similar application using POE, you may want to have a 
look at it both for doing your process forking (see POE::Wheel::Run) and 
for your directory scanning.

http://poe.perl.org

http://danconia.org

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: Global variables and a forked process

2003-08-11 Thread Steve Grazzini
On Sat, Aug 09, 2003 at 01:04:26PM -0700, Ahmed Moustafa wrote:
> Does a forked process share the memory locations of the global 
> variables from the parent process?

Changes made after the fork() won't be visible in the other
process, if that's what you're wondering.

-- 
Steve

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Global variables and a forked process

2003-08-14 Thread Ahmed Moustafa
Steve Grazzini wrote:
On Sat, Aug 09, 2003 at 01:04:26PM -0700, Ahmed Moustafa wrote:

Does a forked process share the memory locations of the global 
variables from the parent process?


Changes made after the fork() won't be visible in the other
process, if that's what you're wondering.
Yes, that's exactly what I am wondering. So, is there a way to assign 
values to the global variables in the parent process?

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: Global variables and a forked process

2003-08-14 Thread wiggins


On Mon, 11 Aug 2003 08:11:07 -0500 (CDT), Paul Archer <[EMAIL PROTECTED]> wrote:

> Yesterday, Wiggins d'Anconia wrote:
> 
> > Yes and no. There are multiple different implementations of threads all
> > of which have some good and bad points. You will need to look more into
> > them depending on your version of Perl and how complex the task is that
> > you wish to accomplish. (There is also a separate mailing list for perl
> > threading).
> >
> Do you know where this mailing list can be found? I didn't see it on the
> "list of lists" (pardon the pun) at perl.org...
> 

Its actually an iThreads list but discussion of the multiple threading models has 
taken place...

http://lists.perl.org/showlist.cgi?name=iThreads

I've often wondered why there isn't just a more general "threads" or "threading" list, 
maybe because none of the models is universally accepted?...

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Global variables and a forked process

2003-08-14 Thread Paul Archer
Yesterday, Wiggins d'Anconia wrote:

> Yes and no. There are multiple different implementations of threads all
> of which have some good and bad points. You will need to look more into
> them depending on your version of Perl and how complex the task is that
> you wish to accomplish. (There is also a separate mailing list for perl
> threading).
>
Do you know where this mailing list can be found? I didn't see it on the
"list of lists" (pardon the pun) at perl.org...

Paul




 Never ascribe to malice what can perfectly
 well be explained by stupidity. -Anonymous


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Global Variables: 'my' vs 'use vars'

2002-03-28 Thread Chas Owens

On Thu, 2002-03-28 at 14:55, [EMAIL PROTECTED] wrote:
> Hello, All:
> 
> I've never been very good at scoping so it it's no surprise that this 
> confuses me:
> 
>   When declaring variables at the beginning of a script, what is the 
>   difference between 'my' and 'use vars'?
> 
> -- 
> Eric P.
> Los Gatos, CA

The big difference is that 'use vars;' doesn't produce warnings when
used with Getopt::Std .  I think it boils down to the fact that
use vars is compiler directive (pragma) that prevents specific warnings
from being issued and my is a function that declares a local variable. 
Variables get declared when they are used by default in Perl (unlike
some languages like C).  This can be a good thing if you only need 10
lines of code and don't want to waste time typing; however, it is a very
bad thing in larger programs (ie you meant to type @commands but typed
@command instead and it simple gets created).  This is why they added
warnings to tell you about one use variables and various other
warnings.  But that was not enough; some programmers still had tons of
errors, so they added 'use strict;' which forces you to do some things
that just make sense for large projects.

Also note that use vars has been superseded by the our command in later
versions of perl (5.6 and up). 


NOTE: The functionality provided by this pragma has been
superseded by "our" declarations, available in Perl v5.6.0
or later.  See the our entry in the perlfunc manpage.




-- 
Today is Boomtime the 14th day of Discord in the YOLD 3168
This statement is false.

Missile Address: 33:48:3.521N  84:23:34.786W


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: using global variables in other files

2001-10-22 Thread Martin

Transform your pl file in a package and then use use to get variables, 
or qualify the full name : $modname:varname.
define this variables global in modul or export the vars .
take a look on Exporter Modul
hope it helps
martin


÷ÁÓÉÌØÞÅÎËÏ îÁÔÁÌØÑ áÎÁÔÏÌØÅ×ÎÁ wrote:

> Hello, all!
> 
> I have some variables defined in file 
> 
> 
> $user_ip = $ENV{'REMOTE_ADDR'};
> $user_name = $ENV{'AUTH_USER'};
> .
> Now I want to use these values in other files.
>   
> 
> unless ($user_ip =~ /^10\./)... # 
> #etc.
> 
> What should I do? use 'require userinfo.pl;' or use 'package' or something
> else?  
> 
> Thank you!
> 
> 



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Defining Global Variables w/use strict;

2001-11-08 Thread dan radom

just a thought, but how can you print a variable that hasn't been defined yet?

dan

* Tomasi, Chuck ([EMAIL PROTECTED]) wrote:
> System: Sun Ultra 2, Solaris 7 (11/99)
> Perl: 5.6.0
> 
> I have a series of related programs that need global definitions ($DOMAIN,
> $ADMIN, $DBNAME, etc).  My code looks something like this:
> 
> --
> #!/usr/local/bin/perl -w
> 
> use strict;
> use DBI;
> 
> require "defs.pl";
> 
> print "Welcome to $DOMAIN, $ADMIN\n";
> 
> 
> 
> #!/usr/local/bin/perl -w
> 
> use strict; 
> 
> my $DOMAIN="plexus.com";
> my $ADMIN="Chuck";
> 
> 
> 
> Of course, when I run g.pl I get the infamous,
> 
> Global symbol "$DOMAIN" requires explicit package name at ./g.pl line 7.
> Global symbol "$ADMIN" requires explicit package name at ./g.pl line 7. 
> Execution of ./g.pl aborted due to compilation errors.
> 
> I've tried "my" and "local", but local just changes the error to complain
> about an explicit package being required.  I'm missing something basic.
> 
> How can I define these variables to be known in g.pl, h.pl, and other
> programs that require defs.pl?
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Defining Global Variables w/use strict;

2001-11-08 Thread Etienne Marcotte

> --
> #!/usr/local/bin/perl -w
> 
> use strict;
> use DBI;
> 
> require "defs.pl";
> 
> print "Welcome to $DOMAIN, $ADMIN\n";
> 

--

#!/usr/local/bin/perl -w

use strict;
use DBI;
use defs;
 
print "Welcome to defs::$DOMAIN, defs::$ADMIN\n";


> 
> #!/usr/local/bin/perl -w
> 
> use strict;
> 
> my $DOMAIN="plexus.com";
> my $ADMIN="Chuck";
> 
> 

 # note it's now pm
 
package defs;

$DOMAIN = "plexus.com";
$ADMIN = "Chuck";

1; #return true
 



I hope it's clear enough, I don't know if I wrote enough details:-)

Etienne

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Defining Global Variables w/use strict;

2001-11-08 Thread Etienne Marcotte

He want's to take then in his global variables list.. where $DOMAIN is
plexus.com and ADMIN is Chuck

Etienne

dan radom wrote:
> 
> just a thought, but how can you print a variable that hasn't been defined yet?
> 
> dan
> 
> * Tomasi, Chuck ([EMAIL PROTECTED]) wrote:
> > System: Sun Ultra 2, Solaris 7 (11/99)
> > Perl: 5.6.0
> >
> > I have a series of related programs that need global definitions ($DOMAIN,
> > $ADMIN, $DBNAME, etc).  My code looks something like this:
> >
> > --
> > #!/usr/local/bin/perl -w
> >
> > use strict;
> > use DBI;
> >
> > require "defs.pl";
> >
> > print "Welcome to $DOMAIN, $ADMIN\n";
> >
> >
> > 
> > #!/usr/local/bin/perl -w
> >
> > use strict;
> >
> > my $DOMAIN="plexus.com";
> > my $ADMIN="Chuck";
> >
> > 
> >
> > Of course, when I run g.pl I get the infamous,
> >
> > Global symbol "$DOMAIN" requires explicit package name at ./g.pl line 7.
> > Global symbol "$ADMIN" requires explicit package name at ./g.pl line 7.
> > Execution of ./g.pl aborted due to compilation errors.
> >
> > I've tried "my" and "local", but local just changes the error to complain
> > about an explicit package being required.  I'm missing something basic.
> >
> > How can I define these variables to be known in g.pl, h.pl, and other
> > programs that require defs.pl?
> >
> > --
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> 
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Defining Global Variables w/use strict;

2001-11-08 Thread Jos I. Boumans

what you probably want is:

use vars qw|$foo @bar|;

but ONLY use that if you're REALLY sure what you're doing
globals are usually a bad idea.

hth
Jos

- Original Message -
From: "Tomasi, Chuck" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, November 08, 2001 4:45 PM
Subject: Defining Global Variables w/use strict;


> System: Sun Ultra 2, Solaris 7 (11/99)
> Perl: 5.6.0
>
> I have a series of related programs that need global definitions ($DOMAIN,
> $ADMIN, $DBNAME, etc).  My code looks something like this:
>
> --
> #!/usr/local/bin/perl -w
>
> use strict;
> use DBI;
>
> require "defs.pl";
>
> print "Welcome to $DOMAIN, $ADMIN\n";
>
>
> 
> #!/usr/local/bin/perl -w
>
> use strict;
>
> my $DOMAIN="plexus.com";
> my $ADMIN="Chuck";
>
> 
>
> Of course, when I run g.pl I get the infamous,
>
> Global symbol "$DOMAIN" requires explicit package name at ./g.pl line 7.
> Global symbol "$ADMIN" requires explicit package name at ./g.pl line 7.
> Execution of ./g.pl aborted due to compilation errors.
>
> I've tried "my" and "local", but local just changes the error to complain
> about an explicit package being required.  I'm missing something basic.
>
> How can I define these variables to be known in g.pl, h.pl, and other
> programs that require defs.pl?
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Defining Global Variables w/use strict;

2001-11-08 Thread Tomasi, Chuck

That's the idea of "defs.pl".  It is supposed to define all the globals
you'll need.   Much like a #include in C.  I've done it before in Perl, but
I wasn't using -w/use strict.  I'm forcing myself to do things the right
way, but keep running in to fundamentals I should have learned years ago.

Thanks.

> -Original Message-
> From: dan radom [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, November 08, 2001 9:50 AM
> To: '[EMAIL PROTECTED]'
> Subject: Re: Defining Global Variables w/use strict;
> 
> 
> just a thought, but how can you print a variable that hasn't 
> been defined yet?
> 
> dan
> 
> * Tomasi, Chuck ([EMAIL PROTECTED]) wrote:
> > System: Sun Ultra 2, Solaris 7 (11/99)
> > Perl: 5.6.0
> > 
> > I have a series of related programs that need global 
> definitions ($DOMAIN,
> > $ADMIN, $DBNAME, etc).  My code looks something like this:
> > 
> > --
> > #!/usr/local/bin/perl -w
> > 
> > use strict;
> > use DBI;
> > 
> > require "defs.pl";
> > 
> > print "Welcome to $DOMAIN, $ADMIN\n";
> > 
> > 
> > 
> > #!/usr/local/bin/perl -w
> > 
> > use strict; 
> > 
> > my $DOMAIN="plexus.com";
> > my $ADMIN="Chuck";
> > 
> > 
> > 
> > Of course, when I run g.pl I get the infamous,
> > 
> > Global symbol "$DOMAIN" requires explicit package name at 
> ./g.pl line 7.
> > Global symbol "$ADMIN" requires explicit package name at 
> ./g.pl line 7. 
> > Execution of ./g.pl aborted due to compilation errors.
> > 
> > I've tried "my" and "local", but local just changes the 
> error to complain
> > about an explicit package being required.  I'm missing 
> something basic.
> > 
> > How can I define these variables to be known in g.pl, h.pl, 
> and other
> > programs that require defs.pl?
> > 
> > -- 
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Defining Global Variables w/use strict;

2001-11-08 Thread Jeff 'japhy' Pinyan

On Nov 8, Tomasi, Chuck said:

>I have a series of related programs that need global definitions ($DOMAIN,
>$ADMIN, $DBNAME, etc).  My code looks something like this:

Global variables aren't declared with my().  It sounds like you want to
use the Exporter module.

Your base program remains the same:

  use DBI;
  use strict;
  require "defs.pl";

  print "Welcome to $DOMAIN, $ADMIN\n";

But defs.pl changes a bit:

  package Defaults;
  require Exporter;
  @ISA = 'Exporter';
  @EXPORT = qw( $DOMAIN $ADMIN );

  $DOMAIN = "...";
  $ADMIN = "...";

  Defaults->import;

  1;

That works.  However, you might want to take the full-fledged module
approach.  Your main program will then do

  use Defaults;

instead of

  require "defs.pl";

And your defs.pl file should be renamed Defaults.pm.  The contents will be
almost exactly the same, except you should remove the call to
Defaults->import.

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Defining Global Variables w/use strict;

2001-11-08 Thread Peter Scott

At 11:06 AM 11/8/01 -0500, Jeff 'japhy' Pinyan wrote:
>On Nov 8, Tomasi, Chuck said:
>
> >I have a series of related programs that need global definitions ($DOMAIN,
> >$ADMIN, $DBNAME, etc).  My code looks something like this:
>
>Global variables aren't declared with my().  It sounds like you want to
>use the Exporter module.
>
>Your base program remains the same:
>
>   use DBI;
>   use strict;
>   require "defs.pl";
>
>   print "Welcome to $DOMAIN, $ADMIN\n";

Global symbol $DOMAIN requires explicit package name...

>But defs.pl changes a bit:
>
>   package Defaults;
>   require Exporter;
>   @ISA = 'Exporter';
>   @EXPORT = qw( $DOMAIN $ADMIN );
>
>   $DOMAIN = "...";
>   $ADMIN = "...";
>
>   Defaults->import;
>
>   1;

I don't think you want to call the import method from within the Defaults 
package.

>That works.  However, you might want to take the full-fledged module
>approach.  Your main program will then do
>
>   use Defaults;
>
>instead of
>
>   require "defs.pl";
>
>And your defs.pl file should be renamed Defaults.pm.  The contents will be
>almost exactly the same, except you should remove the call to
>Defaults->import.

Agreed, much simpler.

It's worth pointing out that strictness isn't being applied to Defaults.pm 
just because it is on in the main program.  It was a *long* time before I 
realized this.

If you enable it so you can strict-check your modules, then you have to do 
something about the variables you're exporting... IMHO the easiest way is 
to insert

 use vars qw(@ISA @EXPORT $DOMAIN $ADMIN);

sufficiently early.

--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Defining Global Variables w/use strict;

2001-11-08 Thread Jeff 'japhy' Pinyan

On Nov 8, Peter Scott said:

>>   use DBI;
>>   use strict;
>>   require "defs.pl";
>>
>>   print "Welcome to $DOMAIN, $ADMIN\n";
>
>Global symbol $DOMAIN requires explicit package name...

Oops, that require() should have been inside a BEGIN { } block.

>>   package Defaults;
>>   require Exporter;
>>   @ISA = 'Exporter';
>>   @EXPORT = qw( $DOMAIN $ADMIN );
>>
>>   $DOMAIN = "...";
>>   $ADMIN = "...";
>>
>>   Defaults->import;
>>
>>   1;
>
>I don't think you want to call the import method from within the Defaults 
>package.

Hrm.  But it worked when I tried it.  No, it doesn't.  Hrm.  Oh well.

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Defining Global Variables w/use strict;

2001-11-08 Thread Jeff 'japhy' Pinyan

On Nov 8, Arul, Rex said:

>Import is redundant because, whenever you say, "use Defaults" it is akin
>to a directive.

Yes,

  use Module;

becomes

  BEGIN {
require Module;
Module->import;
  }

but we weren't use()ing the file.  The bigger problem is that I called the
import method from the wrong package.

I should have used

  Defaults->export_to_level(1, @EXPORT);

This method is documented in the Exporter docs.

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




How do I properly use global variables?

2007-10-23 Thread monk
I'm having problems accessing a variable outside its subroutine.
I've tried several combinations too long to write here. Maybe I just
can't see the forest for the trees.  But I'm lost.  I need your
wisdom.

I'd like my program below to change $status to zero to exit the loop.

meaning...$> perl test.pl --start
it prints out indefinitely "hello world"

But if  $> perl test.pl --stop
it gets out of the loop exiting the program.



#!/usr/bin/perl
use strict;
use warnings;



our $status;

sub start{
$status = 1;
while ($status == 1){
  print "hello world!\n";
}
print "out of loop. Will exit now";
exit;
}


sub stop {
 $status = 0;

}


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




How bad is it to use global variables?

2003-01-23 Thread Nils-Anders Persson
Hello,

I've seen many discussions regarding the use of "use strict".

What i wonder is why is it so bad using global variables?

And another question how do you define constants in perl?

regards,
Nils-Anders



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




[Fwd: Re: Defining Global Variables w/use strict;]

2001-11-08 Thread Arul, Rex

Jeff ,

Import is redundant because, whenever you say, "use Defaults" it is akin 
to a directive.

So what it does implicitly is to invoke the "import" method of the 
module named "Defaults" .

So when you say, "use strict;" or "use warnings;" , implicitly the 
"import" method of those modules are invoked transparently. I did verify 
this by opening the strict.pm and warnings.pm and both of them had the 
"import" function.

Please correct me, if I am wrong.

Thanks,
Rex

>.
>
>>>  package Defaults;
>>>  require Exporter;
>>>  @ISA = 'Exporter';
>>>  @EXPORT = qw( $DOMAIN $ADMIN );
>>>
>>>  $DOMAIN = "...";
>>>  $ADMIN = "...";
>>>
>>>  Defaults->import;
>>>
>>>  1;
>>>
>>I don't think you want to call the import method from within the Defaults 
>>package.
>>
>
>Hrm.  But it worked when I tried it.  No, it doesn't.  Hrm.  Oh well.
>





-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: How do I properly use global variables?

2007-10-23 Thread Rob Coops
 You might want to try the following call your script twice with the start
argument, then look at the processes running (just send the output of the
script to /dev/null or something like that for ease of use)

You will see the script running twice, this is because you started the Perl
interpreter twice. If you want to be able to do this you will have to look
into something called signaling. With this you can send your script a signal
to stop, this signal you can then process in anyway you like.

I hope this will help yo a bit, regards,

Rob



On 10/23/07, monk <[EMAIL PROTECTED]> wrote:
>
> I'm having problems accessing a variable outside its subroutine.
> I've tried several combinations too long to write here. Maybe I just
> can't see the forest for the trees.  But I'm lost.  I need your
> wisdom.
>
> I'd like my program below to change $status to zero to exit the loop.
>
> meaning...$> perl test.pl --start
> it prints out indefinitely "hello world"
>
> But if  $> perl test.pl --stop
> it gets out of the loop exiting the program.
>
>
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> 
>
> our $status;
>
> sub start{
>$status = 1;
>while ($status == 1){
>  print "hello world!\n";
>}
> print "out of loop. Will exit now";
> exit;
> }
>
>
> sub stop {
> $status = 0;
>
> }
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> http://learn.perl.org/
>
>
>


Re: How do I properly use global variables?

2007-10-23 Thread Jeff Pang
On 10/23/07, monk <[EMAIL PROTECTED]> wrote:
> I'm having problems accessing a variable outside its subroutine.
> I've tried several combinations too long to write here. Maybe I just
> can't see the forest for the trees.  But I'm lost.  I need your
> wisdom.
>
> I'd like my program below to change $status to zero to exit the loop.
>
> meaning...$> perl test.pl --start
> it prints out indefinitely "hello world"
>
> But if  $> perl test.pl --stop
> it gets out of the loop exiting the program.
>
>

Hi,

When script is running,how can you re-run it with another argument to
make it stop?
The general way to let a running program stop is to send a signal.
Let me modify your code to,

use strict;
use warnings;

our $status = 1;
$SIG{TERM} = $SIG{INT} = sub {$status = 0};

start();

sub start {
   while ($status){
 print "hello world!\n";
 sleep 1;
   }

   print "out of loop. Will exit now\n";
   exit 0;
}

__END__


When you run it,you can send SIGINT or SIGTERM to let it exit gracefully.
Given the process id is 1234,under unix you can say,

$ kill -s 2  1234

The script would print "out of loop. Will exit now" and exit.
(-s 2 means sending SIGINT,see `man 7 signal` for details).

The most important change for the code above is that we re-defined
singal handlers:
$SIG{TERM} = $SIG{INT} = sub {$status = 0};

When the script receive SIGTERM or SIGINT,it set the global $status to
0,so the loop condition become false,the program exit.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: How do I properly use global variables?

2007-10-23 Thread Matthew Whipple
Jeff Pang wrote:
> On 10/23/07, monk <[EMAIL PROTECTED]> wrote:
>   
>> I'm having problems accessing a variable outside its subroutine.
>> I've tried several combinations too long to write here. Maybe I just
>> can't see the forest for the trees.  But I'm lost.  I need your
>> wisdom.
>>
>> I'd like my program below to change $status to zero to exit the loop.
>>
>> meaning...$> perl test.pl --start
>> it prints out indefinitely "hello world"
>>
>> But if  $> perl test.pl --stop
>> it gets out of the loop exiting the program.
>>
>>
>> 
And if you want to keep the notation similar to that above you'd need
the second instance of the script (with the --stop), to send the signal
to the process of the first instance (generally by storing the pid in a
file and then sending the signal as below if the pid is still running).
>
> Hi,
>
> When script is running,how can you re-run it with another argument to
> make it stop?
> The general way to let a running program stop is to send a signal.
> Let me modify your code to,
>
> use strict;
> use warnings;
>
> our $status = 1;
> $SIG{TERM} = $SIG{INT} = sub {$status = 0};
>
> start();
>
> sub start {
>while ($status){
>  print "hello world!\n";
>  sleep 1;
>}
>
>print "out of loop. Will exit now\n";
>exit 0;
> }
>
> __END__
>
>
> When you run it,you can send SIGINT or SIGTERM to let it exit gracefully.
> Given the process id is 1234,under unix you can say,
>
> $ kill -s 2  1234
>
> The script would print "out of loop. Will exit now" and exit.
> (-s 2 means sending SIGINT,see `man 7 signal` for details).
>
> The most important change for the code above is that we re-defined
> singal handlers:
> $SIG{TERM} = $SIG{INT} = sub {$status = 0};
>
> When the script receive SIGTERM or SIGINT,it set the global $status to
> 0,so the loop condition become false,the program exit.
>
>   


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




RE: How bad is it to use global variables?

2003-01-23 Thread Kipp, James
> 
> I've seen many discussions regarding the use of "use strict".
> 
> What i wonder is why is it so bad using global variables?
> 
> And another question how do you define constants in perl?
> 

use strict does not disallow global variables, it simply enforced you to
declare variables with one of my,local, our. there is a article called
"coping with scoping" you might want to read. I don't have the URl handy,
but a google search will find it.

for constants there are a few ways, one is using the constant pragma:
use constant BUFFER_SIZE=> 4096;


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: How bad is it to use global variables?

2003-01-23 Thread wiggins


On Thu, 23 Jan 2003 14:00:19 +0100, "Nils-Anders Persson" 
<[EMAIL PROTECTED]> wrote:

> Hello,
> 
> I've seen many discussions regarding the use of "use strict".

Probably shouldn't be any discussion. Just 'use' it. :-)

> 
> What i wonder is why is it so bad using global variables?
> 

Memory usage, modularity, debugging, are three good reasons.

> And another question how do you define constants in perl?
> 

use constant CONSTANT_NAME => 5;

perldoc constant (for the details).

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: How bad is it to use global variables?

2003-01-23 Thread wiggins


On Thu, 23 Jan 2003 08:52:01 -0500, "Kipp, James" <[EMAIL PROTECTED]> wrote:

 there is a article called
> "coping with scoping" you might want to read. I don't have the URl handy,
> but a google search will find it.
> 

I keep it handy:  http://perl.plover.com/FAQs/Namespaces.html

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: How bad is it to use global variables?

2003-01-23 Thread Rob Dixon
Nils-Anders Persson wrote:
> Hello,
>
> I've seen many discussions regarding the use of "use strict".
>
> What i wonder is why is it so bad using global variables?
>

It's not. It's bad to use global variables /all the time/, even if
you don't need them. Most variables are only in use for short
sections of code, and using the same global variable everywhere
you have, say,

for ($i = 0; $i < $len; ++$i) {
:
}

will get very confusing and lead to awkward-to-find bugs.

>
> And another question how do you define constants in perl?

use constant MEANING_OF_LIFE => 42;

HTH,

Rob






-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




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

2004-10-08 Thread Adam
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



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Global variables / special variables / variable identifier with just one character

2004-10-08 Thread Steve Bertrand
> 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?

$a and $b have special meanings in Perl.

Aside from that, try defining $b with my:

my ($b) = "";

before using it, and the error *should* dissapear.

Steve

> thanks
> adam
>
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>  
>
>
>



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Global variables / special variables / variable identifier with just one character

2004-10-08 Thread Bee
"Adam" <[EMAIL PROTECTED]> 在郵件 news:[EMAIL PROTECTED] 中撰寫...
> 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
> 

It runs fine from my PC, but $a and $b are normally using for sorting array
more then just lexcal sorting.

@sorted = sort { $a <=> $b} @unsorted ; 

perldoc -f sort

HTH,
Bee



HOWTO:Dynamically getting all of the global variables and their v alues

2003-07-01 Thread Ken Lehman
I would like to write a function that will get all the global variables that
are declared in my script plus their values( which will all be strings if
that matters). This is what I have so far:

my ( $varName, $globValue );

while ( ($varName, $globValue) = each %main:: )
{
  print "\$$varName $globValue \n";
}


This prints a whole lot of stuff including function names but the variables
are not in here. My goal is to have this function pick up the variables
dynamically everytime the script is run, get the values and do some
varification before getting into the meat of the script, without having to
add or remove the variables manually from the sub whenever I add or remove a
variable from the script. 
Thanks in advance for any help
-Ken



The views and opinions expressed in this email message are the sender's
own, and do not necessarily represent the views and opinions of Summit
Systems Inc.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: HOWTO:Dynamically getting all of the global variables and their v alues

2003-07-01 Thread Wagner, David --- Senior Programmer Analyst --- WGO
Ken Lehman wrote:
> I would like to write a function that will get all the global
> variables that are declared in my script plus their values( which
> will all be strings if that matters). This is what I have so far:
> 

Why use global variables when you could use a hash which gives you a key plus 
whatever type of data needed.  I do this all the time. Allows me to put everyhting in 
one spot and I use this vairable hash for holding all the necessary data that I need 
to have.

A thought.

Wags ;) 


**
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: HOWTO:Dynamically getting all of the global variables and their v alues

2003-07-01 Thread Charles K. Clarkson
Ken Lehman <[EMAIL PROTECTED]> wrote:
: 
: I would like to write a function that will get
: all the global variables that are declared in
: my script plus their values( which will all 
: be strings if that matters). This is what I
: have so far:
: 
: my ( $varName, $globValue );
: 
: while ( ($varName, $globValue) = each %main:: )
: {
:   print "\$$varName $globValue \n";
: }
: 
: 
: This prints a whole lot of stuff including
: function names but the variables are not in
: here.

Then the variable you are referring to
are probably lexical (declared with 'my').
Lexical variables do not show up in the
symbol table (%main::).

: My goal is to have this function pick
: up the variables dynamically every time
: the script is run, get the values and
: do some verification before getting
: into the meat of the script, without
: having to add or remove the variables
: manually from the sub whenever I add
: or remove a variable from the script. 
: Thanks in advance for any help

Another poster mentioned the idea
of creating a module full of constants.
This allowed him to use any data type
and to use subroutines for very complex
data. All the constants could be
exported when the module is "use"d.

This still imports everything to the
"main" namespace and since constants are
traditional ALLCAPS, provides the next
programmer to easily recognize that they
may indeed be constants imported into
the script.


HTH,

Charles K. Clarkson
-- 
Head Bottle Washer,
Clarkson Energy Homes, Inc.
Mobile Home Specialists
254 968-8328






-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]