in reply to Problem creating a function
Perl doesn't work that way. Passing data through to a sub goes via @_, not via named arguments.cleaner(\$details); AND $details = cleaner($details); my $dirty; sub cleaner($dirty){ my $clean = ($dirty =~ s/\n/<br>/g;); $clean =~ s/\t/ /g; return $clean; }
And your other huge mistake is in this line:sub cleaner { my($dirty) = @_; ... }
Let's start by dropping the inner semicolon...my $clean = ($dirty =~ s/\n/<br>/g;);
You assign the result of the s/// to $clean, which is the number of substitutions. Remember: s/// does an in-place substitution. The correct syntax for what you want is:
which first assigns the value of $dirty to $clean, and then the substitution takes place on the Lvalue, $clean. Cut into smaller peices, this does the same thing:(my $clean = $dirty) =~ s/\n/<br>/g;
my $clean = $dirty; $clean =~ s/\n/<br>/g;
All put together, you no longer need $dirty:
The code can still be further reduced to:sub cleaner { (my $clean = $_[0]) =~ s/\n/<br>/g; $clean =~ s/\t/ /g; return $clean; } $details = cleaner($details);
sub cleaner { local $_ = shift; s/\n/<br>/g; s/\t/ /g; return $_; } $details = cleaner($details);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Problem creating a function
by BrowserUk (Patriarch) on Dec 25, 2002 at 11:58 UTC | |
by Willman023 (Scribe) on Dec 25, 2002 at 17:06 UTC | |
by BrowserUk (Patriarch) on Dec 25, 2002 at 18:27 UTC | |
|
Re: Problem creating a function
by Willman023 (Scribe) on Dec 25, 2002 at 17:19 UTC |