in reply to How do I include a file and execute the code in the file?
Needs full path to code to be run.# Useage example: my $error = run_code('/file1.pl'); print "Run code failed, err msg:\n$error" if $error; sub run_code { my $path = shift; open(CODE, $path) || return "Oops: $path $!\n"; local $/; my $code = <CODE>; eval $code; return $@; }
The local $/ gives a locally undefined value to the input record seperator $/ with the result that we glob the whole file into $code. This saves us from @code = <CODE>; $code= join'',@code;
The sub returns undef if it suceeds or the error message if it fails to run the code.
Hope this helps.
tachyon
|
|---|