Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How do I pass values from a .pl to a .pm.

Reply
Thread Tools

How do I pass values from a .pl to a .pm.

 
 
Ironhide
Guest
Posts: n/a
 
      04-01-2011
With the code below how do I send the value of $value_to_pass to A.pm,
so that sub-routines in A.pm can use them?

A.pm
====

package A;

.....
.....
Do something with the value that I got from the perl script

test.pl
=====
use A;

my $value_to_pass= get_value(...);

sub get_value{
.....
.....
return $value
}

~hanks for your help in advance
_gourab

 
Reply With Quote
 
 
 
 
RedGrittyBrick
Guest
Posts: n/a
 
      04-01-2011
On 01/04/2011 07:50, Ironhide wrote:
> With the code below how do I send the value of $value_to_pass to A.pm,
> so that sub-routines in A.pm can use them?
>
> A.pm
> ====
>
> package A;
>
> ....
> ....
> Do something with the value that I got from the perl script
>
> test.pl
> =====
> use A;
>
> my $value_to_pass= get_value(...);
>
> sub get_value{
> ....
> ....
> return $value
> }
>


See answers from Henry Law & Tad McClellan

In addition, what you might want to do is make A.pm be object-oriented.
Read `perldoc perltoot`

Then test.pl can do

#!/usr/bin/perl
use strict;
use warnings;
use A;

#
# "send the value of $value_to_pass to A.pm"
#
my $value_to_pass = 42;
my $a = A->new($value_to_pass);

#
# "sub-routines in A.pm can use [it]"
#
$a->foo();
$a->bar();

See the tutorial I referred to for details.

--
RGB
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      04-02-2011
Ironhide <(E-Mail Removed)> wrote:
>With the code below how do I send the value of $value_to_pass to A.pm,
>so that sub-routines in A.pm can use them?
>
>A.pm
>====
>
>package A;
>
>....
>....
>Do something with the value that I got from the perl script


sub Do_something_with_the_value_that_I_got_from_the_pe rl_script {
my $value = shift;
......
}

>
>test.pl
>=====
>use A;
>
>my $value_to_pass= get_value(...);


Do_something_with_the_value_that_I_got_from_the_pe rl_script($value_to_pass);

>sub get_value{
>....
>....
>return $value
>}
>
>~hanks for your help in advance
>_gourab

 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      04-02-2011
RedGrittyBrick <(E-Mail Removed)> wrote:
>On 01/04/2011 07:50, Ironhide wrote:
>> With the code below how do I send the value of $value_to_pass to A.pm,
>> so that sub-routines in A.pm can use them?

>
>In addition, what you might want to do is make A.pm be object-oriented.


Or at the very least if you prefer a procedural interface have a
function "set()" to not break the data abstraction that the module
provides:

A::set($value_to_pass)

jue
 
Reply With Quote
 
ccc31807
Guest
Posts: n/a
 
      04-03-2011
On Apr 1, 1:50 am, Ironhide <(E-Mail Removed)> wrote:
> With the code below how do I send the value of $value_to_pass to A.pm,
> so that sub-routines in A.pm can use them?


Why would you want to do that? A module essentially contains common
code that other code can call. For example, if you had a need to
configure the environment for several scripts, say to set $username,
$password, and $ipaddress, so that the script could communicate with
some server, you might want to put the configuration functionality in
a module, like this:

package commoncode;
sub configure
{
my $script = shift;
my %CONFIG;
# open configuration file and set variables
return \%CONFIG;
}

while in your script that needs to be configured, you might do this:

use commoncode;
my $CONFIG = commoncode::configure('FTPscript');
my $username = $CONFIG->{username};
.... and so on.

The point is that your module contains code that you use in your
scripts, and you pass and return values to that code in the same way
you would as if the code were in the script itself.

I may be dumb, dumber, and dumbest, but I can't see how your question
makes any sense.

CC.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
difference between pass by address and pass by reference!! blufox C Programming 2 04-03-2006 02:53 PM
Pass by reference / pass by value Jerry Java 20 09-09-2005 06:08 PM
pass by ref vs pass by pointer Radde C++ 38 08-23-2005 06:29 PM
Pass-by-reference instead of pass-by-pointer = a bad idea? Mr A C++ 111 07-14-2005 03:04 AM



Advertisments