Home > Archive > PERL Beginners > August 2006 > general subroutine question
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]
| Author |
general subroutine question
|
|
| Derek B. Smith 2006-08-30, 6:57 pm |
| All,
Is there a commonly known way or method to limit the
number of arguments that a subroutine can store or
pass?
thank you
derek
________________________________________
__________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
| |
| usenet@DavidFilmer.com 2006-08-30, 6:57 pm |
| Derek B. Smith wrote:
> Is there a commonly known way or method to limit the
> number of arguments that a subroutine can store or pass?
You could kinda do that with prototypes, but that's not really a good
idea.
But subroutines are called by programs. The best way to limit what
happens is to write the limits into the code (for example, if you pass
an array to a sub but don't want to pass a huge array, pre-validate the
array's size before passing it to the sub).
--
David Filmer (http://DavidFilmer.com)
| |
| Paul Lalli 2006-08-30, 6:57 pm |
| Derek B. Smith wrote:
> Is there a commonly known way or method to limit the
> number of arguments that a subroutine can store or
> pass?
First, don't use prototypes. Even if that's what they were designed
for, eventually they'll break your code.
use Carp;
sub only3 {
croak "only3 called with too many arguments" if @_ > 3;
#do whatever you need to with @_
}
Paul Lalli
| |
| D. Bolliger 2006-08-30, 6:57 pm |
| Derek B. Smith am Mittwoch, 30. August 2006 19:56:
> All,
>
> Is there a commonly known way or method to limit the
> number of arguments that a subroutine can
> store
store where?
> or pass?
pass to what?
Did you mean: Is there a way for a subroutine to react to a call with to many
arguments?
sub accept_max_5_arguments {
die 'too many arguments!' if @_ > 5;
}
sub accept_max_x_arguments { # apart from the first
my $maxargs=shift;
die 'too many arguments!' if @_ > $maxargs;
}
If this does not help: Could you, long time posting list member, clarify your
question?
Dani
| |
| Derek B. Smith 2006-08-30, 6:57 pm |
|
> store where?
>
>
> pass to what?
>
>
> Did you mean: Is there a way for a subroutine to
> react to a call with to many
> arguments?
>
> sub accept_max_5_arguments {
> die 'too many arguments!' if @_ > 5;
> }
>
> sub accept_max_x_arguments { # apart from the first
> my $maxargs=shift;
> die 'too many arguments!' if @_ > $maxargs;
> }
>
> If this does not help: Could you, long time posting
> list member, clarify your
> question?
>
> Dani
>
>
########################################
#############
Why is it so many people on the list are sarcastic???
example from Dani: "Could you, long time posting
list member, clarify your
question?"
This does not help the situation.
Anyway, I will claify. Is there an inheriant Perl
rule, one that does not require you to code one up,
that disallows a subroutine to pass too may arguments
to another subrountine or scalar?
Likewise, is there a inheriant Perl rule, one that
does not require you to code one up, that disallows a
subroutine to store too many arguments in @_?
If not then yes your example will suffice.
thank you
________________________________________
__________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
| |
| Ed Christian 2006-08-30, 6:57 pm |
| > Is there a commonly known way or method to limit the
> number of arguments that a subroutine can store or
> pass?
>
perldoc perlsub
Look there for "Prototypes".
| |
| usenet@DavidFilmer.com 2006-08-30, 6:57 pm |
| Derek B. Smith wrote:
> Why is it so many people on the list are sarcastic???
> example from Dani...
I don't spot the sarcasm. I thought Dani was making an effort to
helpfully answer a very vague question. If you don't want people to
notice that you post vague questions, don't post vague questions.
> Anyway, I will claify. Is there an inheriant Perl
> rule, one that does not require you to code one up,
> that disallows a subroutine to pass too may arguments
> to another subrountine
Yes, it's called prototyping, but it's a BAD IDEA to use prototypes -
they will kinda sorta do what you want until the inevitable day when
they break your code and burn down your house. Get a copy of Damian
Conway's "Perl Best Practices" (every Perl hack should have one) and
check out pp 194-196 for a somewhat better explanation.
> or scalar?
Huh? Pass arguments to a scalar?
> Likewise, is there a inheriant Perl rule, one that
> does not require you to code one up, that disallows a
> subroutine to store too many arguments in @_?
No.
--
David Filmer (http://DavidFilmer.com)
| |
| Adriano Ferreira 2006-08-30, 6:57 pm |
| On 8/30/06, Derek B. Smith <derekbellnersmith@yahoo.com> wrote:
> Why is it so many people on the list are sarcastic???
Many enjoy being that way. Some enjoy helping people at this list and
others, but get tired sometimes and forget they can only ignore what
they thought to be a not-so-clear question and leave others to answer
(if they feel so) and save flames to a stronger argument.
Is there an inheriant Perl
> rule, one that does not require you to code one up,
> that disallows a subroutine to pass too may arguments
> to another subrountine or scalar?
> Likewise, is there a inheriant Perl rule, one that
> does not require you to code one up, that disallows a
> subroutine to store too many arguments in @_?
No. There's nothing like that in Perl. Even prototypes can be
circumvented. It is all there where Christian pointed to
>perldoc perlsub
>Look there for "Prototypes".
Regards.
| |
| D. Bolliger 2006-08-30, 6:57 pm |
| Derek B. Smith am Mittwoch, 30. August 2006 20:44:
>
> ########################################
#############
>
> Why is it so many people on the list are sarcastic???
> example from Dani: "Could you, long time posting
> list member, clarify your
> question?"
Sarcasm was not intended - I'm simply not a native english speaker and I don't
mention it in every post :-)
What I wanted to say - without offending you - was: You asked a lot of
questions, therefore have a lot of experience with this list, and should know
that and how the questions influnce the answers. As more precise the
question, as more precise the answer can be (mostly not from me ;-) ).
["precise" is hardly the correct english word, but...]
> This does not help the situation.
> Anyway, I will claify. Is there an inheriant Perl
> rule, one that does not require you to code one up,
> that disallows a subroutine to pass too may arguments
> to another subrountine
I don't know of such a thing. How should such a feature "don't allow too many
arguments without coding something that specifies 'many'" be implemented?
Christian mentioned prototypes, maybe that's what you're looking for, but they
should be avoided, they do more than just defined the maximum of possible
arguments, and involve some sort of "coding".
> or scalar?
I never heard of a way to pass arguments to (normal) scalars in perl. Somebody
else may know more.
> Likewise, is there a inheriant Perl rule, one that
> does not require you to code one up, that disallows a
> subroutine to store too many arguments in @_?
>
> If not then yes your example will suffice.
I don't think there is such a rule, but wait for other answers.
Dani
| |
| Derek B. Smith 2006-08-30, 6:57 pm |
| --- "D. Bolliger" <info@dbolliger.ch> wrote:
> Derek B. Smith am Mittwoch, 30. August 2006 20:44:
> first
> posting
>
########################################
#############
> sarcastic???
>
> Sarcasm was not intended - I'm simply not a native
> english speaker and I don't
> mention it in every post :-)
>
> What I wanted to say - without offending you - was:
> You asked a lot of
> questions, therefore have a lot of experience with
> this list, and should know
> that and how the questions influnce the answers. As
> more precise the
> question, as more precise the answer can be (mostly
> not from me ;-) ).
> ["precise" is hardly the correct english word,
> but...]
>
> up,
> arguments
>
> I don't know of such a thing. How should such a
> feature "don't allow too many
> arguments without coding something that specifies
> 'many'" be implemented?
>
> Christian mentioned prototypes, maybe that's what
> you're looking for, but they
> should be avoided, they do more than just defined
> the maximum of possible
> arguments, and involve some sort of "coding".
>
>
> I never heard of a way to pass arguments to (normal)
> scalars in perl. Somebody
> else may know more.
>
> disallows a
>
> I don't think there is such a rule, but wait for
> other answers.
>
>
> Dani
>
I was not offended, just a little bothered b/c I am
only trying to learn more. Yes, now I see how I
should of been more precise and yes that word fits
well in this context, but all in all my question was
answered and I am reading the perldoc.
cherri'O
derek
________________________________________
__________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
|
|
|
|
|