[ back to toc ]

Subroutine

Date: 2002/05/29 16:48

Q:
Hello Peter,

At the very beginning of Subroutine is
the code as belows.
Could you please explain to me how these
works? With my ...

sub display_frontpage
{
my %numbers = ();
my $output = "";

Please let me hear you.

*NAME-DELETED*
A:
sub display_frontpage

starts the definition of the subroutine. The next block aka: the code
between { and } is the executable core of the subroutine.

The keyword 'my' tells the interpreter that the variables '%numbers' and
'$output' are local variables and have nothing to do with the variables
that have the same name globally.

The code

my %numbers = ();
my $output = "";

declares these local variables and at each call to the subroutine the code
also initializes the value to

() -> empty hash
"" -> empty string

regards,
Peter
Q:
Hello Peter,

Thanks a lot for your explain.
It is very clear to me now.
Can i ask further, the code
where with while and foreach work?
Especially with = $_; and foreach are confusing.

Please let me hear from you and appreciate
your work.

*NAME-DELETED*

while (<DATABASE>)
{
$line = $_;
chop $line;
@fields = split (/\|/, $line);
if ($fields[14] eq "ok") {
@ad_categories = split (/&&/, $fields[9]);

foreach $adcategory (@ad_categories) {
$numbers{$adcategory}++;
}
}
}
A:
If I start to explain you all these things that would take writing a Perl
tutorial step by step as you ask more and more.

I would rather be happy if you could visit

http://www.peter.verhas.com/tutorials/

and listen to the audio tutorials where I explain all these things about
Perl.

I hope that this helps and you will not be angry with me that I do not
explain the specific details that you ask in this letter. I explain it in
live voice in the audio tutorials.

Regards,
Peter

[ back to toc ]