Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Understanding Scope with 'my'

Reply
Thread Tools

Understanding Scope with 'my'

 
 
Roger
Guest
Posts: n/a
 
      05-17-2004

My code is this:

use warnings;
use strict;

my $number = my_rand();
print "The number is ".$number;
{ my $seed = 1;
sub my_rand (){

$seed = int(($seed * 1103515245 + 12345) / 65536) % 32768;
return $seed;
}

I derived it from an article here: (apparently wrong??)
http://perl.plover.com/FAQs/Namespac...xical_variable

The issue is that declaring 'seed' inside the brackets as shown above yields an
error of:
main::my_rand() called too early to check prototype at test_scope.pl line 6.
Use of uninitialized value in multiplication (*) at test_scope.pl line 11.

Basically I understand local, and am working on 'my' ... it would seem as if
using 'my' one needs to always use it in a sub in the context of returning it as
an lvalue...
like:
my $number = my_rand();
but like I said the code above does not work the way they:
http://perl.plover.com/FAQs/Namespac...xical_variable
said it should or I am missing the big picture. BTW before I posted I read
'Learning Perl' 3rd edition... and prehaps I *should* have 'got it' from that
the example URL I reference above has me scratching my head...

TIA


--
Use our news server 'news.foorum.com' from anywhere.
More details at: http://nnrpinfo.go.foorum.com/
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      05-18-2004

Quoth Roger <(E-Mail Removed)>:
>
> My code is this:
>
> use warnings;
> use strict;
>
> my $number = my_rand();
> print "The number is ".$number;
> { my $seed = 1;


Why the bizzare indentation?

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


You have a missing } here.

> I derived it from an article here: (apparently wrong??)
> http://perl.plover.com/FAQs/Namespac...xical_variable


No, this is a good basic description of how my and local work. The
problem is elsewhere.

> The issue is that declaring 'seed' inside the brackets as shown above yields
> an error of:
> main::my_rand() called too early to check prototype at test_scope.pl line 6.
> Use of uninitialized value in multiplication (*) at test_scope.pl line 11.


The problem is with these:

sub my_rand () {
^^

They are called a prototype, for which see perldoc perlsub. It will
suffice to say now that 1. they are an advanced feature of Perl and you
don't need to use them and 2. that perl needs to know a sub's prototype
*before* it encounters any calls to that sub (as the warning said).

The other warning comes from the fact that the assignment $seed = 1 is
not executed until after the call to my_rand. There are two fixes for
this: either move the whole block up above any calls to the sub, so that
the variable is initialised properly, or, better, make it into a BEGIN
block that will *definitely* be run before anything else. For BEGIN
blocks c.f. perldoc perlmod.

So, to fix your code you want

use warnings;
use strict;

# if you put
# use subs qw/my_rand/;
# here, you can call my_rand without parens

my $number = my_rand();
print "The number is $number\n";

BEGIN {
my $seed = 1;
sub my_rand { # Note: no ()
$seed = int (...);
return $seed;
}
}

Ben

--
Musica Dei donum optimi, trahit homines, trahit deos. |
Musica truces molit animos, tristesque mentes erigit. | http://www.velocityreviews.com/forums/(E-Mail Removed)
Musica vel ipsas arbores et horridas movet feras. |
 
Reply With Quote
 
 
 
 
Roger
Guest
Posts: n/a
 
      05-18-2004

"The other warning comes from the fact that the assignment $seed = 1 is
not executed until after the call to my_rand. There are two fixes for
this: either move the whole block up above any calls to the sub, so that
the variable is initialised properly, or, better, make it into a BEGIN
block that will *definitely* be run before anything else."

Yes that's right as it turns out. Just like a shell script if not for the BEGHIN
block. Thank you for pointing this out and I might say that URL I referenced
might be improved with this tidbit of info and the Llama book never explains
about begin blocks, although I'm sure it mentions the way the interpreter parses
a file... but then again the BEGIN block info is important to that.
Thanks again.
Roger

--
Use our news server 'news.foorum.com' from anywhere.
More details at: http://nnrpinfo.go.foorum.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having trouble understanding function scope and variable scope Andrew Falanga Javascript 2 11-22-2008 09:23 PM
understanding list scope Alex Python 5 09-22-2008 07:26 AM
I'm having trouble understanding scope of a variable in a subclass Pyenos Python 9 12-29-2006 01:14 AM
Understanding 'scope' Prabh Perl Misc 7 11-29-2005 09:05 AM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM



Advertisments