Re: Scope of my() declared variables

2002-04-17 Thread A. Arnstein








At 12:47 PM 4/17/2002 -0700, you wrote:

>On Wednesday, April 10, 2002, at 04:42 , Elaine -HFB- Ashton wrote:
   ^
is this supposed to be funny?


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




Re: Scope of my() declared variables

2002-04-17 Thread drieux


On Wednesday, April 10, 2002, at 04:42 , Elaine -HFB- Ashton wrote:

> I always found the local, my, our mess pretty confusing and the best
> explanation is MJD's "Coping with Scoping"
>
> http://perl.plover.com/FAQs/Namespaces.html
>
> Make good note of the text in red :)

ok, I get the following error message when I use 'local' rather
than a my - in a way old piece of code I found along the way:

vladimir: 66:] perl old_get_ip
Global symbol "$hostname" requires explicit package name at old_get_ip 
line 8.
Global symbol "$hostname" requires explicit package name at old_get_ip 
line 10.
Execution of old_get_ip aborted due to compilation errors.
vladimir: 67:]

  see below

I just had the News Flash Coffee Wake Up Moment..

It is whining that in the function it is expecting to use a
local copy of a globally declared variable - which if I try
with the

our $hostname = '';

will work - but not if I did that with the

my $hostname = '';

which hurls furballs with:

vladimir: 75:] perl old_get_ip wetware
Can't localize lexical variable $hostname at old_get_ip line 10.
vladimir: 76:]

So that is how that works

ciao
drieux

---

WARNING: do Not Code Like This: The following is BAD CODE!

### #!/usr/local/bin/perl -w
### use strict;
### #
### # a dumb perl implementation to get an ip_addr for a hostname
### #
our $hostname = '';
### sub do_gethost{
###
### local($hostname) = pop(@_);
###
### my ( $name, $aliases, $addrtype, $length, 
@addr)=gethostbyname($hostname);
###
### if ( defined($name) )
### {
### my ($a, $b, $c, $d) = unpack('C4', $addr[0]);
###
### printf("%s.%s.%s.%s\n", $a, $b, $c, $d);
### }
###
### }
###
### while(my $name = shift(@ARGV) ) {
###
### &do_gethost($name);
###
### }
###
### exit(0);
###

that it was in the production release of stuff to provide a
work around because the IT staff was never sure if they were
putting stuff into /etc/hosts or through NIS+ and hence or
and the geeks who whacked in the /bin/sh scripting - 'because we
do not want to learn perl' - had way ancien ugly sed wrappers
on nslookup calls - still remains no good excuse for BAD perl Code.

WARNING: if you code like that after reading stuff from
the perl list - do not blame me because you did not read this
Warning Message and heed it.

did I mention that this is BAD PERL CODE!


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




Re: Scope of my() declared variables WAS( Re: -e $filename...)

2002-04-14 Thread Jenda Krynicky

From: Ahmed Moustafa <[EMAIL PROTECTED]>
> Elaine -Hfb- Ashton wrote:
> > I always found the local, my, our mess pretty confusing and the best
> > explanation is MJD's "Coping with Scoping"
> > 
> > http://perl.plover.com/FAQs/Namespaces.html
> > 
> > Make good note of the text in red :)
> 
> Elaine, thanks a lot for MJD's article. There is a great difference
> before reading it and after :)
> 
> In the example:
> 
> 
> { my $seed = 1;
> sub my_rand {
> $seed = int(($seed * 1103515245 + 12345) / 65536) % 32768;
> return $seed;
> }
> }
> 
> 
> , the function my_rand () is declared inside a block. Does that mean a
> function can be declared anywhere in the program (i.e. inside another
> function, loop or if block)?

Yes. But you should be carefull with this!

For example if you write this:

for (1..10) {
my $x = $_;
sub foo {
print $x,"\n";
}
foo;
}
foo;

Or

sub bar {
my $x = shift;
sub foo {
print "$x\n";
}
}
bar 1;
foo;
bar 2;
foo;

what would you think you will get? Try it and play with it some 
more!

As you can see the function keeps a reference to the very first 
instance of $x. The $x variable doesn't stay shared between foo() 
and bar().

This means that you should NOT create named subroutines in any 
loops or bodies of other functions.

Unnamed functions are different beasts though.

sub MakeFoo {
my $x = shift;
return sub {print $x,"\n"}
}

$foo1 = MakeFoo 1;
$foo2 = MakeFoo 2;
$foo1->();
$foo2->();

In this case you are creating a new "function" each time. 
(Actually what you create in this case is called closure.)

Jenda

=== [EMAIL PROTECTED] == http://Jenda.Krynicky.cz ==
There is a reason for living. There must be. I've seen it somewhere.
It's just that in the mess on my table ... and in my brain
I can't find it.
--- me

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




Re: Scope of my() declared variables WAS( Re: -e $filename...)

2002-04-13 Thread drieux


On Saturday, April 13, 2002, at 04:45 , Ahmed Moustafa wrote:

> Elaine -Hfb- Ashton wrote:
>> I always found the local, my, our mess pretty confusing and the best
>> explanation is MJD's "Coping with Scoping"
>> http://perl.plover.com/FAQs/Namespaces.html
>> Make good note of the text in red :)

[..]
> , the function my_rand () is declared inside a block. Does that mean a 
> function can be declared anywhere in the program (i.e. inside another 
> function, loop or if block)?

the handy dandy autonomous 'function' comes to mind

the problem of course is that you want to avoid getting way
esoteric - since that will need more documentation - and can
make the code maintenance harder.

A reasonable rule of thumb that I offer is the old school
pascal trick -

#!/usr/bin/perl -w
use strict;


use vars qw//; #so we remember what was global
use subs qw//;

#---
# list out our globals here - initialize them here.

#-
# our functions


#-
# the main loop

this way your sub's are declared before they are used in the main loop
and hopefully they do not need get interwoven too badly. This way
when You notice that only the main changes - it's clearly obvious
that you should just Whip Out the h2xs - make a module of it all -
and shorten up your 'scripts'





ciao
drieux

---


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




Re: Scope of my() declared variables WAS( Re: -e $filename...)

2002-04-13 Thread Ahmed Moustafa

Elaine -Hfb- Ashton wrote:
> I always found the local, my, our mess pretty confusing and the best
> explanation is MJD's "Coping with Scoping"
> 
> http://perl.plover.com/FAQs/Namespaces.html
> 
> Make good note of the text in red :)

Elaine, thanks a lot for MJD's article. There is a great difference 
before reading it and after :)

In the example:


{ my $seed = 1;
sub my_rand {
$seed = int(($seed * 1103515245 + 12345) / 65536) % 32768;
return $seed;
}
}


, the function my_rand () is declared inside a block. Does that mean a 
function can be declared anywhere in the program (i.e. inside another 
function, loop or if block)?


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




Re: Scope of my() declared variables WAS( Re: -e $filename...)

2002-04-10 Thread Elaine -HFB- Ashton

Timothy Johnson [[EMAIL PROTECTED]] quoth:
*>
*>Here's the part I still don't understand, and maybe some of you can show me
*>the light.  What is the difference between local() and my()?  I have never
*>used local(), the only examples I've ever been given involve scoping $_, and
*>if I am ever tempted to do that, I can usually trace it back to a bad
*>algorithm.

I always found the local, my, our mess pretty confusing and the best
explanation is MJD's "Coping with Scoping"

http://perl.plover.com/FAQs/Namespaces.html

Make good note of the text in red :)

e.

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




RE: Scope of my() declared variables WAS( Re: -e $filename...)

2002-04-10 Thread David Gray

> Here's the part I still don't understand, and maybe some of 
> you can show me the light.  What is the difference between 
> local() and my()?  I have never used local(), the only 
> examples I've ever been given involve scoping $_, and if I am 
> ever tempted to do that, I can usually trace it back to a bad 
> algorithm.

perldoc -q lexical

 -dave



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




Scope of my() declared variables WAS( Re: -e $filename...)

2002-04-10 Thread Timothy Johnson


Here's the part I still don't understand, and maybe some of you can show me
the light.  What is the difference between local() and my()?  I have never
used local(), the only examples I've ever been given involve scoping $_, and
if I am ever tempted to do that, I can usually trace it back to a bad
algorithm.

-Original Message-
From: Jenda Krynicky [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, April 10, 2002 11:31 AM
To: [EMAIL PROTECTED]
Subject: Re: (-e $filename) gives Use of uninitialized value at...


From:"Ahmed Moustafa" <[EMAIL PROTECTED]>

> What is the default scope for a variable defined inside a subroutine?

The scope os a lexical variable is always to the end of the 
enclosing block or eval()ed string or the file.

The body of a subroutine is just a block as far as scoping is 
concerned.

Jenda

P.S.: Please do not quote the whole history of the thread if you 
only need to ask a simple question.

=== [EMAIL PROTECTED] == http://Jenda.Krynicky.cz ==
There is a reason for living. There must be. I've seen it somewhere.
It's just that in the mess on my table ... and in my brain
I can't find it.
--- me

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



This email may contain confidential and privileged 
material for the sole use of the intended recipient. 
If you are not the intended recipient, please contact 
the sender and delete all copies.

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