GPUtils.pm 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. ##############################################
  2. # $Id: GPUtils.pm 6653 2014-10-02 11:59:37Z ntruchsess $
  3. ##############################################
  4. package GPUtils;
  5. use Exporter qw( import );
  6. use strict;
  7. use warnings;
  8. our %EXPORT_TAGS = (all => [qw(GP_Define GP_Catch GP_ForallClients GP_Import)]);
  9. Exporter::export_ok_tags('all');
  10. #add FHEM/lib to @INC if it's not allready included. Should rather be in fhem.pl than here though...
  11. BEGIN {
  12. if (!grep(/FHEM\/lib$/,@INC)) {
  13. foreach my $inc (grep(/FHEM$/,@INC)) {
  14. push @INC,$inc."/lib";
  15. };
  16. };
  17. };
  18. sub GP_Define($$) {
  19. my ($hash, $def) = @_;
  20. my @a = split("[ \t]+", $def);
  21. my $module = $main::modules{$hash->{TYPE}};
  22. return $module->{NumArgs}." arguments expected" if ((defined $module->{NumArgs}) and ($module->{NumArgs} ne scalar(@a)-2));
  23. $hash->{STATE} = 'defined';
  24. if ($main::init_done) {
  25. eval { &{$module->{InitFn}}( $hash, [ @a[ 2 .. scalar(@a) - 1 ] ] ); };
  26. return GP_Catch($@) if $@;
  27. }
  28. return undef;
  29. }
  30. sub GP_Catch($) {
  31. my $exception = shift;
  32. if ($exception) {
  33. $exception =~ /^(.*)( at.*FHEM.*)$/;
  34. return $1;
  35. }
  36. return undef;
  37. }
  38. sub GP_ForallClients($$@)
  39. {
  40. my ($hash,$fn,@args) = @_;
  41. foreach my $d ( sort keys %main::defs ) {
  42. if ( defined( $main::defs{$d} )
  43. && defined( $main::defs{$d}{IODev} )
  44. && $main::defs{$d}{IODev} == $hash ) {
  45. &$fn($main::defs{$d},@args);
  46. }
  47. }
  48. return undef;
  49. }
  50. sub GP_Import(@)
  51. {
  52. no strict qw/refs/; ## no critic
  53. my $pkg = caller(0);
  54. foreach (@_) {
  55. *{$pkg.'::'.$_} = *{'main::'.$_};
  56. }
  57. }
  58. 1;