Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: comments and qw

by rnahi (Curate)
on Feb 12, 2006 at 18:33 UTC ( [id://529668]=note: print w/replies, xml ) Need Help??


in reply to comments and qw

It should not be to hard to implement, since there is a similar mechanism to allow comments in regular expressions with the /x modifier.

Here is a quick example of a qwx function.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; sub qwx { my ( $text ) = @_; $text =~ s/\s*[#].*$//mg; return split ' ', $text; } my @items = qwx( ' this is # possibly a # commented list ' ); print Dumper \@items; __END__ $VAR1 = [ 'this', 'is', 'a', 'list' ];

Replies are listed 'Best First'.
Re^2: comments and qw
by ikegami (Patriarch) on Feb 12, 2006 at 19:10 UTC

    It might be useful to skip blank lines (or lines that just contain comments) like the real qw():

    sub qwx { local $_ = @_ ? $_[0] : $_; s/[#].*$//mg; s/^\s+//; s/\s+$//; s/\s+/ /g; return split ' ', $text; }

    Also, you could tweak the appearance of caller a little as follows:

    my @items = qwx" this is # possibly a # commented list ";

    Single quote won't work without a space before it.

      It might be useful to skip blank lines (or lines that just contain comments) like the real qw():
      That won't be necessary. split with a single space is a special case that splits on any number of whitespace, including newlines.

      Try it for yourself:

      #!/usr/bin/perl use strict; use warnings; use Data::Dumper; sub qwx { my ( $text ) = @_; $text =~ s/\s*[#].*$//mg; return split ' ', $text; } my @items = qwx" this is # possibly a # commented list # with stand-alone comments # and empty lines " ; print Dumper \@items; __END__ $VAR1 = [ 'this', 'is', 'a', 'list' ];
        ah yes, I forgot.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://529668]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-03-19 05:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found