Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Use of Text::ParseWords

by blackgoat (Acolyte)
on Feb 08, 2010 at 04:28 UTC ( [id://821933]=perlquestion: print w/replies, xml ) Need Help??

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

Hi!

I am toatally new to perl and I was going through a tutorial and came across the following piece of code:

$_ = '"Jones, James", "Smith, Susan", Spot'; use Text::ParseWords; @names = quotewords(",", 0, $_); print join(" &", @names);
output: Jones, James & Smith, Susan & Spot

I didnt quite understand what it it does and how exactly it works.

Could anybody pls be kind enough to explain!

Thank You

Replies are listed 'Best First'.
Re: Use of Text::ParseWords
by 7stud (Deacon) on Feb 08, 2010 at 08:25 UTC

    You have a perl string containing this text:

    "Jones, James", "Smith, Susan", Spot

    The goal: split the string on the commas--but not where the commas are inside double quote marks.

    A natural first thought might be to use split(). However, the code:

     my @pieces = split /,/, $string;

    will split the string on every comma, giving you the pieces:

    • "Jones
    • James"
    • "Smith
    • Susan"
    • Spot

    The function quotewords() is in a module called Text::ParseWords, which is found here:

    http://perldoc.perl.org/Text/ParseWords.html

    and quotewords() accepts a list of lines and a delimiter and

    breaks those lines up into a list of words ignoring delimiters that appear inside quotes.

    In addition, if the second argument to quotewords() is false, e.g. 0, then quotewords() will strip the quote marks surrounding any of the pieces.

    By looking at the result of the join():

    Jones, James & Smith, Susan & Spot

    you can tell what quotewords() returned as the pieces:

    • Jones, James
    • Smith, Susan
    • Spot
      Thank you so much.
Re: Use of Text::ParseWords
by quester (Vicar) on Feb 08, 2010 at 07:02 UTC
    Text::ParseWords is a core Perl module. You can find its documentation here at perldoc.perl.org, along with lots of other useful documentation, tutorials, and FAQ's.
Re: Use of Text::ParseWords
by Anonymous Monk on Feb 08, 2010 at 04:35 UTC
    quotewords is a function exported by Text::ParseWords (means you don't have to call it like Text::ParseWords::quotewords()))

      What?

      I did not quite understand what you mean. I did mention I'm totally new to this. Could you care to be more explicit pls.

      Thank you very much

        Perl (and really all code languages) are made up of various 'sets' of functions. You can bring in one of these sets with the 'use Text::ParseWords;' line, which enables you to use one of the functions in that set: 'quotewords'. I.e. if you remove that 'use' line from your file, the quotewords line will give you an error.

        Code modularization and reuse are definitely important aspects of coding. I would say you should start to look into these topics in earnest once you have gotten comfortable writing a few 'basic hello world' examples from scratch on your own. In short: most advanced functionality you are looking for will be package in some module somewhere nowadays :). Hope that helps!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-24 20:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found