943,723 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 4481
  • Perl RSS
Nov 21st, 2008
0

Call perl script with a variable

Expand Post »
Although it sounds pretty simple, i haven't found a proper answer for this issue.

I want to know how can i call a perl script and pass a variable to it.
For example, lets say "Parse.pl" is a parsing script of some sort and i want to call it from another perl script, or from a command line, with a variable: Parse($line);
Parse.pl will get the variable $line, perform parsing on it and return it to the main script (the return part is not really important);

Any idea how its done?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yair7190 is offline Offline
17 posts
since Dec 2007
Nov 25th, 2008
0

Re: Call perl script with a variable

Hi,
I would recommend you to define a method in Parse.pl and use it in your client script which requires parsing. For eg.
#caller.pl
require 'Parse.pl';
#...
# call method parse_me(for eg.) in Parse.pl
my $return_val = Parse::parse_me($line);
#...
#Parse.pl
sub parse_me{
my $line = @_;
#...
}
1;
Note: In Parse.pl define only methods, because when you require something, perl will try to execute that script first and then subsequent lines following require line.

hope this helps.

katharnakh.
Reputation Points: 19
Solved Threads: 34
Posting Whiz in Training
katharnakh is offline Offline
237 posts
since Jan 2006
Feb 6th, 2009
0

Re: Call perl script with a variable

You can use Getopt. Here is the link:

http://perldoc.perl.org/Getopt/Long.html
Reputation Points: 72
Solved Threads: 26
Posting Whiz in Training
GDICommander is offline Offline
209 posts
since Jun 2008
Feb 20th, 2009
0

Re: Call perl script with a variable

Click to Expand / Collapse  Quote originally posted by yair7190 ...
Although it sounds pretty simple, i haven't found a proper answer for this issue.

I want to know how can i call a perl script and pass a variable to it.
For example, lets say "Parse.pl" is a parsing script of some sort and i want to call it from another perl script, or from a command line, with a variable: Parse($line);
Parse.pl will get the variable $line, perform parsing on it and return it to the main script (the return part is not really important);

Any idea how its done?
If you execute your script at ~>Parse.pl a b c
you get the command line values in the @ARGV array which you can run through in a loop.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bryansmith123 is offline Offline
10 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: Help - Install XML-LibXML on windows
Next Thread in Perl Forum Timeline: perl assignment question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC