jacques has asked for the wisdom of the Perl Monks concerning the following question:

I like using qw because it doesn't do interpolation. But qw uses embedded whitespace as the word delimiters and I need it to use commas as the delimiters instead. However I am using qw on the import list, a la:

use asdf qw( . . . );

By the time, I reference the list in the import sub, the list has already been splitted on the whitespace. In other words:

package asdf; require Exporter; our @ISA = qw(Exporter); sub import { my $class = shift; my @list = @_ if @_; # @list contains the splitted string

Could I somehow use join to reunite the list and then re-split it on the commas? What other solutions could I use? By the way, qw is roughly equivalent to:

split(' ', q/STRING/);

Replies are listed 'Best First'.
Re: qw with comma delimiter
by PodMaster (Abbot) on Jul 26, 2003 at 04:40 UTC
    Just don't use qw (that's how qw works, you can't change that).
    use asdf qw[ $a $s $d $f ] , 'the asdf', join ' ', qw[ the asdf ];

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      I forgot to mention that no interpolation is a must. That's why I am using qw//. I guess I could work with q//.
Re: qw with comma delimiter
by Zaxo (Archbishop) on Jul 26, 2003 at 04:43 UTC

    Certainly, but I'd just hand use a real list in the first place. Here's how to negate the wrong use of qw() you describe,

    sub import { my $class = shift; my @list = split /,/, join ' ', @_ if @_; # ... }
    but don't do that. You sound like you are cargo-culting qw//.

    After Compline,
    Zaxo

      You sound like you are cargo-culting qw//.

      Well, it was consensual cargo-culting. So I'm only guilty of adultery.

Re: qw with comma delimiter
by sauoq (Abbot) on Jul 26, 2003 at 07:21 UTC

    I, like the others who have responded, don't understand your fetish with qw//. As you yourself point out, q// avoids interpolation as well. Why don't you just use q// to pass a single argument to import() and then split on commas?

    use asdf q($this,$is,$a,$single,$scalar); package asdf; sub import { my $class = shift; my @list = split /,/, shift; # . . . }

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: qw with comma delimiter
by Limbic~Region (Chancellor) on Jul 26, 2003 at 04:57 UTC
    jacques,
    I want to clear up some confusion. qw is white space delimited - period. It is also single quoted, so no interpolation is done.
    my @array = ('$one', '@two', '%three'); my @qw_array = ($one @two %three);
    You can't change the delimiter, so if it doesn't do what you want you should use something that does.
    my @array = qw(a b c d); my $temp_string = join ',' , @array; my @new_array = split ',' , $temp_string;
    The above code is just a demonstration of how to use join and split, but you really should use perldoc -f join and perldoc -f split for more information.

    Cheers - L~R

      I want to clear up some confusion.

      Hmmm... maybe I didn't make myself clear.

Re: qw with comma delimiter
by bart (Canon) on Jul 26, 2003 at 11:37 UTC
    Why do you insist on using commas between the words? qw() is a source code beautifying tool, so you can only use it in the source code. There's no way you can apply it to user supplied data.

    Simply use whitespace as delimiter, and you can use qw() — with the added advantage that the split is done at compile time, thus once, even in case the code snippet is executed many times, like in a loop or in a sub.

Re: qw with comma delimiter
by BrowserUk (Patriarch) on Jul 26, 2003 at 21:01 UTC

    Not a favoured construct, but if your data doesn't contain newlines, you could get away with a non-interpolating HERE_DOC.

    my @list = split $/, <<'END_OF_LIST'; £$%^&*( £@£@$@^&*£&*@' "$@':>< ;,. . ..^/ END_OF_LIST print join"-|-", @list;

    Using single quotes around the label tells perl not to interpolate the contents. Using $/ or "\n" as the split format will do the delimiting and allow, leading, trailing and embedded whitespace, commas, quotes etc. Basically everything except newline.

    If you need to embed newlines as well, then your probably into the situation of having to use hand crafted CSV with appropriate quoting and escaping and something like Text::xSV.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

      Thanks. That's a nice solution. But how could I use that on the import list? I think I am resigned to going with the q// comma solution and hope for the best.

        You would use it where you could use any list. That includes as arguments to import():

        use Some::Module split( $/, <<'END_IMPORT'); argument #1 second 'argument' joij0986t098$@$*()*%@!~ END_IMPORT
Re: qw with comma delimiter (repost)
by jacques (Priest) on Jul 26, 2003 at 15:27 UTC
    Let me restate my OP. I was sleepy when I first posted. Sorry:

    I need no interpolation.
    The delimiter must delimit complex data.

    I am worried that whitespace and perhaps even commas couldn't get the job done. What if I have data like "dfs$$$\,, =>/?" and I need to separate it from "fd s fs%%#&//lp". It seems that whitespace and commas are too simplistic as delimiters.

    Update: The data will contain quotes. Let's say it can contain anything. Any possible character or combination of characters. How can I properly delimit this and prevent interpolation at the same time?

      Why can't you just create it without qw() using single quotes so you don't get interpolation?

      my @array = ('dfs$$$\,, =>/?','fd s fs%%#&//lp');

      The other option if you must use qw() would be to run your parameters through some sort of filter first that escapes the spaces but that seems to defeat the purpose of using qw at all, and is only the opposite of what you just suggested (splitting on the commas after coming out of qw().

      Like many others I don't understand why using qw() is a must since there are tools for doing exactly what you want to do. Single quoting separated by commas should do exactly what you want.

      Lobster Aliens Are attacking the world!
        Like many others I don't understand why using qw() is a must

        Note: I never said qw// was a must. Nor do I have a "fetish" with it.

        Single quoting separated by commas should do exactly what you want.

        The data will contain quotes. Let's say it can contain anything. Any possible character or combination of characters. How can I properly delimit this and prevent interpolation at the same time?