Home > Archive > PERL Beginners > November 2007 > about ( .)
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Ciwei2103 2007-11-27, 10:01 pm |
| can someone explain how this code segment works:
# execute a command which may be on a local system or a remote system
sub _lrdo {
my ($cmd,$exec,$rtn);
($cmd) = (@_);
$exec = (_localsys()) ? $cmd : "$SRSH{$SYS} $SYS $cmd";
$rtn=_do($exec);
return "$rtn";
}
sub _haconfmakero {
_lrdo("$VB/haclus -value ReadOnly");
}
Q1: is the parenthes () around
_lrdo ( .. ) optional when called?
Q2: why (_) around
($cmd ) = ( @_) ,
what is the desired effect of puting ( )
around @_ , and around ($cmd )
Q3: what ( ) archive in the line
my ($cmd,$exec,$rtn );
thanks
| |
| Jeff Pang 2007-11-27, 10:01 pm |
| On Nov 28, 2007 4:09 AM, ciwei2103 <ciwei2103@gmail.com> wrote:
>
> Q1: is the parenthes () around
> _lrdo ( .. ) optional when called?
>
what's the meanings of "parenthes"?
you can call _Irdo(...) directly in the script.
>
> Q2: why (_) around
> ($cmd ) = ( @_) ,
This can be written as: ($cmd) = @_;
the () around @_ is not needed, since @_ is already a list.
>
> what is the desired effect of puting ( )
> around @_ , and around ($cmd )
putting () around @_ is not needed as I've said.
but () around $cmd is required, this force the entire expression to be
into list context.
without (), the expression is in scalar context, $cmd = @_ will return
@_ 's length.
>
> Q3: what ( ) archive in the line
> my ($cmd,$exec,$rtn );
>
No special meanings here. Instead you can say:
my $cmd; my $exec; my $rtn;
by saying:
my ($cmd,$exec,$rtn);
is shorter for the keyboard input.
| |
| Tom Phoenix 2007-11-27, 10:01 pm |
| On 11/27/07, ciwei2103 <ciwei2103@gmail.com> wrote:
> # execute a command which may be on a local system or a remote system
> sub _lrdo {
> my ($cmd,$exec,$rtn);
> ($cmd) = (@_);
> $exec = (_localsys()) ? $cmd : "$SRSH{$SYS} $SYS $cmd";
> $rtn=_do($exec);
> return "$rtn";
> }
>
> sub _haconfmakero {
> _lrdo("$VB/haclus -value ReadOnly");
> }
>
>
> Q1: is the parenthes () around
> _lrdo ( .. ) optional when called?
Yes, almost certainly. You may always use parentheses around the
arguments to a function call, but they may be omitted if doing so
doesn't change the meaning of the code. (You don't mention a
prototype, but if there is a prototype defined for the subroutine, the
parentheses may be required.)
> Q2: why (_) around
> ($cmd ) = ( @_) ,
The parentheses on the right aren't significant, but the ones on the
left are. The assignment's right side would be evaluated in a scalar
context without the left-side parentheses; with them, it's a list
context, and $cmd is set from the first element of the list.
> Q3: what ( ) archive in the line
> my ($cmd,$exec,$rtn );
In that line, the parentheses hold the list of variables for my().
Without the parentheses, only the first variable would be declared
lexical.
Hope this helps!
--Tom Phoenix
Stonehenge Perl Training
|
|
|
|
|