FrankRalphBob FrankRalphBob - 1 year ago 74
Perl Question

How does one pass $ (list separator) as an parameter?

There is this little piece of code:

use strict;
use warnings;

sub checkDefinition {
my ($var, $desc) = @_;

if (defined $var) {
print "$desc: $var \n";
} else {
print "$desc: undef \n";

checkDefinition($ , "list separator");

however, when I try to run it, I receive the following error:

>perl String found where operator expected at line 14, near "$ , "list separator""
(Missing operator before "list separator"?) syntax error at line 14, near "$ , "list separator"" Execution of aborted due to compilation errors.

so, this begs the question, how does one pass the list separator variable to a subroutine? I tried making a reference to it:

my $lineSepRef = \$ ;

however, I get another error:

Semicolon seems to be missing at line 14.

Is it possible to pass $ to a subroutine? If so, how?

FYI, running on windows, strawberry perl version 5.016003

Thanks in advance.

Answer Source

I guess by "line separator" you mean the special (global) Perl variable $/, which is the input record separator.

You could pass the value of that to a subroutine like this, even when I don't see why you want to do that:

checkDefinition($/ , "list separator");

In case you mean another special variable, you could pass it the same way. You can find the meanings of all special Perl variables here.


Since you were actually looking for the "list separator", you can do the following:

checkDefinition($" , "list separator");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download