reg function in perl

S

sivga123

hello all iam new to perl this might sound basic..
this my program.ark is a perl module file located in same directory and
it has got a function chkdir which returns a value .how doi call that
function

use ark ;

sub gotdir {


// is this right way of invocation and passing param to function
@listofdir =chkdir ark(values);

or is this a right way
@listofdir = ark::chkdir(values);
}

main .....{
}

thanks for th ehelp
 
S

sivga123

another one is when i try to do

if $listofdir[0] it gives as ark it module name so to get the value
returned i have to do $listofdir[1]
 
M

Matt Garrish

hello all iam new to perl this might sound basic..
this my program.ark is a perl module file located in same directory and
it has got a function chkdir which returns a value .how doi call that
function

use ark ;

sub gotdir {


// is this right way of invocation and passing param to function
@listofdir =chkdir ark(values);

or is this a right way
@listofdir = ark::chkdir(values);
}

When you ask which one will work, and of the two options only one will, then
why ask at all? Try running the code and you'll quickly discover which is
right. Unless you're exporting the functions you must prefix them by the
module name. What led you to believe the first one would work? Have you had
a skim through perlmod?

Please note that it's not good style to create module with all lowercase
letters; those are generally reserved for pragmas. And if you're going to
ask a question, please post real code, not pseudo-code (// does not
designate a comment in Perl, among other things wrong with what you posted).

Matt
 
M

Matt Garrish

another one is when i try to do

if $listofdir[0] it gives as ark it module name so to get the value
returned i have to do $listofdir[1]

Another annoyance of gmail posters is that they don't quote context. Usenet
is not a bulletin board. Don't assume that anyone who reads your posts will
have seen anything you wrote before.

You've posted no code, so I have no idea what you are talking about. If you
don't want to return the class name, then don't return it. I doubt very much
that it magically appears in the return values from your function. It would
be perfectly normal for it to be in the values passed to a subroutine when
called via an object, but it's impossible to guess if that's really what you
mean.

Matt
 
S

sivga123

thanks matt for thehelp but myreal code is pretty long so i could not
post i t..but my problem is first one works i dont know why and
second one is not working . .myquestion is how is first one working and
wat is the right way of invocation a function which is in a module.

thanks for the help
 
M

Michael Goerz

thanks matt for thehelp but myreal code is pretty long so i could not
post i t..but my problem is first one works i dont know why and
second one is not working . .myquestion is how is first one working and
wat is the right way of invocation a function which is in a module.

thanks for the help

While you're learning to quote, you might also want to learn how to
spell/capitalize correctly!

http://cfaj.freeshell.org/google/
 
A

A. Sinan Unur

(e-mail address removed) wrote in @z34g2000cwc.googlegroups.com:
thanks matt for thehelp but myreal code is pretty long so i could not
post i t..but my problem is first one works i dont know why and
second one is not working .

What do you mean by "first one", "second one"?

Please post a short but complete script that illustrates the problem you
are encountering.

Please read the posting guidelines for this group.

Sinan
--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
S

sivga123

## this is ark_get.pm file
# use strict;
package ark_get;

sub check_sube () {
my @input = @_;
my @output =()
my @ignore = qw(
Init:primaryipaddress
Init:secondaryipaddress
ATC:primary_address
);

foreach ( @input ) {
$firstline = $_;
foreach ( @ignore ) {
($fea1, $na1) = split /:/, $_;
$lin =~ s/$fea1:\\BD-.*\\KCSS-.*\\.*={$nam1=.*?}//g;
}
push @output, $lin;

}
return @output;

}

##### my main program
#!/usr/bin/perl
use ark_get;


sub new_output {
## this is the place i got struck
# when i use this way of invocation it gets value from the function
@newChanges = check_sube ark_get(@firstchanges);
# when i run it this way it throws an error saying too many arguments
...
@newChanges = check_sube::ark_get(@firstChanges);

#but when i get rid of all the modules and plug the function from
module in the main program it works .it does not complain abt too many
arguments
@newChanges = &ark_get(@firstChanges);

if ($newChanges[1] =~ /^{}/ || $newChanges[1] =~ /{,.+}/){


}

iam new to perl and iam finding difficult to interpret wat's wrong here
...
and sorry for my prev posts.
thanks for all the help .

thanks
 
M

Matt Garrish

## this is ark_get.pm file
# use strict;
package ark_get;

sub check_sube () {

That's not how you write a subroutine declaration in Perl, unless you really
want to use prototyping. You're telling perl that you don't want any
arguments passed to the subroutine, which is why you get the warning later
when you try to pass the array. You would normally write the above as:

sub check_sube {

or if you really want prototyping and to pass an array of values:

sub check_sube(@) {
my @input = @_;
my @output =()

Missing a semicolon at the end of the line. Please post code that can be
run. The empty parens are also not necessary when declaring an array.
my @ignore = qw(
Init:primaryipaddress
Init:secondaryipaddress
ATC:primary_address
);

foreach ( @input ) {
$firstline = $_;

The above variable is not just going to be assigned the first line, so a bad
choice of names. You should also do the assignment when you declare the
loop:

foreach my $line (@input) {
foreach ( @ignore ) {
($fea1, $na1) = split /:/, $_;
$lin =~ s/$fea1:\\BD-.*\\KCSS-.*\\.*={$nam1=.*?}//g;
}

Again, post code that we can run. What is $nam1? What is $lin?
push @output, $lin;

}
return @output;

}

##### my main program
#!/usr/bin/perl
use ark_get;


sub new_output {
## this is the place i got struck
# when i use this way of invocation it gets value from the function
@newChanges = check_sube ark_get(@firstchanges);

Because you can't call subroutines that way, as I alluded to earlier. You
have things reversed, and if you had warnings enabled you would have
discovered this long ago:

Can't locate object method "ark_get" via package "check_sube" (perhaps you
forgo
t to load "check_sube"?) at test.pl line 15.

Always start your scripts with:

use strict;
use warnings;

Especially when you are still learning!
# when i run it this way it throws an error saying too many arguments
..
@newChanges = check_sube::ark_get(@firstChanges);

Again, had you turned on warnings:

Undefined subroutine &main::ark_get called at test.pl line 21.

You specify the function after the package name:

@newChanges = ark_get::check_sube(@firstChanges);

And once again, do not use all lowercase letters for your module names.
ArkGet or even Ark_get are better options.

It would strongly benefit you to read perlmod as I mentioned earlier, and
not just skim it.

Matt
 
M

Matt Garrish

Matt Garrish said:
Again, had you turned on warnings:

Undefined subroutine &main::ark_get called at test.pl line 21.

Oops, bad copy and paste on my part; that was for the next bad subroutine
call. The actual error is:

Undefined subroutine &check_sube::ark_get called at test.pl line 17.

I should have mentioned not to call subroutines using the &myfunc() syntax
unless you know why you would want to prefix the '&'.

Matt
 
T

Tad McClellan

## this is ark_get.pm file
# use strict;


You lose all of the benefits of use strict when you comment it out.

You should choose to KEEP the benefits of use strict.

package ark_get;

sub check_sube () {
^^
^^ do you know what this means?

Here you say that check_sube takes NO arguments.

my @input = @_;


Here you indicate that check_sube DOES take arguments.

As a beginner (and probably also as an adept) you should not
use prototypes.

my @output =()
my @ignore = qw(
Init:primaryipaddress
Init:secondaryipaddress
ATC:primary_address
);

foreach ( @input ) {
$firstline = $_;


If you want it in $firstline, then put it in there straightaway
rather than putting it where you don't want it and then copying it
from where you don't want it to where you do want it:


foreach my $firstline ( @input ) {

foreach ( @ignore ) {
($fea1, $na1) = split /:/, $_;
$lin =~ s/$fea1:\\BD-.*\\KCSS-.*\\.*={$nam1=.*?}//g;


You have never put any value into the $lin variable, so this
statement will not do much, other than generate a warning.

(you DO have warnings enabled, don't you?)

}
push @output, $lin;

}
return @output;

}

##### my main program
#!/usr/bin/perl
use ark_get;


Lower case module names are for pragmas.

You should not use lower case module names for your regular modules.

# when i use this way of invocation it gets value from the function
@newChanges = check_sube ark_get(@firstchanges);


That is a little-used way of calling functions.

It is documented in the "Indirect Object Syntax" section in:

perldoc perlobj

It is widely recommended that you do not use the indirect object syntax.

# when i run it this way it throws an error saying too many arguments
..
@newChanges = check_sube::ark_get(@firstChanges);


That is (intended to be) the more common way of calling a function.

But this most common way has the package name 1st and the
function name 2nd.

You have them reversed.

@newChanges = ark_get::check_sube(@firstChanges);
 
M

Matt Garrish

Tad McClellan said:
That is a little-used way of calling functions.

It is documented in the "Indirect Object Syntax" section in:

perldoc perlobj

It is widely recommended that you do not use the indirect object syntax.

Interesting. I don't remember running across that before. Probably one of
those things I never could see any point in doing and dropped from memory.
My apologies to the OP for thinking he was on crack... : )

(I was copying the errors from the command line after running a whole batch
of tests on his code to see what he might have been trying, and I presume
the error I erroneously posted was from having reversed the above to
"ark_get check_sube(@firstchanges)").

Matt
 

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

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top