| Author |
Warn of Symbolic Refs at "Compile" Time?
|
|
| Nathan.Neff@gmail.com 2005-05-27, 4:00 pm |
| 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
| |
| xhoster@gmail.com 2005-05-27, 4:00 pm |
| Nathan.Neff@gmail.com wrote:
> 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
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
| |
| Fabian Pilkowski 2005-05-27, 4:00 pm |
| * Nathan.Neff@gmail.com schrieb:
>
> 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
| |
| Charles DeRykus 2005-05-27, 8:57 pm |
| In article <1117207004.226934.48070@f14g2000cwb.googlegroups.com>,
<Nathan.Neff@gmail.com> wrote:
>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,
--
Charles DeRykus
| |
| Tad McClellan 2005-05-27, 8:57 pm |
| Nathan.Neff@gmail.com <Nathan.Neff@gmail.com> wrote:
> #!/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).
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
|
|
|
|