problem with subroutines

Hello there,
     I have made a perl subroutine lik this

sub getSign {    
#use strict;
require 5.002;
use Socket;

#my(@input) = @_;
my($remote,$port,$iaddr,$paddr,$proto,$line,$retval,$beg,$end,$sign);
$port =  shift || 8888;
$iaddr =  inet_aton("9.184.192.17") or die "Socket S!";
$paddr =  sockaddr_in($port, $iaddr) or die "Socket S!";
$proto = getprotobyname('tcp') or die "socket $!";
socket(SOCK, PF_INET, SOCK_STREAM, $proto) or die "socket $!";
connect(SOCK, $paddr) or die "socket  $!";
send(SOCK, "\%S\#data::Hellow\$\$\$\n", 0) or die "socket $!";    
$line = <SOCK> ;
if($line =~ /\#signature::/ )
 {
   $beg = index($line, "#signature::");
   $end = index($line, "\$\$\$");
   $sign = substr($line, $beg+12, $end-$beg-12);  
 } else
        {  
            $beg =   index($test,"\%");
            $end  =  index($test,"\$\$\$");
            $sign = substr($test, 1,$end-$beg-1);                
        }      
close(SOCK);
return ($sign);
}

This program basically opens socket connection to some system and sends a packet in format "%S#data::datato Sign$$$\n" and then receives the signed data.
       The problem with the above subroutine is if I am using it like this
$ret = getSign or $ret = &getSign or $ret=getSign( )
  it works fine and when I tries to pass some data like this
$ret = getSign("data1", "data2", "data3") or
  $ret = getSign(@myInput)
   the program files at connect saying Unknown Error: 0x0000274d at sign.pl line 21, what is the problem with this?.

      I am very novice at perl and my problem is  I have to submit this to our client urgently, please help me. And please try to make it as module and tell me how to use that module in regular perl programs .
     Thanks in advance.
-shivaki
shivakiAsked:
Who is Participating?
 
ventolinConnect With a Mentor Commented:
i would fix the way you pass your arguments and then work on you socket implementation.

try passing them with a hash like:

getSign( PORT => '88',
         IP => '127.0.0.1' );

if this is a module, you will first  want to catch the reference:

sub getSign {      
 my $self = shift;

then i'd get arguments:

 %args = @_;

to access them, do this:

 my $port = $args{'PORT'};
 my $ip = $args{'IP'};

and then use this in your socket implementation


0
 
shivakiAuthor Commented:
Thanks for Ur help vetolin,
    I found the bug in my code the real culprit was $port =  shift || 8888; If You help me  how to make this subroutine as apackage/module that's a great help for me.
Thanks alot
shivaki
0
 
ozoCommented:
What arguments are you trying to pass to the sub?
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
shivakiAuthor Commented:
an array comprises 4 arguments. My intention is that this subroutine should be used in many programs, I don't know  whether is there any #include option is available in perl. But I heard that the equivalent concept in Perl is packages/modules, can U help me to build this?.
Thanks & Regards
shivaki
0
 
shivakiAuthor Commented:
an array comprises 4 arguments. My intention is that this subroutine should be used in many programs, I don't know  whether is there any #include option is available in perl. But I heard that the equivalent concept in Perl is packages/modules, can U help me to build this?.
Thanks & Regards
shivaki
0
 
ventolinCommented:
post your revised code and i'll show you.
0
 
shivakiAuthor Commented:
Thanks for all and I got this. But I landed  in a different problem(not good to ask here itself I am posting a new question) please see and reply for it.

Thanks a lot
shivaki
0
All Courses

From novice to tech pro — start learning today.