in reply to Problem creating a function

cleaner(\$details); AND $details = cleaner($details); my $dirty; sub cleaner($dirty){ my $clean = ($dirty =~ s/\n/<br>/g;); $clean =~ s/\t/ /g; return $clean; }
Perl doesn't work that way. Passing data through to a sub goes via @_, not via named arguments.
sub cleaner { my($dirty) = @_; ... }
And your other huge mistake is in this line:
my $clean = ($dirty =~ s/\n/<br>/g;);
Let's start by dropping the inner semicolon...

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:

(my $clean = $dirty) =~ s/\n/<br>/g;
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; $clean =~ s/\n/<br>/g;

All put together, you no longer need $dirty:

sub cleaner { (my $clean = $_[0]) =~ s/\n/<br>/g; $clean =~ s/\t/ /g; return $clean; } $details = cleaner($details);
The code can still be further reduced to:
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

    I realise that your just correcting the OP's code to make it work, but doesn't converting \n's to <BR>'s and then tabs to spaces strike you as a strange thing to do?

    In as much as, one assumes that the results are destined for display in a browser, but the browser will discard both spaces and tabs, so the second step seems pointess.


    Examine what is said, not who speaks.

      Well BrowserUk, you would be correct if I was not working with a program that reads from a text file that's formated such that the paramaters are seperated by tabs, and the records by endlines. This program then takes the input from text file and displays to a browser, but I don't want to give you the impression that I'm heading down the wrong path to begin with.

      bW

        My point was that I understand the need to convert \n's to <BR>'s prior to display in a browser, but that I didn't understand why you were converting tabs to spaces, as in every browser I am familiar with, neither character will have any effect <clarification type="bad wording">both characters will have exactly the same effect</clarification> on what is rendered. If you were converting the \t's to 1 or more &nbsp;'s for example, it would have made more sense to me.


        Examine what is said, not who speaks.

Re: Problem creating a function
by Willman023 (Scribe) on Dec 25, 2002 at 17:19 UTC
    Thanks alot to jdporter,pg,bart and everyone else that helped with this node. I did put an extra semicolon in there, as did jdporter in his solution, sorry about that. I haven't started using -w yet, as I'm just starting to use strict and realize why I should have used it in the past...gimme a little time to wean myself onto that one.

    I think the reason my prototype and parameter passing was so off was my brain thinking in the wrong language(How dare I). But using $_ as many of you pointed out did the trick. I like to use functions in this manner because it is flexible, but not by way of "destructive cleaning".

    bW