Steve Bertrand wrote:
I'm attempting to write a patch for a module that I feel I can use
within one of my own modules, and I've run into something that I can't
solve (I can't come up with the proper query in Google).

Perhaps code will help here. First, I know 'word' does not include
whitespace, and I know that one should not include prototypes in a Perl
function. I'm trying to follow the flow of the original developer.

In the third line of the following code, I want the replacement pattern
to be (whitespace * $_[1]). I'll deal with the undef issue after I
figure out how to use {$num} on essentially nothing. I haven't been able
to make it work with the likes of s/blah/\s{3}/g; etc:

sub to_word ($;$) {
        return unless defined $_[0] && $_[0] ne '';
        my $word = $_[0] =~ s/[^\w\s]/\s{3}/g;
        return unless defined $word;
}

In the substitution operator, the left part is a regular expression and the right part is just a string, so the code above is replacing a single character matching '[^\w\s]' with the string 's{3}' (the backslash is interpolated away.)

You cannot put "whitespace" in the string half because whitespace is a regular expression character class and it has no meaning in a string context.

If you want to replace a pattern with the value of $_[1] space characters then you could do this:

s/blah/ ' ' x $_[1] /eg;


The /e option treats the string on the right as Perl code and evaluates it and returns the result.



John
--
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to