How to code an implicit $_ argument?



This is a function which trims leading and trailing whitespace from a
variable in-place, operating on $_ if there are no arguments provided.
(If the value is undef, it leaves it undef.) I feel a bit unclean
using "goto &sub" and hand-manipulation of @_. Opinions? Better ways
to do it?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#! /usr/bin/perl -w

use strict;

sub trim(;\$) {
if (@_ == 0) {
$_[0] = \$_;
goto &trim;
}
my $var = $_[0];
if (defined $$var) {
$$var =~ s/^\s+//;
$$var =~ s/\s+$//;
}
return $$var;
}

my $v = ' abc ';
trim $v;
if ($v ne 'abc') {
warn "\$v should be 'abc' but is '$v' instead.\n";
}

$_ = ' 123 ';
trim;
if ($_ ne '123') {
warn "\$_ should be '123' but is '$_' instead.\n";
}

exit 0;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The only clean way I see to do it is this, but I don't know whether
later Perl interpreters notice and optimize the tail recursion.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

....
sub trim(;\$);
sub trim(;\$) {
if (@_ == 0) {
return trim($_);
}
my $var = $_[0];
if (defined $$var) {
$$var =~ s/^\s+//;
$$var =~ s/\s+$//;
}
return $$var;
}
....

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In a recent message, someone mentioned that Perl 5.10 has the _
prototype character: if there is no matching argument, use $_.
But that doesn't work here, as the following code produces
Can't use string (" abc ") as a SCALAR ref while "strict refs"
in use at /home/tmcdaniel/local/test/084.pl line 7.
and "sub trim(\_)" causes
Malformed prototype for main::trim: \_ at
/home/tmcdaniel/local/test/084.pl line 15.

Also, where is _ documented? I don't see it in "man perlsub" for Perl
5.10 in Cygwin.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

....
sub trim(_) {
my $var = $_[0];
if (defined $$var) {
$$var =~ s/^\s+//;
$$var =~ s/\s+$//;
}
return $$var;
}
....

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--
Tim McDaniel, tmcd@xxxxxxxxx
.



Relevant Pages

  • Re: Handle Worked - can someone please double check
    ... change the behavior of the trim ... ... Public Class myTextBox ... Protected Overrides Sub OnLeave ... Private _TrimText As Boolean ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Handle Worked - can someone please double check
    ... change the behavior of the trim ... ... Public Class myTextBox ... Protected Overrides Sub OnLeave ... Private _TrimText As Boolean ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Windows Media Encoder in VB -- stopped working
    ... Can you still trim the filethat you were able to ... > creating the sample files without raising any errors. ... > Dim WithEvents BasicEdit As WMEncBasicEdit ... > Sub TrimSampleFile ...
    (microsoft.public.windowsmedia.encoder)
  • Re: Regexp to remove spaces
    ... sub fixsp5 { ... I never need to "trim" an array at this point, ... I wonder how expensive that foreach is. ... Even so, this may not be the fastest trim method - in place, no ...
    (perl.beginners)
  • Re: referencing functions
    ... > sub f { ... > my $var = shift; ... > I need to make sure that the value in $var is a function that exists... ... $ perl dispatch.pl f ...
    (perl.beginners)