Jonathan Mangin wrote:
>
> Jeez, this is a beginner's list? Y'all are defining my
> ignorance of this here purrl stuff. I hope someone with
> patience can help me out. In a simple login/registration
> program using 5.6.1...
>
> #!/usr/bin/perl -wT
> use strict;
>
> [CGI and DBI stuff]
>
> $countryid="1";
> $statename=$gotcha;
> @r_states=getStates([EMAIL PROTECTED], \%states1, \%states2);
> $stateid=$states2{$statename};
> @r_counties=getCounties([EMAIL PROTECTED], \%counties1);
>
> if ($CGI->param("Register")) {
>    Register($CGI, \%$counties1);
> }
> elsif ($CGI->param("Verify")) {
>    Verify($CGI);
> }
> else {
>    displayLoginScreen($CGI);
> }
> sub displayLoginScreen {
>    [misc stuff]
>
>    foreach $state(@states) {    # I already have the state, of
course
>       print "<option>$state";   # Still here for
historical/testing
>    }
>
>    [more stuff]
> }
>
> Through the scope(?) of displayLoginScreen (main?) all of the
> state/county arrays/hashes are available, though note the
> @states syntax instead of @$states. (Why is this syntax
> working?) The states data is more persistent (I'll later
> figure out why if ever a pattern emerges), but Verify knows
> the county stuff only until the first validation error.

All of the variables that you've declared ouside any subroutines
are accessible throughout the rest of the file, both within and
outside subroutines. You're simply accessing the common
'@states' variable in 'displayLoginScreen'. '@states' and
'$states' are totally unrealted: they just happen to have the
same identifier. The syntax '@$states' is an attempt to
dereference the value in the '$states' scalar as an array. This
will fail unless '$states' holds an array reference. I don't
understand what you mean by "more persistent" or by 'Verify'
knows the county stuff only until the first validation error."
You're passing no references to 'Verify' so it can only be
accessing the common variables like '@states'.

> Instead of making subsequent db fetches I want to send(?)
> any/all of these to any/all subsequent (inner?) subroutines
> that may need them. Is this possible and what might the
> referencing/dereferencing look like?

You can either do what you've done above, and simply declare
common variables at a wider scope so that they're accessible
everywhere or - more properly - pass references to all of the
data as as parameters. You'd then have something like

  displayLoginScreen($CGI, [EMAIL PROTECTED]);

  sub displayLoginScreen {
    my ($CGI, $states) = @_;
    foreach my $state (@$states) {
      :
    }
  }

> Verify calls other subroutines and then another HTML page.
> That page calls Register. Do I need to send references to
> Verify before sending them to the other subs?

If you're expecting the call to the other subroutines to pass
all of the data they need, then clearly the calling code has to
have access to that data to be able to pass it on. This is where
parameterising all data can get messy, and it becomes more
appropriate to use common data accessible everywhere, preferably
with some naming convention that makes it clear that it is
global data.

> You can see what I'm trying with Register. After many fanciful
> iterations of code I'm still clueless. My wordiness is to
> encourage you to the same. Thanks to all.

You've called

  Register($CGI, \%$counties1)

which first of all dereferences '$counties1' (which, remember is
totally independent of '%counties1') as a hash. It then takes a
reference to that hash so the resulting value should be the same
as '$counties1' (if that variable held a hash reference in the
first place). Without seeing what '%counties1' might be I can
only guess that your call should be

  Register($CGI, \%counties1)

but you seemed to know what to do in the previous call to
'getStates', like this.

  getStates([EMAIL PROTECTED], \%states1, \%states2)

I hope that helps.

Rob




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

Reply via email to