SitePoint Sponsor

User Tag List

Results 1 to 22 of 22
  1. #1
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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";

  2. #2
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,280
    Mentioned
    51 Post(s)
    Tagged
    2 Thread(s)
    my $seed = pop @seeds;

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

  3. #3
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd write it as:

    Code Perl:
    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.

  4. #4
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!!! yeah..the seed isnt valid anyway

  5. #5
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have trouble to understand these matching equation and foreach function trying to do 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;
    }
    }

  6. #6
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code Perl:
    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

    Code:
    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-Slu.../File/Slurp.pm

  7. #7
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  8. #8
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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+(.*)/ ) {
    $options->{$header}->{$1}->{$2} = $3;
    }
    }

  9. #9
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post

    Please wrap your code in tags, it is more readable that way.

    Code:
    $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
    Code:
    $options->{$header}->{$1}->{$2} = $3
    which can also be more cleanly written as
    Code:
    $options->{$header}{$1}{$2} = $3
    means

    Code:
    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

  10. #10
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    The test even have something like &#37;$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 }} ) {

  11. #11
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!

  12. #12
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright. thanks for the great help!!! appreciate it!

  13. #13
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  14. #14
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

    Code:
    use cafe
    cafe.pm file
    inside it will do the package cafe

    Code:
    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.
    Code:
    my $temp = cafe->new(&#37;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?
    Code:
    push(@cafes, $temp);

  15. #15
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.

    Quote Originally Posted by ahuong View Post
    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
    .pl = perl script.
    .pm = perl module.

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

    Code:
    use cafe
    cafe.pm file
    inside it will do the package cafe

    Code:
    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.
    Code:
    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?:(
    Code:
    push(@cafes, $temp);
    Please use Data::Dumper, you can install it off CPAN.

  16. #16
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your help again.

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


    Code:
    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.

  17. #17
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ahuong View Post
    Thank you for your help again.

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


    Code:
    my $temp = cafe->new(%cafe_switches);
    Code:
    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:

    Code:
    cpan install Data::Dumper
    If that doesn't work, try
    Code:
    perl -MCPAN -e 'install Data::Dumper'
    Best of luck.

  18. #18
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,280
    Mentioned
    51 Post(s)
    Tagged
    2 Thread(s)

  19. #19
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:
    Code:
    sub Check
    {
            print "Not yet implement \n";
    }
    But this return the error:

    Code:
    sub GetTestName() { 
    
    print"GetTest\n"; 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:

    Code:
    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\n";
    
            }
    But it doesnt print out the message when i do the:

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

  20. #20
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nevermind about this post now.I have figured it out though.

  21. #21
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I have a code that will return $id= 0 and $id= 1 randomly but I would like to make it to get the $id=0 always first...how should I do it?

    foreach my $link (sort @linker){
    my $id = XMLP::Attribute($link,"ID");
    $substr .= "_csi" ;
    $substr .= $id ;
    }
    $style .= $substr;

    and the end result should be always style_csi0_csi1 always...

  22. #22
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're being pretty sparse with your code, I cannot look in your head

    Based on this limited information I have no idea what's going on... If I were you, I'd check the workings (documentation) of XMLP::Attribute and see what it returns based on which arguments. Use the debugger.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •