Re: return undef
by tlm (Prior) on May 23, 2005 at 02:44 UTC
|
I can't make sense of your question. If myfunction() returns a filehandle, then it is out of place in an expression like
print FILEHANDLE myfunction();
unless you want your output to look something like GLOB(0x1234567).
I think that what you want is something like
my $output_handle = myfunction();
if ( $output_handle ) {
print $output_handle $whatever;
}
else {
# deal with null-handle error
}
| [reply] [d/l] [select] |
|
|
when i use
print FILEHANDLE myfunction();
the file that is being sent from my function gets copied into FILEHANDLE. thats what i want it to do. however if there is an error myfunction() returns undef. my question is how do i check if it is returning the filehandle or undef? in order to copy the file or display the error message?
| [reply] [d/l] |
|
|
Ah, you've confused people with your terminology. You seem to mean that the function reads from a filehandle and returns the contents. Is this correct?
If so, something like:
my $filedata = myfunction();
if (defined $filedata) {
print FILEHANDLE $filedata;
} else {
# handle error...
}
P.S. Showing some or all of myfunction() would really help us help you. | [reply] [d/l] |
|
|
I think you are confusing the meaning of copying filehandles with that of copying the contents from one file to another.
I am guessing that it is the latter that you really want. I assume that myfunction returns either an open read handle or undef. In this case, you can do something like
my $in = myfunction();
if ( $in ) {
print FILEHANDLE while <$in>;
}
else {
# handle null-handle error;
}
| [reply] [d/l] |
|
|
|
|
|
|
| [reply] [d/l] |
Re: return undef
by Fletch (Bishop) on May 23, 2005 at 02:34 UTC
|
Erm, so check if the function is returning undef or not.
print FILEHANDLE myfunction() || "";
| [reply] [d/l] |
|
|
this works but the whole file does not get copied for some reason. any ideas?
| [reply] |
Re: return undef
by gaal (Parson) on May 23, 2005 at 04:29 UTC
|
Consider raising an exception (with die) when the error happens. Using the return value of a sub for both data and error signalling tends to always have this problem: using exceptions makes error conditions out-of-band and lets you treat the error when it's most convenient for you.
I was about to give an example, but then I realized I don't understand what myfunction() is doing and who opens FILEHANDLE. | [reply] [d/l] [select] |
|
|
Another useful feature of raising exceptions is that you can pass along the actual error message, which makes debugging much easier.
open $fh, ">$filename"
or die "could not open '$filename' for writing: $! "
Also, if you do not write code to catch the exception, your program will terminate with a more meaningful message than "Use of uninitialized value at ... ". | [reply] [d/l] |
|
|
die now takes more than just strings. You can use first-class objects if you like. This lets you add more structured information about the exception, which also makes catch/propagate decisions less kludgy to implement.
| [reply] |