CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Coldfusion passing a variable to perl script

 



shallymeister
Novice

Mar 13, 2012, 10:14 AM

Post #1 of 7 (1221 views)
Coldfusion passing a variable to perl script Can't Post

Hello, I have a file path variable that I need to get from coldfusion to a perl script that requires the file path of a text file that I manipulate.

So I have a coldfusion variable called #filepath# which is the actual location of text files that I want perl to manipulate. In a perl globals file I have a variable $filepath to accept the location of the text files sent from coldfusion that I use to read the text file into an array

@
fileContent = readFile("FILE", $filepath);



What is the best way to accomplish this task? I hope I explained it well enough to understand.

Thanks


naven8
Novice

Mar 13, 2012, 11:52 AM

Post #2 of 7 (1218 views)
Re: [shallymeister] Coldfusion passing a variable to perl script [In reply to] Can't Post

Use following to open and read file.


Code
open(FH,"$filename") || die "Unable to open file $!";   #read the file 
my @content = <FH>;


You can also use : open (FH,"<","$filename") || die "Unable to open file $!";


(This post was edited by naven8 on Mar 13, 2012, 11:58 AM)


FishMonger
Veteran / Moderator

Mar 13, 2012, 2:04 PM

Post #3 of 7 (1206 views)
Re: [shallymeister] Coldfusion passing a variable to perl script [In reply to] Can't Post

Slurping the file into an array is almost always the wrong approach, but if needed, it would be best to use the File::Slurp module.


Code
use File::Slurp; 

my $filename = $ARGV[0]; # I'm assuming that you're passing the filename to the scipt

my @file_contents = read_file( $filename );


If you open a filehandle and read the file line-by-line, then use the 3 arg form of open and a lexical var for the filehandle and don't quote single vars, like naven did.


Code
open my $fh, '<', $filename or die "failed to open '$filename' <$!>"; 

while ( my $line = <$fh> ) {
# process the line as needed
}



shallymeister
Novice

Mar 14, 2012, 5:57 AM

Post #4 of 7 (1187 views)
Re: [FishMonger] Coldfusion passing a variable to perl script [In reply to] Can't Post

I can get the file read in using perl, what I am having trouble with is getting the path and filename from coldfusion to the perl script. Basically I have a website with a menu system that the user selects certain parameters from database queries, once the final menu selection is made it determines which of the text files to go to and stores the path in a coldfusion variable which includes the path on the server and the actual filename of the text file ie.... d:\webdata\text_files\test.txt gets stored in a coldfusion variable called filepath. My perl script needs this path to determine which text file to read in. I need to know the best method to get the coldfusion variable filepath to the perl script.



Thanks


FishMonger
Veteran / Moderator

Mar 14, 2012, 6:30 AM

Post #5 of 7 (1185 views)
Re: [shallymeister] Coldfusion passing a variable to perl script [In reply to] Can't Post


Quote
I need to know the best method to get the coldfusion variable filepath to the perl script.


That's a question best answered on a coldfusion forum. However, the most common method is to have your coldfusion script call the perl script and pass the required vars as arguments to the perl script. Those args are then found/accessed via Perl's @ARGV array.


naven8
Novice

Mar 15, 2012, 1:29 AM

Post #6 of 7 (1167 views)
Re: [FishMonger] Coldfusion passing a variable to perl script [In reply to] Can't Post

Hi FishMonger,
Please let me know the advantage of the following code.

Code
use File::Slurp;  

my $filename = $ARGV[0]; # I'm assuming that you're passing the filename to the scipt

my @file_contents = read_file( $filename );


Why not @file_contents = <FH>; ?


FishMonger
Veteran / Moderator

Mar 15, 2012, 7:02 AM

Post #7 of 7 (1157 views)
Re: [naven8] Coldfusion passing a variable to perl script [In reply to] Can't Post


Quote
Why not @file_contents = <FH>;

1) FH is a (global) bareword filehandle, which is outdated and needlessly alters the symbol table.

2) It requires you to open the filehandle yourself, which you may or may not be doing properly.

3) The File::Slurp module handles the creation of the lexical filehandle, which does not clutter up the symbol table like the golbal bareword handle and it does it properly.

4) The read_file() method used in the module is context aware and will return either a scalar, an array or an array ref.

5) The methods used in the module (such as sysopen/sysread) to read-in the file contents are more efficient (i.e., faster).

6) You can pass config options in the function call to alter some of its functionality.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives