Boschman has asked for the wisdom of the Perl Monks concerning the following question:

I'm attempting to create a subroutine that will take a preexisting cfg file then load it to an array that can be used later in the script, but when I execute it I get the error "Can't use string ("TEST") as a symbol ref while "strict refs" in use ... (F) Only hard references are allowed by "strict refs". Symbolic references are disallowed" My code is as follows:

sub cfgToARRAY { my ($cfgfile, $array) = @_ ; open(OUTPUT, "<", $cfgfile) or die "Can't open $cfgfile: $!" ; my $input ; while ($input = <OUTPUT>) { chomp $input ; push (@$array, $input) ; } close(OUTPUT) ; return(@$array) ; } 1 ;

and what I'm trying to pass to the sub is

cfgToARRAY("/informatica/scripts/arrayDisplayCDCWFs.cfg", "TEST") ;

as a new perl user I'm stumped. Thank you!

Replies are listed 'Best First'.
Re: Symbolic references disallowed
by aaron_baugher (Curate) on Apr 18, 2012 at 19:49 UTC

    Others told you how you should do it, so I'll just explain the error. When you say @$array, you're saying, "the array pointed to by the reference contained in $array." Since your $array contains a text string instead of an array reference, the fallback (Perl 4) way to handle that would be to use the value of the scalar as the name of the array, giving you @TEST in this case. However, this is ugly and dangerous, so strict refs does not allow it.

    Aaron B.
    My Woefully Neglected Blog, where I occasionally mention Perl.

      I would substitute "ugly and dangerous" with "easily abused in ways that lead to buggy and unmaintainable code." Beauty is in the eye of the beholder, and the real danger is misuse, rather than simply use in general.

      I upvoted your node, because it did a nice job of explaining one way of creating a symbolic reference, and offered words of caution. But most of all, because you explained "strict 'refs'" rather than just repeating the tired mantra of "use strict;", with no mention of the specific aspect of strictures that targets this coding practice.

      Maybe you've already read MJD's article, here: Why I Hate strict (part of a series of twelve lightning talks). If not, you might find it interesting.


      Dave

Re: Symbolic references disallowed
by Eliya (Vicar) on Apr 18, 2012 at 18:58 UTC

    A more typical way to handle this would be to say

    sub cfgToARRAY { my ($cfgfile) = @_ ; open(OUTPUT, "<", $cfgfile) or die "Can't open $cfgfile: $!" ; my $input ; my @array; while ($input = <OUTPUT>) { chomp $input ; push (@array, $input) ; } close(OUTPUT) ; return(@array) ; } ### my @TEST = cfgToARRAY("/informatica/scripts/arrayDisplayCDCWFs.cfg");

    instead of specifying the name of the output array as an argument to the subroutine.

    (Upd: changed @OUTPUT to @TEST, 'cos that's what I actually meant to type in the first place (to match the name chosen by the OP) )

      A more idiomatic way to handle this would be to say:

      sub cfgToARRAY { my ( $cfgfile ) = @_ ; open my $OUTPUT, '<', $cfgfile) or die "Can't open $cfgfile: $!"; chomp( my @array = <$OUTPUT> ); return @array; } ### my @OUTPUT = cfgToARRAY( "/informatica/scripts/arrayDisplayCDCWFs.cfg" + );
Re: Symbolic references disallowed
by toolic (Bishop) on Apr 18, 2012 at 19:39 UTC
    Here is another way to read all lines of a file into an array using File::Slurp:
    use File::Slurp; my @lines = read_file('/informatica/scripts/arrayDisplayCDCWFs.cfg', c +homp => 1);