Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to test for existance ?

Reply
Thread Tools

How to test for existance ?

 
 
no spam
Guest
Posts: n/a
 
      02-21-2005

Here's a small script to show what I am trying to do:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~
#!/usr/bin/perl -w

sub print_yep
{
print "yep...\n";
}


$function1 = "print_yep";
$function2 = "rubbish";

$vector->{ACTION1} = \&$function1;
$vector->{ACTION2} = \&$function2;

print "something's fishy !\n" if (! exists $vector->{ACTION2});

$vector->{ACTION1}();
$vector->{ACTION2}();

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~

How can I test if a function exist or not ?
Shouldn't this print "something's fishy" ??
I've tried to use defined, to deference in different ways, but I can't get
it working.


Thanks,


Yves.
----
Yves Dorfsman
http://www.cuug.ab.ca/dorfsmay
http://www.SollerS.ca
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      02-21-2005
no spam <(E-Mail Removed)> wrote in news:mFdSd.447549$6l.334107@pd7tw2no:

> Here's a small script to show what I am trying to do:
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~
> #!/usr/bin/perl -w


use warnings;

is preferable because it allows you to switch specific types of warnings
on/off of various scopes.

Also, you are missing:

use strict;

> sub print_yep
> {
> print "yep...\n";
> }
>
>
> $function1 = "print_yep";
> $function2 = "rubbish";
>
> $vector->{ACTION1} = \&$function1;
> $vector->{ACTION2} = \&$function2;


At this point, it would be instructive to use Data:umper to print what
$vector actually contains. Because of auto-vivification, exists will not
tell you anything useful. You should also have checked for the
documentation for defined to see:

defined EXPR
defined Returns a Boolean value telling whether EXPR has a value other
than the undefined value "undef".

that it would have been less than useful in this case.

> print "something's fishy !\n" if (! exists $vector->{ACTION2});
>
> $vector->{ACTION1}();
> $vector->{ACTION2}();


I am sure others who know more about this stuff than I do will show a way
of doing this by checking the symbol table, but my first inclination would
have been to wrap the call in an eval.

> Shouldn't this print "something's fishy" ??


No.

> I've tried to use defined, to deference in different ways,


ITYM 'dereference'.

use strict;
use warnings;

use Carp;

sub print_yep {
print "yep...\n";
}

my $vector = {
ACTION1 => 'print_yep',
ACTION2 => 'rubbish',
};

sub dispatch {
my $table = shift;
my $action = shift;
my $dispatch_to = \&{$table->{$action}};
my @r = eval {
$dispatch_to->(@_);
};
if( $@ ) {
carp 'Something is fishy';
@r = ();
}
return wantarray ? @r : $r[0];
}

dispatch($vector, 'ACTION1');
dispatch($vector, 'ACTION2');

__END__

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      02-21-2005
no spam wrote:

> Here's a small script to show what I am trying to do:
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~
> #!/usr/bin/perl -w
>
> sub print_yep
> {
> print "yep...\n";
> }
>
>
> $function1 = "print_yep";
> $function2 = "rubbish";
>
> $vector->{ACTION1} = \&$function1;
> $vector->{ACTION2} = \&$function2;
>
> print "something's fishy !\n" if (! exists $vector->{ACTION2});
>
> $vector->{ACTION1}();
> $vector->{ACTION2}();
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~
>
> How can I test if a function exist or not ?


OK first thing you are checking for existance in the wrong way.

exists($vector->{ACTION2}) reports if the hash element exists.

exists(&{$vector->{ACTION2}}) reports if the function referenced by the
hash element exists.

Unfortunately this only works for symbolic references because as Sinan
mentioned the act of creating a hard reference is autovivificational.

You should be able to get around this is you use defined() (which gives
slightly different information[1]) but you can't. The reason for this
is a truely fascinating bug in Perl that I described in my Usenet Gems
talk YAPC::Europe::2004. (Slides are available on
http://birmingham.pm.org/ ).


[1] exists() is true but defined() false for functions for which there
is a forward declaration but no definition yet. This may often apply to
functions that are autoloaded.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to test for existance ? no spam Perl 0 02-13-2005 01:21 AM
check Existance of a file on different machine Ajit ASP .Net 5 09-26-2003 10:59 AM
Test an Array for existance before processing data Jacob ASP General 2 07-07-2003 05:28 PM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments