Subroutine Function

  • Thread starter nicolas-laurent
  • Start date
N

nicolas-laurent

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;
}
 
J

Jim Keenan

nicolas-laurent said:
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.

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
 
B

Brian McCauley

Jim said:
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.
 
J

Jim Keenan

Brian said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top