0

My basic question is this. If I pass no parameters to my package -- DbIoFunc's routine OpenIcsDB() why is the pacakge name in @_?

I am expecting @_ to be null when I pass no parameters to the following function, but instead the parameter contains the package name. I have tried calling with class -> and :: syntax, and there is no difference.

What should I test to determine if no parameter was passed other than the following?

my ($Temp, $DBPathLocal) = @_; 

if(!defined $DBPathLocal)
{
    $DBPathLocal = DBDEV;
}

I'm wondering two things. Why is the package name part of @_ and is what I've done the best way to get rid of the package name?

Here is the call:

my ($DBHand);
$DBHand = DbIoFunc->OpenIcsDb();

Here is the function:

sub OpenIcsDb
#
# DB Path Constant DBPROD or DBDEV.
#
{
    # Path passed in, either DBPROD or DBDEV
    my ($DBPathLocal); 

    my ($DBSuffix);
    my ($DBHand);      # ICS database handle

    $DBPathLocal = shift;

    #
    # Make sure the database path exists.
    #

    if (length($DBPathLocal) <= 0)
    {
        if (!$Debugging)
        {
            $DBPathLocal= DBPROD;
        }
        else
        {
            $DBPathLocal = DBDEV;
        }
    }
    else
    {
        $DBPathLocal = DBDEV;
    }

    my $DBSuffix = DBSUFFIX;

    $! = 2;
    die("Can't find database directory ".$DBPathLocal.".".$DBSuffix)
    unless ((-e $DBPathLocal.".".$DBSuffix) && (-d $DBPathLocal.".".$DBSuffix));
    #
    # See if we can connect to the ICS database.  We can't proceed without it.
    #
    $DBHand = DBI->connect("dbi:Informix:".$DBPathLocal, "", "")
        or die("Can't connect to the ICS database ".$DBPathLocal);

    return $DBHand;
}
octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131

3 Answers3

1

You probably want to call the function as DbIoFunc::OpenIcsDb(). Using the arrow operator does some object stuff.

AKHolland
  • 4,435
  • 23
  • 35
  • 1
    Or if it's supposed to be a "class method", skip or verify the classname argument. – aschepler Jul 03 '13 at 19:32
  • It's a method in a package. – octopusgrabbus Jul 03 '13 at 19:40
  • @aschepler I know this is perl and TIMTOWTDI, but I would assume that if he meant to be calling class methods there would be a "new" in here somewhere. – AKHolland Jul 03 '13 at 19:43
  • 1
    @AKHolland In perl, "new" is only useful for calling a method which is actually named "new". – aschepler Jul 03 '13 at 19:58
  • @aschepler I'm aware of that, hence the disclaimer at the beginning of my previous comment. However, **in general**, if someone meant to be using object methods they would create an object with `new` first. – AKHolland Jul 03 '13 at 20:28
  • I unaccepted this, because $DBPathLocal still contains DbIoFunc, and I've deleted my answer, because it was incorrect, too. – octopusgrabbus Jul 03 '13 at 20:58
  • @AKHolland: but it was being called as a class method, not an object method. – ysth Jul 03 '13 at 22:14
1

I have tried calling with class -> and :: syntax, and there is no difference.

There is a difference. If you use :: and pass no other parameters then @_ will be empty. You can confirm this fact by inserting the following code at the top of the function:

print '@_ contains ' . scalar(@_) . " elements\n";

Your real problem might be here:

$DBPathLocal = shift;
if (length($DBPathLocal) <= 0)

If @_ is empty then $DBPathLocal will be undef. And length(undef) is always undef. And undef <= 0 is always true.

Protip:

use warnings;
use strict;
Oktalist
  • 14,336
  • 3
  • 43
  • 63
  • Sorry. @_ was not empty, when I passed no parameters. This is perl, v5.10.1 (*) built for i386-linux-thread-multi – octopusgrabbus Jul 03 '13 at 21:46
  • @octopusgrabbus I edited my answer. Try inserting the `print` line I added and see what it says. – Oktalist Jul 03 '13 at 21:49
  • What I've decided to do is throw away the Package name in a scalar variable. Most of our Perl code uses `->` syntax, and I want to keep using it for continuity. – octopusgrabbus Jul 03 '13 at 21:53
1

To do OOP, Perl has to know the package name or the object's reference to work. If you use the :: call method, you avoid this.

package Foo;

sub bar {
  print "@_\n";
}

package main;

Foo->bar();
Foo::bar();
bar Foo();

Note that the call to Foo::bar(); does not print the package name.

shawnhcorey
  • 3,545
  • 1
  • 15
  • 17
  • Thanks. I'll check this out after the holiday weekend. I'll go see if the package name prints. I'm inclined to use class syntax, given that's used all over our Perl code -- there is quite a bit of it -- and I like continuity. I've gone for a package name throw away concept that I put in the edited OP. – octopusgrabbus Jul 04 '13 at 14:22