If you have a question on how to do something in Perl, or you need a Perl solution to an actual real-life problem, or you're unsure why something you've tried just isn't working... then this section is the place to ask. Post a new question!

However, you might consider asking in the chatterbox first (if you're a registered user). The response time tends to be quicker, and if it turns out that the problem/solutions are too much for the cb to handle, the kind monks will be sure to direct you here.

User Questions
undefined value as filehandle from Curl.pm
2 direct replies — Read more / Contribute
by wanderedinn
on Jun 28, 2016 at 09:46

    I am trying to understand why I am receiving the following error:

    Can't use an undefined value as filehandle reference at /home/geof/httppost/lib/perl5/site_perl/5.8.8/LWP/Curl.pm line 236

    I have to admit, I don't understand what the code is attempting. Maybe someone can enlighten me? Code with line numbers below:

    233 my $content = ""; 234 open( my $fileb, ">", \$content ); 235 $self->{agent}->setopt( CURLOPT_WRITEDATA, $fileb ); 236 $self->{retcode} = $self->{agent}->perform;

    How is it that the open() above will even work? Can anyone explain to me why I an getting the 'undefined' error? Thanks for any assistance.

Queries for elastic search using perl
2 direct replies — Read more / Contribute
by ravi45722
on Jun 28, 2016 at 09:32
    #$condition = "((column08=Submit & column10=Delivered & column09=Somet +hing) | (column08=Delivered))"; $condition = "(column08=Submit & column10=Delivered & column09=Somethi +ng)"; #$condition = "((column08=Submit & column10=Delivered & column09=Somet +hing) | (column08=Delivered & column09=Something))";

    Hope you have idea on elastic search. From the GUI i will get these type of conditions. I have to parse this into elastic search queries. Is there any easiest way to do this.

    Example : { 'query' => { 'constant_score' => { 'filter' => { 'bool' => { + 'must' => [ + { + 'term' => { + 'column08' => 'Submit' + } + }, + { + 'term' => { + 'column10' => 'Delivered' + } + }, + { + 'term' => { + 'column09' => 'Something' + } + } + ] } } } } }

    Any help will be appreciated.

    Upto now I tried this. But missing some where

    #!/usr/bin/perl #use strict; #use warnings; use Data::Dumper; use List::MoreUtils qw(any uniq); use JSON; $condition = "((column08=Submit & column10=Delivered & column09=Someth +ing) | (column08=Delivered))"; #$condition = "(column08=Submit & column10=Delivered & column09=Someth +ing)"; #$condition = "((column08=Submit & column10=Delivered & column09=Somet +hing) | (column08=Delivered & column09=Something))"; my $char_index = -1; my $stack = 0; my @condition_array = (); sub parse_function { $start_pos = $_[0]; $end_pos = ($_[1]); my @return_array = (); my $end_pos = ($end_pos - $start_pos)+1; my $string = substr($condition,$start_pos,$end_pos); print $string,$/; if (($string =~ m/(=|!=)/)) { my @matches = grep defined, split / ([|&]) /, $string; if ((scalar @matches) > 1) { foreach my $one (@matches) { if (($one =~ tr/=//) == 1) { my ($key,$value)=(split /=/,$one); push @return_array, { term => { $key => $value} }; } elsif (($one =~ tr/!=//) == 1) { my ($key,$value)=(split /!=/,$one); push @return_array, { term => { $key => $value} }; } else { push (@symbol_array,$one); } } @symbol_array = uniq (@symbol_array); $symbol_count = scalar @symbol_array; if ($symbol_count > 1) { print "Check the condition once again... Getting multi + symbol in one brace"; exit; } if (@symbol_array[0] == '&') { $return_hash{'bool'}{'must'} = [@return_array]; } elsif (@symbol_array[0] == '|') { $return_hash{'bool'}{'should'} = [@return_array]; } push @condition_array, %return_hash; print Dumper \@condition_array; exit; $count = @condition_array; return $count; } else { my ($key,$value)=(split /=/,$string); push @condition_array, { term => { $key => $value} }; $count = @condition_array; return $count; } } else { #print "In final else loop"; my @matches = grep defined, split / ([|&]) /, $string; foreach my $one (@matches) { if ($one == '&' or $one == '|') { push (@array_symbol,$one); } else { push (@return_array,$one); } } @array_symbol = uniq (@array_symbol); $count_array_symbol = @array_symbol; if ($count_array_symbol > 1) { print "In final loop : Got two different operators"; exit; } else { if ($array_symbol[0] == '|') { foreach my $instance (@return_array) { $return_hash{'bool'}{'should'} = [@condition_array +[int($instance)]]; } } if ($array_symbol[0] == '&') { foreach my $instance (@return_array) { $return_hash{'bool'}{'match'} = [@condition_array[ +int($instance)]]; } } push @condition_array, \%return_hash; print Dumper \%return_hash; $count = @condition_array; return $count; } } } HERE: $char_index = -1; foreach my $char (split //, $condition) { $char_index++; if ( (index($condition,'(') != -1)) { if ($char eq "(") { $open_brace_index = $char_index; } elsif ($char eq ")") { $returned = parse_function(($open_brace_index+1),($char_in +dex-1)); $first_string = substr($condition,0,$open_brace_index); $last_string = substr($condition,($char_index+1),(length($ +condition)-$char_index)); #print $last_string,$/; $condition = $first_string.$returned.$last_string; print $condition,$/; goto HERE; } } else { $main_hash{"query"}{"constant_score"}{"filter"} = @condition_a +rray[(int($condition)-1)]; my $json = encode_json \%main_hash; print $json; print Dumper \%main_hash; #print "In this loop"; } } exit;
creating a directory remotely implicitly
4 direct replies — Read more / Contribute
by t-rex
on Jun 28, 2016 at 09:14

    Hi guys , I am transferring a file on remote host using openssh package using function scp_put(), the place where i intend to transfer this file has to be under certain directory which also should be created by us based on a uid/date. but it throws me error that transfer failed coz directory is not present on remote host. so how do i create this directory remotely.

    for example : $sourcedir = "/tmp/xyz/test.tar"; $remotedir = "/tmp/some-dir/myfolder_uid_date/"; ssh->scp_put ( $sourcedir, $remotedir); or die "scp failed";

    the output should be i should be able to transfer test.tar in the remote dir path, but i need to create this remote dir path on the fly or from host itself. how can i achieve this ? right now i get error no such file or directory /tmp/some-dir/myfolder_uid_date/

ExtUtils::MakeMaker and module version
1 direct reply — Read more / Contribute
by frazap
on Jun 28, 2016 at 09:12
    When I run perl Makefile.PL for the Glib perl module, I received a message
    "Requested 'glib-2.0 >= 2.24.0' but version of glib-2.0 is 2.16.5"
    As far as I'm award my PKG_CONFIG_PATH does not include any glib-2.0 of version 2.24 And
    pkg-config glib-2.0 --modversion 2.16.5
    Which is the version I would like to build.

    Where does that 2.24 requirement comes from ?

    Thanks

    François

Error when running on larger files
2 direct replies — Read more / Contribute
by K_Edw
on Jun 28, 2016 at 04:36

    I have a small snippet of code which processes a tab-delimited .txt file 2 lines at a time:

    while (<$IN2>) { chomp $_; next if eof; my @F2 = split( "\t", $_ ); #Split each tab-delimite +d field my $partner = <$IN2>; my @F3 = split( "\t", $partner ); #Split each tab-delimite +d field $store{ ( abs( $F2[2] - $F3[2] ) + 1 ) }++; ( $F2[2], $F3[2] ) = ( $F3[2], $F2[2] ) if $F2[2] > $F3[2]; $Tally{ $F2[1] }{ $F2[2] }{ $F3[2] }++; } foreach my $key ( sort { $a <=> $b } keys %store ) { print $OUT4 "$key\t$store{$key}\n"; } foreach my $chr ( sort { $a <=> $b } keys %Tally ) { foreach my $value1 ( sort { $a <=> $b } keys %{ $Tally{$chr} } ) { foreach my $value2 ( sort { $a <=> $b } keys %{ $Tally{$chr}{$value1 +} } ) { print $OUT5 "$chr\t$value1\t$value2\t$Tally{$chr}{$value1}{$value2}\ +n"; } } }

    When attempting to run this on larger .txt files (>4,000,000 lines), I receive the following errors often near the end of the file but >50 lines from it):

    Use of uninitialized value in subtraction (-) at line 117, <$IN2> line + 4148567. Use of uninitialized value in numeric gt (>) at line 118, <$IN2> line +4148567. Use of uninitialized value $F2[2] in hash element at line 119, <$IN2> +line 4148567. Argument "" isn't numeric in sort at line 127, <$IN2> line 4148567.
    Line 117 - $store{(abs($F2[2]-$F3[2])+1)}++; Line 118 - ($F2[2], $F3[2]) = ($F3[2], $F2[2]) if $F2[2] > $F3[2]; Line 119 - $Tally{$F2[1]}{$F2[2]}{$F3[2]}++; Line 127 - foreach my $value1 (sort {$a <=> $b} keys %{$Tally{$chr}}) +{

    Printing $. confirms that the script simply terminates at this input file line and no further lines are read in. If the input file is sorted, the error occurs approximately in the same place but on a different line of content. There is nothing obviously wrong with the content of the file and all lines match expectations.

    However, if I split the input file into two halves - the script runs to completion without error.

    Am I hitting some sort of memory or hash limit? Is there a way to fix this without having to split the input file before processing? This was run on Perl 5.25.2 but also occurs on 5.24.0.

    The format of the input file is as such:

    w 11 99658 75 75M 0 c 11 99999 75 75M 74 w 2 702424 75 75M 0 c 2 702556 75 75M 74 c 13 82486 75 75M 74 w 13 82171 75 75M 0 c 2 702585 75 75M 74 w 2 702390 75 75M 0 c 18 2529 75 75M 74 w 18 2232 75 75M 0 c 12 264648 75 75M 74 w 12 264366 74 74M 0 c 10 177758 75 75M 74 w 10 177438 74 74M 0 w 7 185488 74 74M 0
Loading package error : script works in weird way
1 direct reply — Read more / Contribute
by t-rex
on Jun 28, 2016 at 02:44

    I am using 2 packages ( which i have written) initially i was using them as different scripts ( one script calling another and it was working fine ) but now i can't even see the print output.

    here is one main.pl script
    1 #!/usr/bin/perl 2 print "hello"; 3 use strict; 4 use warnings; 5 use YAML_Lib; 6 use YAML::XS; 7 use Run_Lib; 8 9 print "hello 1"; 10 my $input_file; 11 if(scalar(@ARGV) < 1) 12 { 13 print("USAGE:: $0 <Input file>\n"); 14 exit(0); 15 } 16 17 print STDOUT "Welcome to our little program\n"; 18 19 $input_file = $ARGV[0]; 20 print "Input File = $input_file\n"; 21 # parse the yaml file 22 $YAML_Lib::yaml_input = YAML::XS::LoadFile("$input_file"); 23 &YAML_Lib::parse_yaml($YAML_Lib::yaml_input); 24 25 # call client functions + 26 &Run_Lib::socket_check();

    here is the package Run_Lib

    1 #!/usr/bin/perl 2 package Run_Lib; 3 use strict; 4 use warnings; 5 use Exporter 'import'; + 6 use Net::OpenSSH; 7 #use Fcntl; 8 #use Client_Lib; 9 10 #function declarations 11 sub socket_check(); 12 sub open_file($$); 13 sub read_file_str($$); 14 sub write_file_str($$); 15 sub server_setup(); 16 17 #global variable declarations 18 my $client_connect_flag = 0; 19 my $pwd = $ENV{'PWD'}; 20 my $filename = "$pwd/LOG.txt"; 21 22 #check for if the socket is established by calling the client (hos +t) script 23 #socket_check(); 24 25 sub socket_check() 26 { 27 # open the log file ( flush for the first time ) 28 print "helloooooooo!!"; 29 my $log_fh = open_file( $pwd, $filename ); 31 #write the log about calling client script 32 my $string = "INFO :: Calling the Client script \n"; 33 write_file_str( $log_fh, $string ); 34 35 # call the client functions to connect to the socket 36 #my $cmd = "$pwd/utpsm_lts_client.pl"; 37 #my $rc = system($cmd) 38 #or die "cant run client.pl $! \n"; 39 40 # my $socket_client = &Client_Lib::client_socket_create(); 41 # &Client_Lib::client_main($socket_client); 42 43 # check the log file if the client connected or not 44 my $client_str = "FAIL_CLIENT_CONNECT"; 45 read_file_str( $log_fh, $client_str); 46 47 # check if client connected or not 48 if ($client_connect_flag) 49 { 50 # setup the server on target 51 server_setup(); 52 } 53 54 } 55 56 sub open_file ($$) 57 { 58 my ( $pwd, $filename ) = @_; 59 my $FH; 60 open ( $FH ,"+>$filename" ) or 61 die "can't open/create $filename $!"; 62 return $FH; 63 } 64 65 66 sub read_file_str ($$) 67 { 68 my ($log_fh, $find_str) = @_; 69 my @lines = <$log_fh>; 70 #check for the output of log file if the client started or not 71 for (@lines) 72 { 73 if ($_ =~ /$find_str/ ) 74 { 75 $client_connect_flag = 0; 76 } 77 } 78 close $log_fh; 79 } 80 81 sub write_file_str ($$) 82 { 83 my ( $log_fh, $string_to_write ) = @_; 84 print $log_fh "$string_to_write"; 85 close $log_fh; 86 }

    now i am confused as to what is the problem. anyone has some clues to it

Keeping deflated data in memory
3 direct replies — Read more / Contribute
by murrayn
on Jun 28, 2016 at 00:44

    I need to "deflate" a data buffer and then embed that into another data stream before writing it out (I'm trying to write a bitmap into a new PNG file).

    IO::Compress::Deflate appears to do the job and its doc says it "allows writing compressed data to files or buffer". All the methods and examples appear to write files. Am I wrong to interpret "buffer" as meaning an area of my program's working storage ($buffer)? Have I missed reading some crucial piece of documentation which should have been blindingly obvious? Am I using the wrong module altogether?

Compare 2 arrays
3 direct replies — Read more / Contribute
by niceguy
on Jun 27, 2016 at 19:18

    Dear PerlMonks,

    I am relatively new with Perl. I am trying to delete some old files from a directory based on, if it is not listed in the file and this file has a SDF file extension.

    I have an array (@file) that contains a list of all the file name in the directory. And another array (@name) contains a list of all the lines from the SDF file.

    What I like to do is to compare the two arrays and find the file name that is not listed in the SDF file.

    The SDF file contains lines like:

    • fullpath="C:\directory\filename1.nfo"
    • id="1a"
    • fullpath="C:\directory\filename2.nfo"

    I've been searching the web and found some good examples but not exactly on what I am trying to do. I have tried using "!($name =~ $filename)", it just delete all the files in the directory. I think there is something wrong with the logic in the loop but not sure where. Below, is my code that are able to find the matching files. I am wondering if you can help.

    Thank you in advance for your help.

    #!\perl\bin\perl use strict; use warnings; my $files = "C:\\Directory"; my $list = "C:\\Test.sdf"; my @name; open(my $name, "< $list") or die "Failed to open file: $!\n"; while(<$name>) { chomp; push @name, $_; } close $name; my @file = $files; opendir(OUTPUT, $files); @file = readdir(OUTPUT); closedir(OUTPUT); foreach my $filename (@file) { foreach $name (@name) { if ($name =~ $filename) { last; }else { unlink ($files . "\\" . $filename) or warn qq{cannot delete $fil +ename: $!+}; last; } } }
UnMesh or UnZip List
1 direct reply — Read more / Contribute
by awohld
on Jun 27, 2016 at 18:44
    I have this list that is basically what a List::MoreUtils mesh or zip output of a list would look like.

    Looking all over there doesn't seem to be an unmesh or unzip function out there and my attempts look ugly. I know how many list were zipped into the input, so
    my @test1 = qw( 1 1 2 2 3 3 4 4 5 5 6 6 7 7 ); my @test2 = qw( 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 ); # unzip @test1, we know to make it two lists my $unzipped = unzip( 2, @test1 ); $unzipped = [ [ 1, 2, 3, 4, 5, 6, 7 ], [ 1, 2, 3, 4, 5, 6, 7 ], ] # unzip @test2, we know to make it three lists my $unzipped = unzip( 3, @test2 ); $unzipped = [ [ 1, 2, 3, 4, 5, 6, 7 ], [ 1, 2, 3, 4, 5, 6, 7 ], [ 1, 2, 3, 4, 5, 6, 7 ], ]
    Any ideas if "unzip" or "unmesh" exists or an elegant way to implement unzip?


Strings overwrite themselves in print command
5 direct replies — Read more / Contribute
by tdsny71
on Jun 27, 2016 at 17:55

    I am trying to make a CSV file out of an LDAP output that comes from an external process.

    Here is the structure of the output file:

    cn: applegroup
    member: CN=mem001,OU=Users,DC=myorg,DC=com
    member: CN=mem002,OU=Users,DC=myorg,DC=com
    member: CN=mem003,OU=Users,DC=myorg,DC=com
    member: CN=mem004,OU=Users,DC=myorg,DC=com

    cn: orangegroup
    member: CN=mem301,OU=Users,DC=myorg,DC=com
    member: CN=mem302,OU=Users,DC=myorg,DC=com
    member: CN=mem303,OU=Users,DC=myorg,DC=com

    And here is the code I have to read it:
    #!/usr/bin/perl use strict; use warnings; # 0a) Initialize variables my $grpNm = ""; my $memID = ""; my @groupInfo = ("", ""); my @memberInfo = ("", ""); my @ldapMemInfo = ("", ""); my @idInfo = ("", ""); my @csvContent = ("", ""); my $allGroupResult = "allTmp.txt"; my $allGroupDataFinal = "allData.csv"; open my $fhGroupFile, '<', "$allGroupResult" or die "Could not create +or open $allGroupResult"; open my $fhCSVOutput, '>', "$allGroupDataFinal" or die "Could not crea +te or open $allGroupDataFinal"; sub lTrim { my $s=shift; $s =~ s/^\s+//; return $s; } sub rTrim { my $s=shift; $s =~ s/\s+$//; return $s; } sub trimAll { my $s=shift; $s =~ s/^\s+|\s+$//g; return $s; } # ------------ Main Program -------------------- while (<$fhGroupFile>) { my $line = $_; chomp $line; # Check if line begins with "cn" or "member" if ($line =~ m/^cn/) # found group name { @groupInfo = split /:/, $line; $grpNm = $groupInfo[1]; # second entry is group name $grpNm = lTrim($grpNm); #trim leading spaces from group name # print "array indice $groupInfo[1]\n"; print "scalar grpNm:$grpNm\n"; $csvContent[0] = $grpNm; } elsif ($line =~ m/^member/) # found member name { @memberInfo = split /:/, $line; @ldapMemInfo = split /,/, $memberInfo[1]; # second entry is full + string for ldap content @idInfo = split /=/, $ldapMemInfo[0]; # second entry is ldap con +tent #print "array idInfo /= @idInfo\n\n"; $memID = $idInfo[1]; # second entry is the specific ID #print "scalar memID = $memID\n"; $csvContent[1] = $memID; print $fhCSVOutput "$csvContent[0]"; print $fhCSVOutput ","; #print $fhCSVOutput "$csvContent[1]"; print $fhCSVOutput "\n"; } else { } } # while <$fhGroupFile> close($fhGroupFile); close($fhCSVOutput);

    When I run it I get:
    ,pplegroup
    ,pplegroup
    ,pplegroup
    ,pplegroup
    ,rangegroup
    ,rangegroup

    Before you ask, I get the same result if I use the scalar variables without the array @csvContent so,

    print $fhCSVOutput "$csvContent[0]"; print $fhCSVOutput ","; #print $fhCSVOutput "$csvContent[1]"; print $fhCSVOutput "\n";

    AND

    print $fhCSVOutput "$grpNm"; print $fhCSVOutput ","; #print $fhCSVOutput "$memID"; print $fhCSVOutput "\n";

    yield the same erroneous data.

Add your question
Title:
Your question:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":


  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.