[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Bad subroutine!



Allan,

> sub removeLeadingTrailingDoubleSpaces(){
Your problem is here------------------>^^

The parentheses are telling perl that you will provide prototyping for function
arguments.  Leaving it blank says that your routine should not accept any
arguments.  Remove the parentheses and you will be fine.  Also, if you are
performing this routine many times in a script you should consider:

 $string = shift;
 $string =~ tr/ //s;    # Squash multiple spaces--tr always faster than s///
 $string =~ s/ $//;
 $string =~ s/^ //;
 return $string

If your string contains newlines, you may consider using the /s modifier to the
substitution to force absolute beginning and end--but always TIMTOWTDI.

Alex
----------
>From: Allan Greenier <agreenier@snet.net>
>To: macperl@macperl.org
>Subject: [MacPerl] Bad subroutine!
>Date: Thu, May 27, 1999, 2:52 PM
>

> Greetings,
> This one's driving me crazy and I know the answer must be simple.
> I've got this sub:
>
> sub removeLeadingTrailingDoubleSpaces(){
> $string = shift;
> $string =~ s/^ +//g;
> $string =~ s/ +$//g;
> $string =~ s/ +/ /g;
> return $string
> }
>
> When I call it from the "main" part of my program (not within any other
> subs) it works as would be expected:
>
> $searchItem = "    hello   ";
> $searchItem = removeLeadingTrailingDoubleSpaces($searchItem);
>
> But when I call it from within a subroutine:
>
> sub searchModel(){
>
> $searchItem = shift;
> my $modelLabel;
> $searchItem = removeLeadingTrailingDoubleSpaces($searchItem);
> foreach $modelLabel (keys(%Models)){
>  if ($searchItem =~ /$modelLabel/i){
>   push(@FoundModels,$modelLabel);
> } #end if
> } # end foreach
> } #end sub
>
> I get the following error:
>
> # Too many arguments for main::removeLeadingTrailingDoubleSpaces, near
> "$searchItem)"
> File '<AppleEvent>'; Line 183
> # Execution of <AppleEvent> aborted due to compilation errors.
>
> I guess I don't understand this at all. I removed any 'my' lexical
> scoping from variables cause I thought that was it.
> What am I doing wrong?
>
> Allan Greenier
>
> agreenier@snet.net
> http://www.geocities.com/~autoscript
>
> ===== Want to unsubscribe from this list?
> ===== Send mail with body "unsubscribe" to macperl-request@macperl.org
> 

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org