Warn of Symbolic Refs at "Compile" Time?

N

Nathan.Neff

Hello,

Is there a way to be warned of symbolic references when I run
"perl -c"?

Example:-----------------------------
#!/usr/bin/perl
# This code dies when it comes to print $$var_name.
# perl -c doesn't say anything.
# How can I get perl to warn me before I run the script?
use strict;
my $YES = "The quick brown fox";
my $var_name = "YES";
print "$$var_name";

#Thanks
#--Nate
 
X

xhoster

Hello,

Is there a way to be warned of symbolic references when I run
"perl -c"?

Example:-----------------------------
#!/usr/bin/perl
# This code dies when it comes to print $$var_name.
# perl -c doesn't say anything.
# How can I get perl to warn me before I run the script?
use strict;
my $YES = "The quick brown fox";
my $var_name = "YES";
print "$$var_name";

No. At the time the last line (with the derefernce) is compiled, the
compiler doesn't know that $var_name is not going to contain a scalar
reference.

Xho
 
F

Fabian Pilkowski

Is there a way to be warned of symbolic references when I run
"perl -c"?

AFAIK, no. Calling "perl -c" checks the syntax of your script and
there's no problem within. Your script *compiles* correctly.
Example:-----------------------------
#!/usr/bin/perl
# This code dies when it comes to print $$var_name.
# perl -c doesn't say anything.
# How can I get perl to warn me before I run the script?
use strict;
my $YES = "The quick brown fox";
my $var_name = "YES";
print "$$var_name";

When printing $$var_name you get a *runtime* error. I would be surprised
if there is a possibility to detect runtime errors during compile time.

regards,
fabian
 
C

Charles DeRykus

Hello,

Is there a way to be warned of symbolic references when I run
"perl -c"?

Example:-----------------------------
#!/usr/bin/perl
# This code dies when it comes to print $$var_name.
# perl -c doesn't say anything.
# How can I get perl to warn me before I run the script?
use strict;
my $YES = "The quick brown fox";
my $var_name = "YES";
print "$$var_name";

-c will execute BEGIN and CHECK blocks so here's
something that will work in your specific case:

perl -ce 'BEGIN{ require "myscript.pl";}'
Can't use string ("YES") as a SCALAR ref while "strict refs"
in use at myscript.pl at line 8.
Compilation failed in require at -e line 1.
BEGIN failed--compilation aborted at -e line 1.

hth,
 
T

Tad McClellan

#!/usr/bin/perl
use strict;
my $YES = "The quick brown fox"; ^^
my $var_name = "YES";
print "$$var_name";


This with not output

The quick brown fox

even if you remove use strict.

Symbolic references only work for variables that are in the
symbol table, your $YES variable is not in the symbol table
(lexical variables never are).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top