Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

qw space issues

by robertw (Sexton)
on Aug 25, 2012 at 21:31 UTC ( #989758=perlquestion: print w/replies, xml ) Need Help??
robertw has asked for the wisdom of the Perl Monks concerning the following question:

Dear perlmonks, I want to pass these quoted parameters in the subroutine however how can I pass in brent olie as "brent olie" while using qw? I am too lazy to put quotes around so i want to use qw even for words that have a space in between... Ironically I am not too lazy to ask this questionxD

&comgetter(qw(Aardgas Goud brent olie Koper,));

Replies are listed 'Best First'.
Re: qw space issues
by tobyink (Abbot) on Aug 25, 2012 at 21:34 UTC
    &comgetter(qw(Aardgas Goud), q wbrent oliew, qw(Koper,));
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: qw space issues
by Athanasius (Chancellor) on Aug 26, 2012 at 03:49 UTC

    Here is another possibility:

    comgetter(qw(Aardgas Goud brent#olie Koper,)); sub comgetter { my @words = @_; tr/#/ / for @words; ... }

    (Change # to any suitable delimiter.)

    Update: Removed trailing /g from tr/#/ /.

    Athanasius <°(((><contra mundum

      Thanks to all of you, very helpful:)
Re: qw space issues
by Mr. Muskrat (Canon) on Aug 26, 2012 at 17:21 UTC

    I'm curious. Why are you "too lazy to put quotes around"?

    Based on your post, it's obvious that you don't understand how qw() works. It is often called "quote words". It will give you a list of literal strings.

    &comgetter(qw(Aardgas Goud brent olie Koper,)) will pass the list ("Aardgas", "Goud", "brent", "olie", "Koper,") into the comgetter subroutine.

    tobyink's sample uses qw() which you now understand and q() which returns a literal string (although he used an alternate quoting character 'w'). &comgetter(qw(Aardgas Goud), q wbrent oliew, qw(Koper,)); is the same as writing comgetter(qw(Aardgas Goud), q(brent olie), qw(Koper,)); or even comgetter('Aardgas', 'Goud', 'brent olie', 'Koper,');.

    More information is available in perlop's section on Quote and Quote-like Operators.

    BTW, don't use an ampersand (&) in front of the subroutine name unless you understand what you are asking for and need it for correct functionality. As used above, you're telling Perl to call your subroutine and ignore the prototypes in place on that subroutine. Assuming this is the same comgetter subroutine you shared in a previous thread, then you don't have prototypes in place (and the name of the subroutine doesn't conflict with a built-in function) so using it is misleading to anyone who sees the code. Save using the ampersand for when you need it. See perlsub for all the details. (I know that many argue that beginners should always use it but I disagree. It just ingrains bad habits.)

      Thank you for your reply:), I will read thos paragraphs and I was unaware of the ampersand meaning: I just people it that way.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://989758]
Approved by ww
[erix]: well, I hope you're right
[Your Mother]: Bill Clinton assaulted and raped repeatedly. Even when it was hinted and finally all but proved, no one cared because his sexism, and racism, and killing was decorous.
[Your Mother]: I think Trump might be better specifically because no one respects him so he won't get his way the way the others did.
[Your Mother]: (I certainly did not vote for him and have been an outspoken critic of his for three decades, by the by.)
[erix]: I'd like Russia threatened a bit more, flattered a bit less
[Your Mother]: The last two regimes killed a million, displaced 10 million, torture prisons, and ruined countries… somehow it's magically bad _now_.
[erix]: what/who are the last two regimes?
[Your Mother]: Bush/Obama. 15 straight years of warfare, torture, etc, etc.
[Your Mother]: All caused by the political fallout of Carter/Reagan when the Afghan chickens came home to roost on 9/11.
[Your Mother]: US foreign policy is toxic and like a broken record.

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2017-01-20 01:05 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (173 votes). Check out past polls.