sub in a package with parameters

R

Roman

Hello guys

I have this code:
_______________________________________________________________________
package RWToDatasotre;

use strict;
use Config::Simple;

sub ReadParameter {
my($none, $file, $categorie, $parameter) = @_;

my $cfg;
my $param;
my $returnparam;

#Create instance on File with name
$cfg = new Config::Simple("$file") or die Config::Simple->error();

#Open File
$cfg = new Config::Simple();
$cfg->read("$file") or die $cfg->error();

#Reading value in File
$param = $cfg->param("$categorie.$parameter") or die $cfg->error();

$returnparam = $param;
}
_______________________________________________________________________


I Call the sub ReadParameter like this!

_______________________________________________________________________
my $testcategorie = "GeneralSection";
my $testparameter = "Testparam";
my $newtestparameter = "Just to say a hello! ;-)";

my( $ReadConfigFile) = RWToDatasotre->ReadParameter(
$MainApplicationfile, $testcategorie, $testparameter);
_______________________________________________________________________


Why do I have to have to define one more parameter in the package then
defined in the streing where i call the string?

Thanks to help me

Roman
 
J

Josef Moellers

Roman said:
Hello guys

I have this code:
_______________________________________________________________________
package RWToDatasotre;

use strict;
use Config::Simple;

sub ReadParameter {
my($none, $file, $categorie, $parameter) = @_;

my $cfg;
my $param;
my $returnparam;

#Create instance on File with name
$cfg = new Config::Simple("$file") or die Config::Simple->error();

#Open File
$cfg = new Config::Simple();
$cfg->read("$file") or die $cfg->error();

#Reading value in File
$param = $cfg->param("$categorie.$parameter") or die $cfg->error();

$returnparam = $param;
}
_______________________________________________________________________


I Call the sub ReadParameter like this!

_______________________________________________________________________
my $testcategorie = "GeneralSection";
my $testparameter = "Testparam";
my $newtestparameter = "Just to say a hello! ;-)";

my( $ReadConfigFile) = RWToDatasotre->ReadParameter(
$MainApplicationfile, $testcategorie, $testparameter);
_______________________________________________________________________


Why do I have to have to define one more parameter in the package then
defined in the streing where i call the string?

Because perl treats this as an object reference and passes the object as
the first argument.
BTW if you want to ignore the argument, you can replace $none by undef.
 
R

Roman

Josef said:
Because perl treats this as an object reference and passes the object as
the first argument.
BTW if you want to ignore the argument, you can replace $none by undef.
Thanks Josef
 
A

Anno Siegel

Roman said:
Hello guys

I have this code:
_______________________________________________________________________
package RWToDatasotre;

use strict;
use Config::Simple;

sub ReadParameter {
my($none, $file, $categorie, $parameter) = @_;
[...]

}
_______________________________________________________________________


I Call the sub ReadParameter like this!

_______________________________________________________________________
my $testcategorie = "GeneralSection";
my $testparameter = "Testparam";
my $newtestparameter = "Just to say a hello! ;-)";

my( $ReadConfigFile) = RWToDatasotre->ReadParameter(
$MainApplicationfile, $testcategorie, $testparameter);
_______________________________________________________________________


Why do I have to have to define one more parameter in the package then
defined in the streing where i call the string?

That's how method calls work. The first element of @_ is whatever the
method was called through (an object or, in your case, a class name).
The following elements are the arguments that were given inside the
parentheses in the method call. Read "perldoc perlobj", paying special
attention to the section "A Method is Simply a Subroutine".

Anno
 
J

Joe Smith

Roman said:
sub ReadParameter {
my($none, $file, $categorie, $parameter) = @_;

I would write that as

my $self = shift;
my($file,$category,$parameter) = @_;

so that the object-oriented interface is explicit.
-Joe
 
D

Dave Weaver

sub ReadParameter {
my($none, $file, $categorie, $parameter) = @_;
my( $ReadConfigFile) = RWToDatasotre->ReadParameter(
$MainApplicationfile, $testcategorie, $testparameter);
Why do I have to have to define one more parameter in the package then
defined in the streing where i call the string?


If ReadParameter is a class method, rather than an instance method
(i.e. it needs no direct reference to any object of the RWToDatasotre
package, as it appears in your case), then you can define it like this:

sub ReadParameter {
my( $file, $categorie, $parameter) = @_;
...

and call it like this:

RWToDatasotre::ReadParameter( $MainApplicationfile,
$testcategorie, $testparameter );

^^
Note!

If, on the other hand, it requires access to an object of the
RWToDatasotre class, you would define it something like:

sub ReadParameter {
my( $self, $file, $categorie, $parameter) = @_;
,,,


and call it like this:
$RWToDatasotre_obj->ReadParameter( $MainApplicationfile,
$testcategorie, $testparameter );

^^^
this 'object' goes into the $self variable
 
X

xhoster

Roman said:
_______________________________________________________________________
package RWToDatasotre; ....
sub ReadParameter {
my($none, $file, $categorie, $parameter) = @_; ....


my( $ReadConfigFile) = RWToDatasotre->ReadParameter(
$MainApplicationfile, $testcategorie, $testparameter);
_______________________________________________________________________

Why do I have to have to define one more parameter in the package then
defined in the streing where i call the string?

You don't, it is just that one of the paramaters looks funny.

RWToDatasotre->ReadParameter($MApp, $cat, $para);
^parm1^ ^parm2^ ^parm3^ ^parm4^


That is how OO works.


Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top