perl query (59 Views)
Reply
Honored Contributor
steven Burgess_2
Posts: 1,493
Registered: ‎11-21-2001
Message 1 of 6 (59 Views)
Accepted Solution

perl query

Hi everyone

I am trying to pass the the following into a sub routine

&insert_audit("$hostid successfully $hostname");

the subroutine is used to pass the data into a MySQL database via perl::DBD module. I appear to have a problem though when trying to create an array from the pass to the sub routine

sub insert_audit ()

{

my (@record)=shift (@_);
print "@record\n";
}

The above is ok, ie (1 line of output below)

48 successfully rs-demo8

If I try to print an element of the array. The statement is as follows

sub insert_audit ()

{

my (@record)=(@_);
print "@record\n";
print "$record[1]";

}

Use of uninitialized value in join or string at configUpload.pl line 49

Which suggests the attempt to create the array from the parameters passed to the sub routine fails ?

Am I doing something wrong ?

TIA

Steve
take your time and think things through
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 6 (59 Views)

Re: perl query

Hi Steve:

Yes, one or more of the elements is the array passed is undefined. Consider:

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

sub insert_audit
{
my (@record)=(@_);
print "@record\n";
print "$record[1]\n";
}

insert_audit ("a", "b" , "c");
print "------\n";
insert_audit ("a", undef, "c");
1;

If you run that you will see:

a b c
b
------
Use of uninitialized value in join or string at /tmp/subs line 8.
a c
Use of uninitialized value in concatenation (.) or string at /tmp/subs line 9.

Regards!

...JRF...
Please use plain text.
Honored Contributor
steven Burgess_2
Posts: 1,493
Registered: ‎11-21-2001
Message 3 of 6 (59 Views)

Re: perl query

Hi

I understand your description and why I would see this error. I can't however see why in this example I get the error. When I print the array as a whole

print "@record\n";

I get each element that I have passed, ie

&insert_audit("$hostid successfully $hostname");

gives

49 successfully rs-dev

Am I being a little thick here ?

TIA

Steve
take your time and think things through
Please use plain text.
Honored Contributor
steven Burgess_2
Posts: 1,493
Registered: ‎11-21-2001
Message 4 of 6 (59 Views)

Re: perl query

Interesting

my $sum=@record;
print "$sum\n";

1

Steve
take your time and think things through
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 6 (59 Views)

Re: perl query

Hi Steve:

OK, you passed a scalar, not an array or list when you said:

# insert_audit("$hostid successfully $hostname");

You should do:

# insert_audit (($hostid, "successfully",$hostname));

Thus:

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

sub insert_audit
{
my (@record)=(@_);
print "@record\n";
print "$record[1]\n";
}

my $hostid = 48;
my $hostname = "myhost";
insert_audit ( ($hostid, "successfully", $hostname) );
1;

...yields:

48 successfully myhost
successfully

Regards!

...JRF...
Please use plain text.
Honored Contributor
steven Burgess_2
Posts: 1,493
Registered: ‎11-21-2001
Message 6 of 6 (59 Views)

Re: perl query

Bargain !!

I get it,

Thanks James

In the meantime I came to this

my ($hostid, $exit, $hostname);
my ($record)=(@_);
($hostid, $exit, $hostname)=split(/ /, $record);

Will do it your way as that is correct

Thanks again

Steve
take your time and think things through
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation