C
Chris Heller
Is it possible to write a subroutine in Perl that would act just like
a call to open()?
I have a project which currently uses NFS for file storage. This being
a web project, and NFS being what it is, the decision has been made to
remove dependancy on NFS. Of course this needs to be done in the
simplest possible manner, so I've devised a simple file-transfer
protocol and server and thought it would be great if I could simply
replace calls to open() and close() with myopen() and myclose().
Consider this:
sub myopen(*$$$){
my ($sh, $fileop, $ip, $port ) = @_;
socket($sh,PF_INET,SOCK_STREAM,(getprotobyname('tcp'))[2]);
...
print $sh "$fileop\n";
...
return 1;
}
myclose(*){
my $fh = shift;
...
close($fh);
}
The subroutines are declared using prototypes so that they will work
just like open() and close() do when called with no parens around
their arguments.
myopen() takes a bareword, which I wish to turn into a socket handle,
a string like ">/writetofile" just like open() does, and two
additional arguments an IP address and a port.
myclose() is the analog of close() but handles some extra bookkeeping
and shutsdown the socket if needed.
Now the trick is, I want myopen() to not have to explicitly return a
filehandle in the return statement, I want this to work like open().
So in my code you would see something like this:
myopen FH,">fileop","127.0.0.1",4556;
while(<FH>){
print $_;
}
myclose FH;
From what I've discovered in playing with this code is that the handle
that I create in myopen() does not exist once I return from the
subroutine.
So the question I am stumped with is how can I pass a bareword into a
sub routine and treat it like a reference so that and changes I make
to it in the subroutine will persist after I return from that
subroutine?
Regards,
C. Heller
a call to open()?
I have a project which currently uses NFS for file storage. This being
a web project, and NFS being what it is, the decision has been made to
remove dependancy on NFS. Of course this needs to be done in the
simplest possible manner, so I've devised a simple file-transfer
protocol and server and thought it would be great if I could simply
replace calls to open() and close() with myopen() and myclose().
Consider this:
sub myopen(*$$$){
my ($sh, $fileop, $ip, $port ) = @_;
socket($sh,PF_INET,SOCK_STREAM,(getprotobyname('tcp'))[2]);
...
print $sh "$fileop\n";
...
return 1;
}
myclose(*){
my $fh = shift;
...
close($fh);
}
The subroutines are declared using prototypes so that they will work
just like open() and close() do when called with no parens around
their arguments.
myopen() takes a bareword, which I wish to turn into a socket handle,
a string like ">/writetofile" just like open() does, and two
additional arguments an IP address and a port.
myclose() is the analog of close() but handles some extra bookkeeping
and shutsdown the socket if needed.
Now the trick is, I want myopen() to not have to explicitly return a
filehandle in the return statement, I want this to work like open().
So in my code you would see something like this:
myopen FH,">fileop","127.0.0.1",4556;
while(<FH>){
print $_;
}
myclose FH;
From what I've discovered in playing with this code is that the handle
that I create in myopen() does not exist once I return from the
subroutine.
So the question I am stumped with is how can I pass a bareword into a
sub routine and treat it like a reference so that and changes I make
to it in the subroutine will persist after I return from that
subroutine?
Regards,
C. Heller