parse arguments passed to subroutines

Hello,
I usually use my ($arg1, $arg2) = [at] _;
to get the arguments parsed to a subroutine.

I want this to be smarter. Sometimes I want to pass $start, $end to
the sub, sometimes I want to pass $start, $count to the sub.

but in this case my ($arg1, $arg2) = [at] _; will get confused, it
doesn't know if the argument is meant to be $end or to be $count.

does subroutine have some mechanism like getopt::long and it can know
what kind of arguments are passed in?

Thank you in advance for pointers,
Jim


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Jim Green [ Mo, 31 Januar 2011 04:01 ] [ ID #2054254 ]

Re: parse arguments passed to subroutines

Jim Green writes:

> Hello,
> I usually use my ($arg1, $arg2) = [at] _;
> to get the arguments parsed to a subroutine.
>
> I want this to be smarter. Sometimes I want to pass $start, $end to
> the sub, sometimes I want to pass $start, $count to the sub.
>
> but in this case my ($arg1, $arg2) = [at] _; will get confused, it
> doesn't know if the argument is meant to be $end or to be $count.
>
> does subroutine have some mechanism like getopt::long and it can know
> what kind of arguments are passed in?
>

Jim,

You could pass a hash reference instead.

%arg = (start=>'..',end=>'..',count=>'..');
the_func(\%arg);

Regards.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
pyh [ Mo, 31 Januar 2011 04:00 ] [ ID #2054255 ]

Re: parse arguments passed to subroutines

this will work, Thank you!

On 30 January 2011 22:00, <pyh [at] mail.nsbeta.info> wrote:
> Jim Green writes:
>>
>> Hello,
>> I usually use my ($arg1, $arg2) =3D [at] _;
>> to get the arguments parsed to a subroutine.
>> I want this to be smarter. Sometimes I want to pass $start, $end to
>> the sub, sometimes I want to pass $start, $count to the sub.
>> but in this case my ($arg1, $arg2) =3D [at] _; =A0will get confused, it
>> doesn't know if the argument is meant to be $end or to be $count.
>> does subroutine have some =A0mechanism like getopt::long and it can know
>> what kind of arguments are passed in?
>
> Jim,
> You could pass a hash reference instead.
> %arg =3D (start=3D>'..',end=3D>'..',count=3D>'..');
> the_func(\%arg);
> Regards.
>

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Jim Green [ Mo, 31 Januar 2011 04:06 ] [ ID #2054256 ]
Perl » gmane.comp.lang.perl.beginners » parse arguments passed to subroutines

Vorheriges Thema: date handle
Nächstes Thema: about utf8