Hi Lee, On Fri, 28 Jun 2013 15:15:27 +0200 lee <l...@yun.yagibdah.de> wrote:
> timothy adigun <2teezp...@gmail.com> writes: > > >>>> use strict; > >>>> use warnings; > >>>> > >>>> > >>>> sub test { > >>>> print $counter . "\n"; > >>>> } > >>>> > >>>> > >>>> my $counter = 0; > >>>> while($counter < 5) { > >>>> test(); > >>>> $counter++; > >>>> } > >>>> > >>>> > >>>> It says "Global symbol "$counter" requires explicit package name ...". > >>>> When I put the subroutine after the 'while' loop, it works just fine, so > >>>> what's the problem? > >>> > >>> The subroutine does not see the lexical $counter variable because it was > >>> declared after its scope. > >> > >> The subroutine is never called before $counter is declared, so it is > >> always available to the subroutine. There should be an error only for > >> instances when the subroutine is called before $counter is declared. > >> > >>> For more information, see: > >>> > >>> * http://www.plover.com/FAQs/Namespaces.html > >> > >> Ok, so perl has a totally broken design with variables :( What's the > > I don't think so. I think you are not really getting perl and Perl. > > How would you this in another language say C or C++? > > I would either declare global variables or, much better, make a data > structure a pointer to which can be handed over to functions easily and > efficiently. > > Declaring the variables on top didn't seem a good idea in perl, but > re-reading the documentation it seems that they might never be > accessible from outside the program when declared with 'my'. If that is > so, declaring them at the top wouldn't be different from declaring > global variables in C. > > >> solution to this problem? > > Below is one way of solving this problem: > > <CODE> > > use strict; > > use warnings; > > > > sub test { > > print shift(@_) . "\n"; > > } > > > > > > my $counter = 0; > > while($counter < 5) { > > test($counter); # pass the variable > > $counter++; > > } > > </CODE> > > > > You can write your test subroutine like this: > > <CODE> > > sub test{ > > my $count = shift(@_); > > print $count,$/; > > } > > </CODE> > > I would of course not call the subroutine test, using a more > > descriptive name would be better. > > Hope this helps. > > Passing a number of variables around to every function that might need > them is what I'm trying to avoid. When you later modify one of the > functions and it needs more variables, you have to go through all > functions that call the modified function, modify the all the calls of > the modified function and add these variables to the calling functions > as well just so they can pass them over. > > When you pass a pointer to a data structure around, you have only one > place to modify (besides modifying the function), which is the > declaration of the data structure. (And you can get away without global > variables.) > > What's the equivalent to passing around a pointer in perl? You could > pass a hash. Doing that would create a copy of the hash every time it's > handed over, wouldn't it? Passing just a pointer is far more beautiful > and doesn't involve the overhead of creating copies. You can either pass a reference to a hash (which will cause the hash not to be copied) or better yet - look into object-oriented programming in Perl: http://perl-begin.org/topics/object-oriented/ (= a link to my site.) Passing an object allows you to pass a single scalar which holds several fields/slots/attributes/member variables/etc. Regards, Shlomi Fish -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ "Star Trek: We, the Living Dead" - http://shlom.in/st-wtld Nobody expects the Randal Schwartz condition! — David Fetter Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/