# Subroutine Function

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

Hello world,
Can anybody help me understand what does the following subroutine do?
I dont understand the syntax 1. my \$res = ""; 2. \$sub = "\."; and not
sure what it does exactly. Thanks for your help.

sub unrollExcTree {
my (\$alias, \$conf) = @_;
my \$res = "";
my \$sub;
#printc "unroll \$alias";

# main alias like DERT
if (\$alias =~ /^[A-Z_]+\$/m) {
\$sub = "";
}
# sub alias like DERT19.2 or DERT3
elsif (\$alias =~ /^[A-Z_]+\d+\.\d+\$/m) {
\$sub = "";
}
else {
\$sub = "\.";
}
#printc "sub=\$sub", 'blue';

while (my (\$a, \$re) = each (%\$conf)) {
#trc("\$a =~ /^\$alias\$sub/");
if (\$a =~ /^\$alias\$sub/m) {
\$res .= "\$a,";
#trc("found \$a", '', 'green');
}
}

if (\$res eq '') {
\$res = \$alias;
}
else {
\$res = substr(\$res, 0, length(\$res)-1);
}

#trc("res=\$res"); die;
return \$res;
}

## Re: Subroutine Function

nicolas-laurent wrote:

> Hello world,
> Can anybody help me understand what does the following subroutine do?
> I dont understand the syntax 1. my \$res = ""; 2. \$sub = "\."; and not
> sure what it does exactly. Thanks for your help.
>
>
> sub unrollExcTree {
>   my (\$alias, \$conf) = @_;
>   my \$res = "";

\$res is declared within the scope of the subroutine and an empty string
is assigned as its value.

>   my \$sub;

A very poor choice of variable name, because 'sub' is a keyword in Perl
.... as shown above.

>   #printc "unroll \$alias";
>
>   # main alias like DERT
>   if (\$alias =~ /^[A-Z_]+\$/m) {
>     \$sub = "";
>   }
>   # sub alias like DERT19.2 or DERT3
>   elsif (\$alias =~ /^[A-Z_]+\d+\.\d+\$/m) {
>     \$sub = "";
>   }
>   else {
>     \$sub = "\.";

Assign badly-named \$sub the value between the double quotes.  I think
this will evaluate to a backslash followed by a dot (but I haven't
checked it out).

jimk

## Re: Subroutine Function

Jim Keenan wrote:

> nicolas-laurent wrote:
>>   my \$sub;
>
> A very poor choice of variable name, because 'sub' is a keyword in Perl
> ... as shown above.

Since Perl variables hav sigils there's no problem with having the same
name for a Perl variable and a function or keyword.

## Re: Subroutine Function

Brian McCauley wrote:
>
> Jim Keenan wrote:
>
>> nicolas-laurent wrote:
>>
>>>   my \$sub;
>>
>>
>> A very poor choice of variable name, because 'sub' is a keyword in
>> Perl ... as shown above.
>
>
> Since Perl variables hav sigils there's no problem with having the same
> name for a Perl variable and a function or keyword.
>
No problem for the compiler.  It's only a problem for the humans.
Higher probability of being misread.  Higher probability of typing
errors.  Not a good practice, particularly for a beginner (which is
where I think the OP is at).

jimk