Help: Snowed on how to pass argument to subroutine

Help: Snowed on how to pass argument to subroutine

am 28.01.2007 20:29:24 von HerbF

I'm new to Perl, and I'm stumped by how do I pass an argument to a
subroutine. I have a variable, $variable1, that I want to pass to a
subroutine, say subX.

I call the routine as &subX ($variableX);

sub subX (who) {
do something with $variableX;
}

As you all know, this doesn't work. I've tried to find the answer, but I get
snowed reading the solution to the problem. How do I do it?

TIA, Herb

Re: Help: Snowed on how to pass argument to subroutine

am 28.01.2007 21:44:31 von someone

HerbF@earthlink.net wrote:
> I'm new to Perl, and I'm stumped by how do I pass an argument to a
> subroutine. I have a variable, $variable1, that I want to pass to a
> subroutine, say subX.
>
> I call the routine as &subX ($variableX);
>
> sub subX (who) {
> do something with $variableX;
> }


subX( $variableX );

sub subX {
my $variableX = shift; # get the first argument from @_
# and store it in a lexically scoped variable
# do something with $variableX;
}


> As you all know, this doesn't work. I've tried to find the answer, but I get
> snowed reading the solution to the problem. How do I do it?

It is all explained in the Fine Manual:

perldoc perlsub


Or on the web at: http://perldoc.perl.org/perlsub.html




John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall

Re: Help: Snowed on how to pass argument to subroutine

am 28.01.2007 22:11:18 von paduille.4060.mumia.w+nospam

On 01/28/2007 01:29 PM, HerbF@earthlink.net wrote:
> I'm new to Perl, and I'm stumped by how do I pass an argument to a
> subroutine. I have a variable, $variable1, that I want to pass to a
> subroutine, say subX.
>
> I call the routine as &subX ($variableX);
>
> sub subX (who) {
> do something with $variableX;
> }
>
> As you all know, this doesn't work. I've tried to find the answer, but I get
> snowed reading the solution to the problem. How do I do it?
>
> TIA, Herb

my $variableX = 'xyz';

subX($variableX);

sub subX {
my $param = $_[0];
print "$param\n";
}

-----------------
Read the documentation:
Start->Run->"perldoc perlsub"


--
Windows Vista and your freedom in conflict:
http://www.securityfocus.com/columnists/420/2

Re: Help: Snowed on how to pass argument to subroutine

am 28.01.2007 23:05:58 von HerbF

John W. Krahn scribed:

>HerbF@earthlink.net wrote:
>> I'm new to Perl, and I'm stumped by how do I pass an argument to a
>> subroutine. I have a variable, $variable1, that I want to pass to a
>> subroutine, say subX.
>>
>> I call the routine as &subX ($variableX);
>>
>> sub subX (who) {
>> do something with $variableX;
>> }
>
>
>subX( $variableX );
>
>sub subX {
> my $variableX = shift; # get the first argument from @_
> # and store it in a lexically scoped variable
> # do something with $variableX;
>}
>
Thanks....exactly what I needed.
>
>> As you all know, this doesn't work. I've tried to find the answer, but I get
>> snowed reading the solution to the problem. How do I do it?
>
>It is all explained in the Fine Manual:
>
>perldoc perlsub
>
>Or on the web at: http://perldoc.perl.org/perlsub.html
>
If I could only understand the manual...I'm the guy they write the 'for
Dummies' books for. :-(

Herb

Re: Help: Snowed on how to pass argument to subroutine

am 29.01.2007 02:37:40 von Paul Lalli

On Jan 28, 5:05 pm, H...@earthlink.net wrote:
> John W. Krahn scribed:
> >> As you all know, this doesn't work. I've tried to find the answer, but I get
> >> snowed reading the solution to the problem. How do I do it?
>
> >It is all explained in the Fine Manual:
>
> >perldoc perlsub
>
> >Or on the web at:http://perldoc.perl.org/perlsub.htmlIf I could only understand the manual...I'm the guy they write the 'for
> Dummies' books for. :-(

Uh-huh. It helps if you actually *read* it. Can you please explain
to me what part of the following was difficult to understand?

Any arguments passed in show up in the array @_. Therefore,
if you called a function with two arguments, those would be
stored in $_[0] and $_[1].

Paul Lalli