Dmitresso Dmitresso - 1 year ago 73
Perl Question

foreach my $var ($var, $var) expression in Perl

How to interpret expression

foreach my $nm ($a, "default")
? AFAIK, by
my $nm
we replace standart
$_
special character for current variable access within current loop, but I can't understand relations between loop args.
$a
is also string, if it has meaning, and let's suppose, that
$a = "foo"
.
How can it be used excepting getting
$nm = "foodefault"
?

Answer Source

when you crating the (), it means you are creating the list.

Then you have itreating loop for those list elements.

For example

my @ar = ($a,"default");

foreach (@ar)
{

In above code you are creating the list and storing into the array. And iterating for these @ar

How can it be used excepting getting $nm = "foodefault"`?

For this question, you can use the join function

my $nm;

foreach  ($a,"default")
{
    $nm.=$_;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download