Re: use of require causing name space problems?
Ok, I entirely retract my post to which this is a reply. Hey, I've been up all night. If the scripts are running as separate processes, then, well, I don't know. > Does anyone know why perl behaves like this? [see earlier posts in thread]
RE: use of require causing name space problems?
Does anyone know why perl behaves like this? It seems to me that since the scripts are running as seperate processes, there should not be a problem. The only information Ive found on the subject is this: require - the file being required inserts the subroutine names into a package ( a namespace ) of its own choosing, not your package. Second, require happens at run-time, so the decleration occurs to late to serve as a declaration in the file invoking the require. use - performs a require at compile time, then lets you import declerations into your own namespace. Would it behoove me to modify my library to be a perl module and switch require to use? Would this solve my problem? thanks rodney > -Original Message- > From: Me [mailto:[EMAIL PROTECTED]] > Sent: Thursday, June 21, 2001 9:23 AM > To: Rodney Holm; [EMAIL PROTECTED] > Subject: Re: use of require causing name space problems? > > > > sub WriteFile > > { > > my ($file,@lines) = @_; > > my ($line); > > > > open(FILE,">$file"); > > FILE is a problem. > > If you are using perl 5.6 or later, you can do: > > my $fh; > > and then use $fh where you were using FILE, eg > > open($fh, ">$file"); > > If you aren't using 5.6, it gets more complicated > and I'm out of, er, my depth. I'll post later if I see > what to do, but I suggest you post again and ask. > > > foreach $line (@lines) > > $line is a problem. > > Fixing $line is easy. I'll let you guess. :> > Ok, no I won't: > > foreach my $line (@lines) > > > { > > print FILE $line; } > > } > > close(FILE); > > return(0); > > } > > open(FILE,">$file"); > >
Re: use of require causing name space problems?
> sub WriteFile > { > my ($file,@lines) = @_; > my ($line); > > open(FILE,">$file"); FILE is a problem. If you are using perl 5.6 or later, you can do: my $fh; and then use $fh where you were using FILE, eg open($fh, ">$file"); If you aren't using 5.6, it gets more complicated and I'm out of, er, my depth. I'll post later if I see what to do, but I suggest you post again and ask. > foreach $line (@lines) $line is a problem. Fixing $line is easy. I'll let you guess. :> Ok, no I won't: foreach my $line (@lines) > { > print FILE $line; } > } > close(FILE); > return(0); > } > open(FILE,">$file");
RE: use of require causing name space problems?
## somewhat simplified version of the WriteFile sub sub WriteFile { my ($file,@lines) = @_; my ($line); open(FILE,">$file"); foreach $line (@lines) { print FILE $line; } } close(FILE); return(0); } > -Original Message- > From: Me [mailto:[EMAIL PROTECTED]] > Sent: Thursday, June 21, 2001 8:45 AM > To: Rodney Holm; [EMAIL PROTECTED] > Subject: Re: use of require causing name space problems? > > > You need to make sure that variables in the called perl > code are unique to each instance of a call. (Sorry if that > was already obvious to you.) > > I suggest you post the WriteFile code and we go from there. > > > I have a few functions that are common to many different > > perl applications. All of these functions live in one > > file. > > > > I have many perl programs that run from cron that make > > use of these functions. So, in each of these programs > > I use require to gain access to these functions. > > > > Example: > > > > <<<<< doit.pl >>>>> > > #!/usr/bin/perl > > require '/usr/local/myperllib.pl'; > > @array = ("one\n", "two\n", "three\n"); > > ## calling WriteFile from myperllib.pl > > WriteFile('/path/filename', @array); > > exit; > > <<<<< doit.pl >>>>> > > > > <<<<< doit1.pl >>>>> > > require '/usr/local/myperllib.pl'; > > @array = ("four\n", "five\n", "six\n"); > > ## calling WriteFile from myperllib.pl > > WriteFile('/path/filename1', @array); > > exit; > > > > imagine many of these doit.pl scripts ( doit1.pl, doit2.pl ... ), all > using > > require, all calling WriteFile, all running at the same time. > > > > What happens is sometimes, what should end up in one file, ends up in > > another file. I would expect: > > $ cat /path/filename > > one > > two > > three > > > > what I sometimes get is the contents of /path/filename1 ending up in > > /path/filename > > $ cat /path/filename > > four > > five > > six > > > > How would I code my library correctly to avoid this type of namespace > > pollution? > > > > thanks > > rodney > >
Re: use of require causing name space problems?
You need to make sure that variables in the called perl code are unique to each instance of a call. (Sorry if that was already obvious to you.) I suggest you post the WriteFile code and we go from there. > I have a few functions that are common to many different > perl applications. All of these functions live in one > file. > > I have many perl programs that run from cron that make > use of these functions. So, in each of these programs > I use require to gain access to these functions. > > Example: > > < doit.pl > > #!/usr/bin/perl > require '/usr/local/myperllib.pl'; > @array = ("one\n", "two\n", "three\n"); > ## calling WriteFile from myperllib.pl > WriteFile('/path/filename', @array); > exit; > < doit.pl > > > < doit1.pl > > require '/usr/local/myperllib.pl'; > @array = ("four\n", "five\n", "six\n"); > ## calling WriteFile from myperllib.pl > WriteFile('/path/filename1', @array); > exit; > > imagine many of these doit.pl scripts ( doit1.pl, doit2.pl ... ), all using > require, all calling WriteFile, all running at the same time. > > What happens is sometimes, what should end up in one file, ends up in > another file. I would expect: > $ cat /path/filename > one > two > three > > what I sometimes get is the contents of /path/filename1 ending up in > /path/filename > $ cat /path/filename > four > five > six > > How would I code my library correctly to avoid this type of namespace > pollution? > > thanks > rodney
use of require causing name space problems?
I have a few functions that are common to many different perl applications. All of these functions live in one file. I have many perl programs that run from cron that make use of these functions. So, in each of these programs I use require to gain access to these functions. Example: < doit.pl > #!/usr/bin/perl require '/usr/local/myperllib.pl'; @array = ("one\n", "two\n", "three\n"); ## calling WriteFile from myperllib.pl WriteFile('/path/filename', @array); exit; < doit.pl > < doit1.pl > require '/usr/local/myperllib.pl'; @array = ("four\n", "five\n", "six\n"); ## calling WriteFile from myperllib.pl WriteFile('/path/filename1', @array); exit; imagine many of these doit.pl scripts ( doit1.pl, doit2.pl ... ), all using require, all calling WriteFile, all running at the same time. What happens is sometimes, what should end up in one file, ends up in another file. I would expect: $ cat /path/filename one two three what I sometimes get is the contents of /path/filename1 ending up in /path/filename $ cat /path/filename four five six How would I code my library correctly to avoid this type of namespace pollution? thanks rodney