Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

perl trim function
Does this look right? Any suggestions?

http://www.somacon.com/blog/page14.php

Report this thread to moderator Post Follow-up to this message
Old Post
Shailesh Humbad
11-27-04 01:55 PM


Re: perl trim function
Also sprach Shailesh Humbad:

> Does this look right? Any suggestions?
>
> http://www.somacon.com/blog/page14.php

Which is:

sub trimwhitespace($)
{
my $string;
$string = pop(@_);
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}

That looks right, yes. The argument handling is a bit clumsy, though.
The prototype of $ means the function always receives one argument,
never more, never less. The pop() suggests otherwise:

sub trimwhitespace ($) {
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}

Another thing you could do is add some convenience to the function, for
example by checking in which context it was called. If it was called in
void context, trim the argument in-place:

sub trim ($) {
if (! defined wantarray) {
$_[0] =~ s/^\s+//;
$_[0] =~ s/\s+$//;
return;
}
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}

That will allow:

my $s1 = my $s2 = "     \t string   \n\n";

trim $s1;	    # $string trimmed in-place
$s2 = trim $s2; # returns trimmed string (does some copying)

But you could get fatal errors at runtime when doing this:

trim("     \t string   \n\n");

In void context, the argument must not be read-only.

Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
 pam{rekcahbus})(rekcah{lrePbus})(lreP{re
htonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval

Report this thread to moderator Post Follow-up to this message
Old Post
Tassilo v. Parseval
11-27-04 01:55 PM


Re: perl trim function
Shailesh Humbad <noreply@nowhere.com> wrote:
> Does this look right? Any suggestions?
>
> http://www.somacon.com/blog/page14.php


The forward declaration is unnecessary when the sub is called
the way you are calling it.


--
Tad McClellan                          SGML consulting
tadmc@augustmail.com                   Perl programming
Fort Worth, Texas

Report this thread to moderator Post Follow-up to this message
Old Post
Tad McClellan
11-27-04 08:56 PM


Re: perl trim function
"Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de> writes:
>     sub trim ($) {
> 	if (! defined wantarray) {
> 	    $_[0] =~ s/^\s+//;
> 	    $_[0] =~ s/\s+$//;
> 	    return;
> 	}
> 	my $string = shift;
> 	$string =~ s/^\s+//;
> 	$string =~ s/\s+$//;
> 	return $string;
>     }

I think it's a good idea to always try to avoid duplicated code
(*). Using an auxiliary function and sending \$_[0] and \$string to
it, respectively, as below, seems to work. Since I'm new to Perl,
I have to ask: is it a good way? Are there other ways?

(If speed is important, one has to do benchmark tests and see if the
overhead of the extra function is acceptable.)

sub trim1 ($) {
my $r = shift;
$$r =~ s/^\s+//;
$$r =~ s/\s+$//;
}

sub trim ($) {
if (! defined wantarray) {
trim1 \$_[0];
return;
}
my $string = shift;
trim1 \$string;
return $string;
}

(*) Not that much can go wrong in this function as it stands, but if it
grows and changes semantics, the same logic will have to be entered in
two places every time, and one may easily miss something.

Report this thread to moderator Post Follow-up to this message
Old Post
Arndt Jonasson
11-29-04 09:05 PM


Re: perl trim function
>>>>> "AJ" == Arndt Jonasson <do-not-use@invalid.net> writes:

AJ> "Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de> writes: 

AJ> sub trim1 ($) {
AJ>     my $r = shift;
AJ>     $$r =~ s/^\s+//;
AJ>     $$r =~ s/\s+$//;
AJ> }

why pass in a ref when @_ is aliased to the args? you save a deref (if
you are looking for speed).

AJ> sub trim ($) {
AJ>     if (! defined wantarray) {
AJ> 	trim1 \$_[0];
AJ> 	return;
AJ>     }

eww, prototypes! steer clear of them in general.

AJ>     my $string = shift;
AJ>     trim1 \$string;
AJ>     return $string;
AJ> }

with a cleaner api you can easily fold that code and not need a helper
sub. something like (untested):

sub trim {

my $str_ref = defined wantarray ? \"$_[0]" : \$_[0] ;

$$str_ref =~ s/^\s+// ;
$$str_ref =~ s/\s+$// ;

return $$str_ref if defined wantarray ;
return ;
}

there could be a better way to get a ref to a copy of $_[0] but that
should work ok.

uri

--
Uri Guttman  ------  uri@stemsystems.com  -------- [url]http://www.stemsystems.com[/url
]
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding
-
Search or Offer Perl Jobs  ----------------------------  [url]http://jobs.perl.org[/url
]

Report this thread to moderator Post Follow-up to this message
Old Post
Uri Guttman
11-29-04 09:05 PM


Re: perl trim function
Uri Guttman <uri@stemsystems.com> writes: 
>   AJ> sub trim ($) {
>   AJ>     if (! defined wantarray) {
>   AJ> 	trim1 \$_[0];
>   AJ> 	return;
>   AJ>     }
>
> eww, prototypes! steer clear of them in general.

Oh. I thought they were meant to be useful, for example catching incorrect
calling usage. What are the arguments against them?

Report this thread to moderator Post Follow-up to this message
Old Post
Arndt Jonasson
11-30-04 02:04 PM


Re: perl trim function
Uri Guttman  <uri@stemsystems.com> wrote in comp.lang.perl.misc:

[...]

> 	sub trim {
>
> 		my $str_ref = defined wantarray ? \"$_[0]" : \$_[0] ;
>
> 		$$str_ref =~ s/^\s+// ;
> 		$$str_ref =~ s/\s+$// ;
>
> 		return $$str_ref if defined wantarray ;
> 		return ;
> 	}
>
> there could be a better way to get a ref to a copy of $_[0] but that
> should work ok.

That's the lack in Perl of an anonymous scalar ref constructor.

I don't see an attractive alternative to quoting (or $_[ 0] . '' and
equivalent).  One could say "... ? my $x = shift : shift;" instead,
but that's neither cleaner nor clearer.  Here's a ref-free
alternative:

sub trim {
for ( defined wantarray ? my $x = shift : shift ) {
s/^\s+//;
s/\s+$//;
return $_ if defined wantarray;
}
}

Anno

Report this thread to moderator Post Follow-up to this message
Old Post
Anno Siegel
11-30-04 02:04 PM


Re: perl trim function
Arndt Jonasson  <do-not-use@invalid.net> wrote in comp.lang.perl.misc:
>
> Uri Guttman <uri@stemsystems.com> writes: 
>
> Oh. I thought they were meant to be useful, for example catching incorrect
> calling usage. What are the arguments against them?

Tom Christansen has written the "classical" paper on prototypes and
their drawbacks.  Google for "tchrist prototypes".

Anno

Report this thread to moderator Post Follow-up to this message
Old Post
Anno Siegel
11-30-04 02:04 PM


Re: perl trim function
>>>>> "AS" == Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> writes:

AS> Uri Guttman  <uri@stemsystems.com> wrote in comp.lang.perl.misc:
AS> [...]
 
 

AS> That's the lack in Perl of an anonymous scalar ref constructor.

yep.

AS> I don't see an attractive alternative to quoting (or $_[ 0] . '' and
AS> equivalent).  One could say "... ? my $x = shift : shift;" instead,
AS> but that's neither cleaner nor clearer.  Here's a ref-free
AS> alternative:

AS>     sub trim {
AS>         for ( defined wantarray ? my $x = shift : shift ) {
AS>             s/^\s+//;
AS>             s/\s+$//;
AS>             return $_ if defined wantarray;
AS>         }
AS>     }

i like it but it took a while for my morning eyes to follow the aliasing
levels. i never realized that shift kept the same aliasing as $_[0] but
i rarely use shift on @_ or the aliasing like that. and we both have the
duplicate use of wantarray but i don't see any easy way around that.

uri

--
Uri Guttman  ------  uri@stemsystems.com  -------- [url]http://www.stemsystems.com[/url
]
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding
-
Search or Offer Perl Jobs  ----------------------------  [url]http://jobs.perl.org[/url
]

Report this thread to moderator Post Follow-up to this message
Old Post
Uri Guttman
11-30-04 09:01 PM


Re: perl trim function
Also sprach Uri Guttman:
 

>  AS> I don't see an attractive alternative to quoting (or $_[ 0] . '' and
>  AS> equivalent).  One could say "... ? my $x = shift : shift;" instead,
>  AS> but that's neither cleaner nor clearer.  Here's a ref-free
>  AS> alternative:
>
>  AS>     sub trim {
>  AS>         for ( defined wantarray ? my $x = shift : shift ) {
>  AS>             s/^\s+//;
>  AS>             s/\s+$//;
>  AS>             return $_ if defined wantarray;
>  AS>         }
>  AS>     }
>
> i like it but it took a while for my morning eyes to follow the aliasing
> levels. i never realized that shift kept the same aliasing as $_[0] but
> i rarely use shift on @_ or the aliasing like that. and we both have the
> duplicate use of wantarray but i don't see any easy way around that.

There is no need for the second wantarray check. It doesn't really harm
to return a value in void context. :-) As for the aliasing effect of
shift, I am not too much a fan of that either, so I'd write the whole
thing thusly:

sub trim ($) {
local *_ = defined wantarray ? \(my $dummy = shift) : \$_[0];
s/^\s+//;
s/\s+$//;
return $_;
}

Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
 pam{rekcahbus})(rekcah{lrePbus})(lreP{re
htonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval

Report this thread to moderator Post Follow-up to this message
Old Post
Tassilo v. Parseval
11-30-04 09:01 PM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

PERL Miscellaneous archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:44 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.