Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: help! Please please help!

by amarquis (Curate)
on Mar 14, 2008 at 12:47 UTC ( [id://674193]=note: print w/replies, xml ) Need Help??


in reply to help! Please please help!

In addition to what Corion said, you don't need to use qw, but it can save you some work and help you format your source better.

Basically:

my @servers = ('213.123.20.134','213.123.20.119','213.123.20.120','213.123.20.121','213.123.20.122','213.123.20.123', '213.123.20.124','213.123.20.125','213.123.20.126','213.123.20.127','213.123.20.128','213.123.20.129', '213.123.20.130','213.123.20.131','213.123.20.132');

... can turn into:

my @servers = qw( 213.123.20.134 213.123.20.119 213.123.20.120 213.123.20.121 #etc etc. );

Basically, it will split on (I believe) whitespace* and treat anything between as quoted, so you get rid of quotes and commas. It lets you have more freedom in arranging a long list.

Also, you might get better results posting here if you add <code;> tags around your code and write a title that summarizes your issue. It helps people get a handle on your problem quickly, which makes them more likely to help you out.

* - Just checked perldoc, "Evaluates to a list of the words extracted out of STRING, using embedded whitespace as the word delimiters."

Replies are listed 'Best First'.
Re^2: help! Please please help!
by grinder (Bishop) on Mar 14, 2008 at 13:12 UTC

    I should point out that one may not embed comments in a qw(), as your example might tend to suggest. Doing so (with warnings enabled) will cause the "Possible attempt to put comments in qw() list" warning to be issued.

    Actually, you can, but I don't think the results will be what you expected.

    • another intruder with the mooring in the heart of the Perl

      That's one of the reasons why when I start getting a qw// that's spanning more than 2-ish lines I've taken to changing to using YAML::Syck in a here-doc sort of like this:

      use YAML::Syck qw( Load ); my @servers = @{ Load( <<'EOT' ) }; ## Now I can have comments - 213.123.20.134 - 213.123.20.119 - 213.123.20.120 - 213.123.20.121 EOT

      If the list grows even longer you can easily move it after your __END__ token and change to my $server_data = do { local $/; <DATA> };  my @servers = @{ Load( $server_data ) };; and it's not a big step from there to swap to a separate data file and use LoadFile instead.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

      Thanks for the correction, I'm sure it will save me some bug hunting someday :). I've never put a comment inside a qw() before and had never really thought about it before.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-03-29 05:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found