barrycarlyon has asked for the wisdom of the Perl Monks concerning the following question:

If I have a function that I am going to call more than once

Is the following syntax correct?

Pseudo code-:

$foo = where function is ( send out value)

Real code:

$foo = Whaterverlib::pmfile->_nameoffunction($sent);

so in fucntion _nameoffucntion

sub _nameoffucntion { my $self = shift; my $recieved = shift; some function return $value; }

so $foo is (return) $value, and $recieved is $sent

yours

update: Thanks for every ones help!

Update: Turns out that it worked intially but I have an If in the _whatever which was not affected my returned value which was in the else section but the then was true! Useful help though!

Barry Carlyon barry@barrycarlyon.co.uk

Replies are listed 'Best First'.
Re: Global Functions? sent and returned values
by Tanktalus (Canon) on Jun 20, 2006 at 18:39 UTC

    I'm not entirely clear what you're trying to do - there are approximately 2 different call styles in perl. And they all have their nuances, none of which are necessarily completely obvious to the casual newbie.

    Style 1: Direct calling

    This is where you call a function directly. There is no runtime lookup of where a function is, which gains some speed, at the expense of flexibility (which, to be honest, is not wanted most of the time anyway).

    There are two variations here. The first variation is what you're used to: some_func(). The nuance? That some_func may be declared in the same file as where you're calling it, or it may be exported there from another package (or namespace). Note that even if it's exported, Perl only looks in the current namespace or package to find it - the act of exporting merely makes a copy of the function from the originating namespace/package in to your namespace/package. No extra parameters are used:

    my $returned = some_func($sent); # ... sub some_func { my $received = shift; # ... $value; }
    Advantages: no extra parameters, very simple, you can actually get the same function name from a new module if you decide to move things around, and not change anything but the use statement. Disadvantages: you may not always be acutely aware of where functions are coming from (which is why some would encourage you to make your imports explicit in your use statements).

    The second variation, which I think is what you're looking for, is fully-qualified naming. It's the same as the previous one, but is longer-winded.

    my $returned = Foo::Bar::some_func($sent); # in Foo::Bar: sub some_func { my $received = shift; #... $value; }
    Advantages: You can call functions that aren't even exported. Disadvantages: if the function gets moved to a new module, you need to change all the calls. Also, if it's not being exported, you probably aren't supposed to call it anyway - caveat emptor!

    Style 2: Indirect calling

    Here we tell Perl where to start looking. It may decide to look elsewhere after that if it can't find the function you're asking for. This can chew up a few cycles just resolving the function call. You can distinguish this style from the previous one by the fact that the arrow notation is used.

    The first variation is class-indirect. This is used primarily for constructors and by File::Spec. If you don't know why File::Spec does it this way, you probably don't want to do it yourself - it's somewhat advanced, IMO. And I've never needed to do it, either, so it's a good bet you probably don't either.

    my $returned = Foo::Bar->some_func($sent); # in Foo::Bar (or any package that Foo::Bar derives from) sub some_func { my $class_name = shift; # will be Foo::Bar, even if we aren't in t +hat package right now my $received = shift; # ... $value }
    Advantages: being able to crawl up the @ISA inheritence tree to find a method. Disadvantages: can be quite confusing until you really understand it.

    The other variation is object-indirect. This is where you have an object created via a constructor, and then call methods on it. This is a very popular style as it is what allows us to call perl "Object Oriented" (as much as, say, C++ or Java are, but not as much as other languages such as Smalltalk). Perl starts with the package of the object given, and walks up the @ISA inheritance tree the same as before if it can't find the method in the starting package.

    my $returend = $obj->some_func($sent); # in the package that $obj is blessed into, or any package it is der +ived from sub some_func { my $self = shift; my $received = shift; #... $value; }
    Advantages: see OOP (Object Oriented Programming). Disadvantages: Same ;-)

Re: Global Functions? sent and returned values
by Joost (Canon) on Jun 20, 2006 at 18:35 UTC
    Functions defined with a name (using sub function_name {  ... } syntax are always global. That means, they have a name in a package and can be called from anywhere else.

    Named subs are also used as class or object methods. The only difference between a method and a named subroutine is the first argument passed: if you call methods on an object the first argument passed will be the object and if you call a method on a class the first argument will be that class (i.e. the name of the package).

    package MyPackage; sub new { return bless {},shift; # create an empty object of class MyPackage } sub called { my (@args) = @_; print "called with args @args\n"; } package main; MyPackage::called(1,2,3); # call as a normal subroutine MyPackage->called(1,2,3); # call as a class method my $object = MyPackage->new(); # create object $object->called(1,2,3); # call as an object method.
    See also perlsub and perltoot.

    Update: and subroutines always return whatever is passed to return() or failing that, the value of the last expression.

Re: Global Functions? sent and returned values
by Zaxo (Archbishop) on Jun 20, 2006 at 18:39 UTC

    What you show is typical for class methods, though $_[0] would ordinarily be called $class or $stash when it's shifted in.

    Usually the leading underscore signifies an internal function which should not be called from other namespaces. That is a convention, Perl does nothing to enforce it.

    If you want an ordinary function, not a class method, leave out the class name shift and call like this:

    $foo = Whaterverlib::pmfile::nameoffunction($sent);
    with nameoffunction written as,
    sub nameoffucntion { my $recieved = shift; my $value = whatever; return $value; }

    In general, dereference arrow notation puts an extra argument in @_ - either the name of the class, or a reference to the instance which called the object method.

    After Compline,
    Zaxo

Re: Global Functions? sent and returned values
by kwaping (Priest) on Jun 20, 2006 at 18:34 UTC
    Your post is a little confusing to me, but I believe your concepts and code are correct.

    ---
    It's all fine and dandy until someone has to look at the code.