> -----Original Message-----
> From: Lysander [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, January 24, 2002 4:06 PM
> To: Bob Showalter; [EMAIL PROTECTED]
> Subject: Re: Trying to use strict
> 
> 
> ----- Original Message -----
> From: "Bob Showalter" <[EMAIL PROTECTED]>
> To: "'Lysander'" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
> Sent: Thursday, January 24, 2002 2:48 PM
> Subject: RE: Trying to use strict
> 
> 
> > > -----Original Message-----
> > > From: Lysander [mailto:[EMAIL PROTECTED]]
> > > Sent: Thursday, January 24, 2002 3:23 PM
> > > To: [EMAIL PROTECTED]
> > > Subject: Trying to use strict
> > >
> > >
> > > Okay, after reading various documents online I have decided
> > > to try and write my code using -w and strict.
> >
> > Excellent!
> >
> > >
> > > So here is the problem.  How do I pass Arrays to subs?  I
> > > assume declaring everything main:: would pretty much
> > > undermine the whole point of strict vars.  So... for example
> > > I have this sub which sends me mail if there is an error.
> > >
> > > ...snip a huge bunch of stuff...
> >
> > 'use strict' has nothing to do with passing stuff to subs. It is
> > a compile-time pragma that catches common errors due to misspelled
> > variable names, use of symbolic refs, etc.
> >
> > When you add 'use strict' to an existing script, you need to declare
> > the variables used, typically by using 'my' (but sometimes 
> by 'use vars'
> > or 'our').
> >
> > So, if you use an array @headers in your script, you simply 
> need to add
> the
> > line
> >
> >    my @headers;
> >
> > to the top of your script. Likewise with other variables like this.
> > That's all you need to do.
> >
> > Now, if you misspell it somewhere in your script as @heders, or use
> > $headers{0} when you meant $headers[0], you'll get a nice 
> compile-time
> > error message instead of having to track down why your program isn't
> > behaving properly.
> >
> > HTH
> 
> I had figured that out on my own.  The problem I was trying 
> to convey is
> that prior to using strict I had to Arrays, @headers and 
> @body.  In several
> subs I access the information in these arrays.  Now when I 
> declare these
> arrays with my they are no longer accessable to my subs.
> 
> I realize that I could declare them @main::headers and @main::body but
> thought that since one of the things strict does is help to 
> avoid global
> variables that I might be better of passing the values to the 
> subs instead
> of making them global.

Putting the "my" declaration at the top of the _script_, not in
the sub, will make 'use strict' happy. It's still a "global" variable
though (actually a file-scoped lexical).

If you want to avoid using globals, that's another matter.

> 
> If this is not correct, then I will just use @main::

No, don't use @main::foo, put a 'my @foo' declaration at the
top of the script.

> If it is, however, then I need some advice on how to get a 
> string and two
> Arrays passed to a sub.

When you pass multiple arguments to a sub, they are "flattened" 
into a single list (@_ inside the sub). You can't tell where one
array ends and the next begins. The solution is to pass references:

   foo($scalar, \@arr1, \@arr2);

   sub foo
   {
      my ($scalar, $arr1, $arr2) = @_;

      # print the first array
      print "$_\n" for @$arr1;
   }

Personally, I don't think its worth the trouble to avoid the globals
if your script is working fine.

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

Reply via email to