Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How to check if a word is reserved

by genecutl (Beadle)
on Jun 12, 2003 at 23:55 UTC ( [id://265540]=perlquestion: print w/replies, xml ) Need Help??

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

I just wrote a very simplistic syntax checker to run my development perl code through that checks if I've actually defined all the subroutines that I'm trying to use. One inelegance among many is that I currently have to define in the syntax checker what the reserved words are because I can't otherwise tell that they are not subroutine calls in the code being checked. So, is there a simple way to determine in perl whether a given string is a reserved word?

Replies are listed 'Best First'.
Re: How to check if a word is reserved
by shotgunefx (Parson) on Jun 13, 2003 at 00:17 UTC
    Or... B::Keywords

    -Lee

    "To be civilized is to deny one's nature."
Re: How to check if a word is reserved
by jsprat (Curate) on Jun 13, 2003 at 00:16 UTC
    Check keywords.h, located in the CORE directory in your perl tree. Contains a #define line for each keyword. Just parse it, strip off the unneeded cruft, place the words in a hash and there you go!

    HTH

      That's cool! I never knew about that, but I do have one question.

      I've got the following lines in my keywords.h file:

      #define KEY_x 249 #define KEY_xor 250 #define KEY_y 251

      xor I get as that's a function, but x and y are reserved? What gives?

      There is no emoticon for what I'm feeling now.

        It's hard to spot when they look paired like that, as they do wholly different things. Proof by example:

        perl -e 'print "x is a reserved word!\n" x 3;'
        perl -e '$a = "y is a _synonym_ for tr\n"; $a =~ y/_//d; print $a;'

        perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Re: How to check if a word is reserved
by hossman (Prior) on Jun 13, 2003 at 00:57 UTC

    You ask an excellent question, but i worry about your motivation.

    Wouldn't "perl -c your_file_here" serve quite well as a syntax checker?

    Update: As it has been pointed out in a reply, perl (5.6.0 anyway) doesn't seem to care if you use undefined methods when run with -c ... I thought this behavior depended on wether the method was called with "&" or not ... but it doesn't seem to make a difference one way or another...

    !/usr/local/bin/perl -wc use strict; sub foo { return not_here("baz"); } print &some_undef_method(1); __END__ bester:~> tmp/monk.pl tmp/monk.pl syntax OK
      I would prefer 'perl -wc your_file_here'.

      Always, alway, always (well nearly always) use '-w'
      - or 'use warnings;' with newer Perls.

      I mainly want to use this syntax checker for code that is still under development which may not be ready to pass a full syntax check. Also, I may be mistaken, but it seems like calling a non-existant function doesn't get caught by the perl syntax checker. It only triggers an error when the code is actually run.

        Holy Crap! ... you're right.

        I can't believe I've never noticed that before. Man, that's anoying.

        ... i retract my comment ... keep up the good work.

      Excellent point++

      -Lee

      "To be civilized is to deny one's nature."
Re: How to check if a word is reserved
by genecutl (Beadle) on Jun 13, 2003 at 18:10 UTC
    OK, here's my quickie code. It won't catch fancy things like eval'd subs, etc., but it does what I need.
    #!/usr/bin/perl use strict; my %used_sub = (); my %declard_sub = (); my @imported_sub = (); my @reserved = (); my $keywords_define_file = '/System/Library/Perl/darwin/CORE/keywords. +h'; if (open F, $keywords_define_file) { /^#define KEY_(\w+)/ and push @reserved, $1 while (<F>); } close F; while (<>) { while (/(?:\A|[^\%\@\$\w>])(\w+)\s*\(/g) { $used_sub{$1}++; } while (/sub\s+(\w+)/g) { $declard_sub{$1}++; } if (/^use\s+\S+\s+qw\((.*)\)/) { push @imported_sub, $_ foreach split /\s+/, $1; } } delete @used_sub{@reserved}; delete @used_sub{@imported_sub}; print join("\n", 'Declared subs (used):', sort( grep {exists $used_sub{$_}} keys %declard_sub ), '','', 'Declared subs (unused):', sort( grep {!exists $used_sub{$_}} keys %declard_sub ), '','', 'Used subs (declared):', sort( grep {exists $declard_sub{$_}} keys %used_sub ), '','', 'Used subs (undeclared):', sort( grep {! exists $declard_sub{$_}} keys %used_sub ), '' );

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-12-08 05:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (50 votes). Check out past polls.