尝试自己的Perl语言的包 UDP协议的再包装起到类似python语言装饰器的效果

尝试自己的Perl语言的包 UDP协议的再包装起到类似python语言装饰器的效果,第1张

概述#!/usr/bin/perl  # Filename: BuildSocketUDP.pm # #   Copyright 2012 Axxeo GmbH #   Licensed under the Apache License, Version 2.0 (the "License"); #   you may not use this file except in compliance wi

#!/usr/bin/perl 

# filename: BuildSocketUDP.pm

#

#   copyright 2012 Axxeo GmbH

#   licensed under the Apache license,Version 2.0 (the "license");

#   you may not use this file except in compliance with the license.

#   You may obtain a copy of the license at

#

#       http://www.apache.org/licenses/liCENSE-2.0

#

#   Unless required by applicable law or agreed to in writing,software

#   distributed under the license is distributed on an "AS IS" BASIS,

#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implIEd.

#   See the license for the specific language governing permissions and

#   limitations under the license.

#




package BuildSocketUDP;

require Exporter;

@ISA = qw(Exporter);

@EXPORT = qw(readfile checkfile);


use IO::Socket::INET; # provIDes an object interface to creating and using Socket

use strict 'vars'; # this generates a runtime error if you use symbolic references

use constant false => 0;

use constant true  => 1;

# flush after every write

$| = 1;


#Create a new instance

sub new {

  my $self = {}; # Connect the hash to the package Cocoa.

  shift;

  my ($ip,$port,$proto,$isserver) = @_;

  my $socket;

  my $self->{'ip'} = $ip;

  my $self->{'port'} = $port;

  if ($isserver == true && $proto == 'udp')

  {

  #print "Buldi socket for server\n";

  $socket = new IO::Socket::INET(

 LocalPort => $port || '8765',

    Blocking => '0',

 Proto    => $proto) or dIE "* Error Server in Socket Creation : $!\n";

    print "UDP Server connected successful be created with port : $port\n";

    print "---------------------\n";

  }

  else

  {

    #print "Buldi socket for clIEnt\n";

    $socket = new IO::Socket::INET(

    PeerHost => $ip  || '127.0.0.1',

    PeerPort => $port || '8765',

    Proto    => $proto) or dIE "* Error ClIEnt in Socket Creation : $!\n";

    print "UDP ClIEnt connected successful be created with host : $ip\n";

    print "UDP ClIEnt connected successful be created with port : $port\n";

    print "---------------------\n";

  }

  #print "$socket"."\n";

  $self->{'socket'} = $socket;

  #print $self->{'socket'}."AAAA\n";

  bless ($self);

  return $self; # Return the reference to the hash.

}



#Subroutine to close the socket

sub closeSocket

{

my $self = shift;

($self->{'socket'})->close() or dIE "* Error to close the socket"

}


#Subroutine to send the data

sub sendViasocket

{

my $self = shift;

my ($data_out,$length,$description) = @_;

($self->{'socket'})->send($data_out);

  #($self->{'socket'})->flush;

#print "Send data successful via udp socket>> : $description >>: $data_out\n";


}


#Subroutine to recv the data

sub recvViasocket

{

my $self = shift;

my ($length,$description) = @_;

my $data_in;

($self->{'socket'})->recv($data_in,$length);

  #($self->{'socket'})->flush;

#print "RecvIEd data successful via udp socket: $description >>: $data_in\n";

return $data_in;

}


1; # 当然不要忘了这个不知所以然的1


这只是给大家提供一种思路,毕竟是第一次尝试写类似的东西,难免存在不足之处,希望大家谅解,这样类似与Python语言装饰器的效果,给一些基本的包里面的方法提供了更多扩展和美化的作用,也为后来使用提供了方便。

总结

以上是内存溢出为你收集整理的尝试自己的Perl语言的包 UDP协议的再包装起到类似python语言装饰器的效果全部内容,希望文章能够帮你解决尝试自己的Perl语言的包 UDP协议的再包装起到类似python语言装饰器的效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1274298.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存