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

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

shallymeister
Novice

Mar 13, 2012, 10:14 AM

Post #1 of 7 (1486 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 (1483 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 (1471 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 (1452 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 (1450 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 (1432 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 (1422 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.

 Announcements     PerlGuru Announcements Perl Programming Help     Frequently Asked Questions     Beginner     Intermediate     Advanced     Regular Expressions     mod_perl     DBI     Win32 Programming Help Fun With Perl     Perl Quizzes - Learn Perl the Fun Way     Perl Golf     Perl Poetry Need a Custom or Prewritten Perl Program?     I need a program that...     I Need a Programmer for Freelance Work     Throw Down The Gauntlet General Discussions     General Questions     Feedback     Tutorial/Article Suggestions for The Learning Cent     Internet Security Other Programming Languages     Javascript     PHP

 Search this forum this category all forums for All words Any words Whole Phrase (options) Powered by Gossamer Forum v.1.2.0