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

Re: Yet another does-function-exist question (eval sub)

by tye (Sage)
on Apr 14, 2007 at 16:22 UTC ( #610079=note: print w/replies, xml ) Need Help??

in reply to Yet another does-function-exist question

sub definedWord { my( $word )= @_; return eval "use strict; my \$x= sub { $word }; 1" || ( warn("$@\n"), $@ !~ /^Bareword/ ); } @ARGV= qw( definedWord time system foo BEGIN if -e lt goto last ) if ! @ARGV; for( @ARGV ) { printf "$_:\t%sdefined\n", definedWord($_) ? "" : "UN"; }

is one interesting interpretation, producing:

definedWord: defined time: defined system: defined Bareword "foo" not allowed while "strict subs" in use at (eval 4) line + 1. foo: UNdefined BEGIN: defined syntax error at (eval 6) line 1, at EOF if: defined -e: defined syntax error at (eval 8) line 1, near "{ lt" lt: defined goto: defined last: defined

- tye        

Replies are listed 'Best First'.
Re^2: Yet another does-function-exist question (eval sub)
by kyle (Abbot) on Apr 14, 2007 at 16:33 UTC

    Neat! It seems to consider a lot of things to be defined that aren't so executable. All of these, for instance:

    @ARGV= qw( ; "foo" 123 {} [] ) if ! @ARGV; # etc. __END__ ;: defined "foo": defined 123: defined {}: defined []: defined

      As the name implies, it is meant to be passed a word. (Since non-words can't be called as subroutines without unusual syntax.)

      - tye        

      If that really matters for an example, definedWord could be just powered with regex. ++tye.

      Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!
Re^2: Yet another does-function-exist question (eval sub)
by xaprb (Scribe) on Apr 14, 2007 at 17:30 UTC

    I like this idea a lot. Thanks for the suggestion.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://610079]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2021-01-26 19:15 GMT
Find Nodes?
    Voting Booth?