Question about using chomp and other functions together

G

George

Dear All,

I wrote a function to mimic the behavior of the trim function in
Javascript as follows:

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

Then I am using it as follows:

$finalstate = chomp(trim(<STDIN>));

When I try and run it, I get the following message:
Can't modify non-lvalue subroutine call in chomp at ./trans2autnew.pl
line 63, near "))"
Execution of ./trans2autnew.pl aborted due to compilation errors.
(of course, strict and warnings are on).

Could you help me resolving this as I new to Perl (but not in
programming in general)?

Regards,
George
 
R

RedGrittyBrick

George said:
Dear All,

I wrote a function to mimic the behavior of the trim function in
Javascript as follows:

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

Then I am using it as follows:

$finalstate = chomp(trim(<STDIN>));

When I try and run it, I get the following message:
Can't modify non-lvalue subroutine call in chomp at ./trans2autnew.pl
line 63, near "))"
Execution of ./trans2autnew.pl aborted due to compilation errors.
(of course, strict and warnings are on).

Could you help me resolving this as I new to Perl (but not in
programming in general)?

chomp($finalstate = trim(<STDIN>));
 
D

Dr.Ruud

George said:
sub trim($)
{
my $string = shift;

I use shift only if changing @_ is profitable.

my ($string) = @_;

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

s/\s+\z//, s/\A\s+// for $string;
return $string;
}

Then I am using it as follows:

$finalstate = chomp(trim(<STDIN>));

No need for chomp, because trim() already
removed all whitespace at both ends.
 
J

John W. Krahn

Dr.Ruud said:
I use shift only if changing @_ is profitable.

my ($string) = @_;



s/\s+\z//, s/\A\s+// for $string;


No need for chomp, because trim() already
removed all whitespace at both ends.

Assuming that $/ contains some kind of whitespace character(s).



John
 
S

sln

Dear All,

I wrote a function to mimic the behavior of the trim function in
Javascript as follows:

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

Then I am using it as follows:

$finalstate = chomp(trim(<STDIN>));

When I try and run it, I get the following message:
Can't modify non-lvalue subroutine call in chomp at ./trans2autnew.pl
line 63, near "))"
Execution of ./trans2autnew.pl aborted due to compilation errors.
(of course, strict and warnings are on).

Could you help me resolving this as I new to Perl (but not in
programming in general)?

Regards,
George

\s will remove trailing \n
Perldocs: "\s is a whitespace character and represents [\ \t\r\n\f]"

If you want to generalize it even more you could do something like below.
You should avoid passing around copies of unknown length strings.
Trim() act's on the existing string (instead of a copy), the last two
return the modified string.

-sln
---------------------
use strict;
use warnings;

sub trim($)
{
$_[0] =~ s/^\s+//;
$_[0] =~ s/\s+$//;
# wantarray() returns
# true = list, false = scalar, undefined = void context
return $_[0] if defined wantarray;
}

my $finalstate = <STDIN>;
trim($finalstate);
print $finalstate, "<-1-\n\n";

$finalstate = trim(<STDIN>);
print $finalstate, "<-2-\n\n";

print trim(<STDIN>), "<-3-\n\n";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top