Search code examples
perlparametersglobal-variablessubroutine

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 listSeparator.pl String found where operator expected at listSeparator.pl line 14, near "$ , "list separator""
        (Missing operator before "list separator"?) syntax error at listSeparator.pl line 14, near "$ , "list separator"" Execution of
listSeparator.pl 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 listSeparator.pl 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.


Solution

  • 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.

    EDIT:

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

    checkDefinition($" , "list separator");