Uninitialized value in concatenation (.) or string at

I am having this error in the line self->{_seedpath} below…can somebody help!!!

my @seeds = glob("*r0.32.obj");
@seeds = sort @seeds;
my $seed = pop @seeds;

$self->{_seedPath} = “$self->{_work_dir}/$seed”;

Appreciate if anyone can help! I am confused on how the file is calling when learning up the perl script.
FYI, I have files called rr.pl and cafe.pm

rr.pl file
it does “use cafe” that i am not sure if this is the way to call to cafe.pm file

use cafe

cafe.pm file
inside it will do the package cafe

package cafe

How does the code below (in rr.pl file) works with the cafe.pm file as I saw there is a sub new() function calling in cafe.pm. And I am still confused on how the referencing works.

my $temp = cafe->new(%cafe_switches);

And I try to print out the array cafes but it comes with some hash number. What is the right way to print out these variable if i want to see what it pass into these?:frowning:

push(@cafes, $temp);

my $seed = pop @seeds;

You should check to see if something’s actually getting popped out before going on to the next line?

I’d write it as:


my @seeds = sort glob("*r0.32.obj");
if(my $seed = pop @seeds) {
    $self->{_seedPath} = "$self->{_work_dir}/$seed";
}

That way the expressions in the if() conditions aren’t reached unless $seed is true (defined, etc)

Also the output from glob() can directly be fed into sort(), so your @seeds will be what you want in one pretty line of code.

Thanks!!!:smiley: yeah…the seed isnt valid anyway:(

I have trouble to understand these matching equation and foreach function trying to do:(:(:frowning: anyone can try to explain to me? thanks a million!!

my $testtest=“test.txt”;
open (INFILE,“$testtest”);
my @line=<INFILE>;
close(INFILE);

foreach (@lines) {
    my $line = $_;

    # Search for new header

if ($line =~ /^\[(.*)\]/) {
if ($new) {
$header = $1; next;
} else {
$new = 1; $header = $1; next;
}
}


my $testtest="test.txt";

open (INFILE,$testtest) or die("Cannot open $testtest: $!"); # It's good to check for I/O failure
my @line=<INFILE>; # read all lines into @line array
close(INFILE);

foreach (@lines) {                  # iterate over every line in @lines
    my $line = $_;                  # set $line to current line, which is $_
    if ($line =~ /^\\[(.*)\\]/) {     # see if a pattern matches
        if ($new) {                 # $new = true?
            $header = $1;           # set $header to what you've captured in the pattern match (.*)
            next;                   # next line
        } else {                    # $new = false?
            $new = 1;               # set $new to true   
            $header = $1;           # set $header to what you've captured in the pattern match (.*)
            next;                   # next line
        }
    }
}

If you use foreach, you can also do

foreach my $line (@lines) { ... }

That way you don’t have to assign $line to $_ for every iteration.

Instead of open() you can also use File::Slurp, see http://search.cpan.org/dist/File-Slurp/lib/File/Slurp.pm

Thanks for the clear explanation but from the code I see, the $new is initialize as 0
so that is mean it wont go into if loop in the first line if the pattern matched, right?

But what is the purpose of doing that?

I am rather confused of the below highlighter item as well…what does it trying to do?=.=

Read Header

    if ($header =~ /PI/) {    #
            if ($line =~ /psmi_release\\s+(\\S+)/) { $pitool = $1;}  
            if ($line =~ /handler\\s+(\\S+)/) { $handler = $1;}
            if ($line =~ /model\\s+(\\S+)/) {$mdl = $1;}
            next;
    } else {
            if ( $line =~ /^(\\S+):(\\S+)\\s+(\\S+)\\s+(.*)/ ) { 
                  [COLOR="Red"]  $options-&gt;{$header}-&gt;{$1}-&gt;{$2} = $3;[/COLOR]
            }
    }

Please wrap your code in



tags, it is more readable that way.

$options->{$header}

means "address the item in hashreference $options with the hashkey equal to the value of $header. So if $header equals ‘foo’, it’d be $header->{foo}.

$1, $2 and $3 are regular expression captures. The first, second and third capture. So

$options->{$header}->{$1}->{$2} = $3

which can also be more cleanly written as

$options->{$header}{$1}{$2} = $3

means


Set the hashvalue corresponding with hashkey $2 
    [which is the hashvalue corresponding with hashkey $1 
        [which is the hashvalue corresponding with hashkey $header 
            [which is a hashkey of hashreference $options] 
        ] 
    ] 
to $3

I suggest you read up on basic Perl data structure and reference use; see http://perldoc.perl.org/perlref.html and http://perldoc.perl.org/perldsc.html

Thanks for the pointer. I have googled and actually read through the link pasted but I couldn’t get the meaning when it does the double referencing {}->{}->
FYI, this is not my code and i start picking up perl so it is really hard for me to visualize what it is trying to do.:(:frowning:
The test even have something like %$tests variable but i couldnt found it in google:(

for my $test (sort keys %$tests )
{
my ($seed, $seed2, $seed3, $seed4, $seed5, $nt, $pfmn);
for my $label (keys %{$tests->{ $test }} ) {

The documentation I gave you in my previous post explains reference use.

$ => scalar
@ => array
% => hash

%$hash_reference : dereference the hashreference contained in the scalar $hash_reference
@$array_reference : dereference the arrayreference contained in the scalar $array_reference

etcetera.

I would really suggest you read up on Perl. Try to get your hands on O’reilly’s Programming Perl book, or O’reilly’s Learning Perl. Also, http://www.perlmonks.org has loads of documentation and tutorials. So does http://perldoc.perl.org.

Good luck!

Alright. thanks for the great help!!! appreciate it!

Glad it’s useful.

If you want to be explicit in your dereferencing, you can also do:

@{ $array_reference }[0]
%{ $hash_reference }{foo}

compare this to

$array_reference->[0]
$hash_reference->{foo}

Cheers.

Hi.

.pl = perl script.
.pm = perl module.

“use modulename” (so “use cafe” in case of “cafe.pm”) is the correct way of including that.

use cafe

cafe.pm file
inside it will do the package cafe

package cafe

How does the code below (in rr.pl file) works with the cafe.pm file as I saw there is a sub new() function calling in cafe.pm. And I am still confused on how the referencing works.

my $temp = cafe->new(%cafe_switches);

It seems the cafe package defines a class somewhere, which is instantiated with the new() subroutine. For more information on this, please read up on Perl Object Oriented programming, it’s probably a bit different to what you’re used to.

And I try to print out the array cafes but it comes with some hash number. What is the right way to print out these variable if i want to see what it pass into these?:frowning:

push(@cafes, $temp);

Please use Data::Dumper, you can install it off CPAN.

[COLOR=“navy”]Thank you for your help again. :blush:

So when this is done. It is referencing a function (new) from cafe.pm?[/COLOR]

my $temp = cafe->new(%cafe_switches);

anyway, I am not sure how to download the CPAN as i am looking the code from unix/linux environment.

cafe->new(%cafe_switches) # call "new" method in the cafe package, with argument %cafe_switches

This would assign to $temp whatever comes out of new(), usually a class instance in the form of a blessed hash reference. please read up on basic Perl and Perl OO.

anyway, I am not sure how to download the CPAN as i am looking the code from unix/linux environment.

If you’re on Linux, you can just do (as root) on the commandline:

cpan install Data::Dumper

If that doesn’t work, try

perl -MCPAN -e 'install Data::Dumper'

Best of luck.

http://sunsite.bilkent.edu.tr/pub/CPAN/misc/cpan-faq.html ++

I have one perl module call cafe.pm but I can print the message in some of its subroutine and some just return me the error message:

Can’t use string (“cafe”) as a HASH ref while “strict refs” in use

From the main code:

my $temp2=cafe->Check();
my $temp1 = cafe->GetTestName();

Both subroutine are under the same cafe.pm:

This can successfully printed out when I run the main perl script:

sub Check
{
        print "Not yet implement \
";
}

But this return the error:

sub GetTestName() { 

print"GetTest\
"; my $self = shift;  return $self->{_o}; }

And can somebody tell me if sub new() is a special subroutine in a package??? Coz I cant seems to print any message from it as well.
For example when i try to see if it execute the sub new(), I put a print inside the subroutine:

sub new
{
        my $class = shift;
        my (%switches) = @_;
        my ($self) = { };
        bless $self, $class;

        foreach my $key (sort keys %switches) { 
                $self->{$key} = $switches{$key}; 
print " in xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxst new\
";

        }

But it doesnt print out the message when i do the:

 my $temp = cafe->new(%cafe_switches);

:blush:Nevermind about this post now.I have figured it out though.