Another thing to remember is that declaring a variable with my() at the top
of your script does NOT make the variable global.  It loses scope in
subroutines.  The easiest way to get around this is to pass variables to
your subs by reference.  Consider the example below, which passes a
reference to $var instead of the value of $var.  If you just pass the value,
then any operations performed on your variable in the subroutine will be
destroyed when the sub exits.  This way you will be performing all
operations on the original variable, allowing you to change it as if it were
in scope.

my $var

PrintSub(\$var);  #@_ now contains (\$var)
print "\$var is now $var.\n";

sub PrintSub{
   my $subvar = ${$_[0]); #dereference the reference you passed.
   print "\$subvar is now $subvar.\n";
}

-----Original Message-----
From: zentara
To: [EMAIL PROTECTED]
Sent: 4/8/02 6:07 AM
Subject: Re: Scope of variables. Lost in subs

On Mon, 08 Apr 2002 11:00:54 +0200, [EMAIL PROTECTED] (Tor Hildrum)
wrote:

>On 8/4/02 9:15, "Tor Hildrum" <[EMAIL PROTECTED]> wrote:
>
>> Here are some of the error messages I get:
>> Use of uninitialized value in concatenation (.) at script.cgi line
55.
>> Use of uninitialized value in concatenation (.) at script.cgi line
55.
>> Can't open : No such file or directory
>
>Full source for the script can be located at:
>http://tortest.nuug.no/perl.txt
>
>The script dies on this line:
>open (REGISTRER, ">>$registrerte") or die();  ## $registrerte
null-value
>
>I don't understand why it is, because I have defined it at the start of
my
>script with my $registrerte = "";
>I thought that made it global?
>And, it is defined in the sub registrer().
>I have also tried setting it inside the sub without luck.
>
>Any advice would be much appreciated.
>I have re-read chapter 8 of Learning Perl, but I'm not getting any
wiser.

It's hard to say what the problem is, your multiple posts are kindof
confusing.
I tried to run your nuugmedlum.cgi, but of course I don't have the
files you are trying to open. When I substitute a text file to open,
the cgi program seems to run OK from the commandline.
What happens when you run the program from the commandline,
enter your name=value pairs, 1 pair per line, then hit control-d.
Turn on warnings too.

One thing to look at if your opens are failing is if you have "rights"
to do it.  Most of the time, a web server is running as
"nobody:nogroup";
not you.  So you have to make sure the directory you are opening
a file in is mode 777.

Since you are learning, this would be a good time to learn how to
use ptkdb, the perl-tk debugger. You will be able to step thru the
program
and see what is happening to each variable as you move from 1 sub
to another.



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

Reply via email to