Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

[resolved] Can't use string ("0") as a symbol ref while "strict refs" in use

by bryan.p.spears (Initiate)
on Feb 22, 2013 at 20:46 UTC ( [id://1020224]=perlquestion: print w/replies, xml ) Need Help??

bryan.p.spears has asked for the wisdom of the Perl Monks concerning the following question:

I've written very little code, and this is my first attempt at something in perl, so please forgive the ignorance. I am trying to pass output from an external program called checkuser, into an array. Here is the error:

$ ./testpl.pl bryan@domain1.net Can't use string ("0") as a symbol ref while "strict refs" in use at . +/testpl.pl line 29. $ cat -n testpl.pl | grep 29 29 open (@checkuser_output, "checkuser $user{'name'}\@$user{'doma +in'} $domain_action{$user{'domain'}}") || die "Failed: $!\n";

And, for context, here is the script

#!/usr/local/bin/perl -Tw use strict; use Data::Dumper; chomp $ARGV[0]; my %user; my @input; my @checkuser_output; <==== took this out my $default_domain = 'default.net'; my $maintainer = 'bryan.p.spears@default.com'; my %domain_action = ( 'default1.net' => 'first', 'default2.net' => 'second', 'default3.net' => 'third', ); @input = split(/@(.*)/,lc($ARGV[0])); $user{'name'} = $input[0] ? $input[0] : '?'; $user{'domain'} = $input[1] ? $input[1] : $default_domain; my $explanation = <<END; explanation goes here END if ((@ARGV > 1) || ($user{'name'} eq '?')) { printf $explanation; exit(1); }; # placeholder: going to validate domain, here # Instead of this ====> open (@checkuser_output, "checkuser $user{'nam +e'}\@$user{'domain'} $domain_action{$user{'domain'}}") || die "Failed +: $!\n"; open (my $checkuser_output, "checkuser $user{'name'}\@$user{'domain'} +$domain_action{$user{'domain'}}") || die "Failed: $!\n"; print Dumper @checkuser_output

Per suggestions by choroba and anonymous (thank you both, very much), I removed the checkuser_output array declaration at top, and referenced it in the open statement as a scalar. One hurdle cleared..

Replies are listed 'Best First'.
Re: Can't use string ("0") as a symbol ref while "strict refs" in use
by choroba (Cardinal) on Feb 22, 2013 at 21:03 UTC
    The first argument to open should be a filehandle, not an (empty) array.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Can't use string ("0") as a symbol ref while "strict refs" in use
by Anonymous Monk on Feb 22, 2013 at 21:08 UTC

    filehandles are SCALARs, first argument to open is a filehandle, it forces scalar context, @array in scalar context is number of elements (zero), open will try to treat 0 as a filehandle, strict refs forbids it (good)

    Use a scalar ( my($fh) )

    read perlintro, Tutorials: Context tutorial

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-19 16:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found