views:

222

answers:

4

I want to set the LIST_SEPARATOR in perl, but all I get is this warning:

Name "main::LIST_SEPARATOR" used only once: possible typo at ldapflip.pl line 7.

Here is my program:

#!/usr/bin/perl -w

@vals;
push @vals, "a";
push @vals, "b";

$LIST_SEPARATOR='|';

print "@vals\n";

I am sure I am missing something obvious, but I don't see it.

Thanks

+12  A: 

Only the mnemonic is available

$" = '|';

unless you

use English;

first.

As described in perlvar. Read the docs, please.

The following names have special meaning to Perl. Most punctuation names have reasonable mnemonics, or analogs in the shells. Nevertheless, if you wish to use long variable names, you need only say

use English;

at the top of your program. This aliases all the short names to the long names in the current package. Some even have medium names, generally borrowed from awk. In general, it's best to use the

use English '-no_match_vars';

invocation if you don't need $PREMATCH, $MATCH, or $POSTMATCH, as it avoids a certain performance hit with the use of regular expressions. See English.

ephemient
+9  A: 

perlvar is your friend:

$LIST_SEPARATOR
$"

This is like $, except that it applies to array and slice values interpolated into a double-quoted string (or similar interpreted string). Default is a space. (Mnemonic: obvious, I think.)

$LIST_SEPARATOR is only avaliable if you use English; If you don't want to use English; in all your programs, use $" instead. Same variable, just with a more terse name.

Chris Lutz
Have to love it. One variable, with two names, and only one of them works. :-S. Thanks for the help
Aaron
They will both work as soon as you use the English module.
Geo
They both work. The second name is only if you ask for it in order to prevent unnecessary namespace clog-age.
Chris Lutz
Also, you might want to "use English qw( -no_match_vars );" to avoid performance issues (unless you want $PREMATCH, $MATCH, or $POSTMATCH for some reason).
gpojd
+2  A: 

you SHOULD use the strict pragma:

use strict;

you might want to use the diagnostics pragma to get additional hits about the warnings (that you already have enabled with the -w flag):

use diagnostics;
Yann
Something I've wondered - in ActivePerl or another Windows-based Perl (excluding Cygwin), does it actively look for flags in the #!/usr/bin/perl line, and thus understand that you were using warnings, or would it just treat it like another comment? Basically, is #!/usr/bin/perl -w portable off Unix?
Chris Lutz
Chris, that's a good question that you might want to promote as a question on its own. I don't have windows so I can't tell, but it should be fairly easy to prove it one way or the other by trials (sometimes faster than looking at the documentation)
Yann
@Chris Lutz: yes. perl itself parses the #! line and handles the flags if needed, but at a later point in starting up than if they were actually on the command line, causing some minor differences.
ysth
Awesome. I've avoided #!/usr/bin/perl -w like the plague because I didn't know if it was portable, but it does have some appeal in its terseness. I'll stick to my 'use warnings;' though. TMTOWTDI.
Chris Lutz
`use warnings` has the benefit of only applying to the local scope, rather than setting the interpreter to apply warnings to all modules loaded. If you use modules that aren't warnings-clean, this is a reason not to use `-w`.
ephemient
+6  A: 

Slightly off-topic (the question is already well answered), but I don't get the attraction of English.

Cons:

  • A lot more typing
  • Names not more obvious (ie, I still have to look things up)

Pros:

  • ?

I can see the benefit for other readers - especially people who don't know Perl very well at all. But in that case, if it's a question of making code more readable later, I would rather this:

{
  local $" = '|'; # Set interpolated list separator to '|'
  # fun stuff here...
}
Telemachus
I agree, but I can't vote for another hour.
Chris Lutz
Thanks - I accidentally hit return while my post was only half-finished. So I hope that it makes better sense now.
Telemachus