Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Seekers of Perl Wisdom

( #479=superdoc: print w/ replies, xml ) Need Help??

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
@INC not set for perlbrew
3 direct replies — Read more / Contribute
by Alessandro
on Jul 25, 2016 at 14:07

    Hi monks,

    I am on os x El capitan and have perlbrew installed. However, when I install a perl module I can't get it working, because it seems perl is not looking at the right place.

    Can't locate WWW/ in @INC (you may need to install the WWW::Mechanize module) (@INC contains: /Library/Perl/5.18/darwin-thread-multi-2level /Library/Perl/5.18 /Network/Library/Perl/5.18/darwin-thread-multi-2level /Network/Library/Perl/5.18 /Library/Perl/Updates/5.18.2/darwin-thread-multi-2level /Library/Perl/Updates/5.18.2 /System/Library/Perl/5.18/darwin-thread-multi-2level /System/Library/Perl/5.18 /System/Library/Perl/Extras/5.18/darwin-thread-multi-2level /System/Library/Perl/Extras/5.18 .)


     which perl returns  /opt/local/bin/perl5

    my understanding is that it should return the path to perlbrew install and not the system perl.

    and  perl -v returns  This is perl 5, version 22, subversion 2 (v5.22.2) built for darwin-thread-multi-2level and I indeed used perlbrew to install and switch to perl 5.22.2 but it seems somehow, the "switch" was only partial. What am I missing here?


Recommendations on a module for web crawling
1 direct reply — Read more / Contribute
by edimusrex
on Jul 25, 2016 at 13:59

    I am trying to crawl a web page where the data I am seeking is generated by javascript. I haven't had to deal with this before but I can imagine I am not the only one who has come across this. Is there a specific module that is preferred in this instance? I've tried using Mechanize::Firefox but the page complains about unsupported browser. I'd prefer to do this in perl but will use other languages if need be.

    Thanks for your recommendations

building sql statement
3 direct replies — Read more / Contribute
by mwill007
on Jul 25, 2016 at 13:42

    I have to update database manually alot, so I wrote an app that reads the first line of a csv file and creates the headers/tables for an insert or update statement. The next lines are the data in the file.

    I build the statement like so
    if ($checksite eq $none)
    $insertsqlstatement = 'INSERT INTO database.database(';
    foreach (@dbcolumn)
    $insertsqlstatement = $insertsqlstatement . "@dbcolumn$x";
    # if ($x != $numelements )
    $insertsqlstatement = $insertsqlstatement . ",";
    # else
    $insertsqlstatement = $insertsqlstatement . ') VALUES (';
    foreach (@dbcolumn)
    $insertsqlstatement = $insertsqlstatement . '\'' . "@splitarray$x" . '\'';
    $splitstatement = $splitstatement . '$' .@dbcolumn$x ;
    if ($x != $numelements)
    $insertsqlstatement = $insertsqlstatement . ',';
    $splitstatement = $splitstatement . ',';
    # else
    $insertsqlstatement = $insertsqlstatement .')';
    when I print the value of $insertsqlstatement is equal to INSERT INTO database.database(site_id, site_store_type, project, site_city, site_state, site_country, project_begin ) VALUES ('9999','SC OSR','Prebuild','Penfield','NY','US','8/8/2016 ')
    I can copy it an it runs just fine in sql
    Buy when i write from my sql statement it will not write sqlexec($insertsqlstatement);
    But if i manualy set the value of $insertsqlstatement = "INSERT INTO database.database(site_id, site_store_type, project, site_city, site_state, site_country, project_begin ) VALUES ('9999','SC OSR','Prebuild','Penfield','NY','US','8/8/2016 ')";
    The sqlexec($insertsqlstatement); executes and updates the database

Push values into a hash
3 direct replies — Read more / Contribute
by Anonymous Monk
on Jul 25, 2016 at 10:37
    Hi Monks!

    I need to add values into a hash to create a JSON file. Is there a better way to do this. Here is the code I am trying:
    #!/usr/bin/perl use strict; use warnings; use Data::Dump 'pp'; use Data::Dumper; use JSON; my %data; foreach my $number (@{ $names }) { $data{name} = $claim->{"n_names"}; $data{address} = $claim->{"n_add"}; $data{phone} = $claim->{"n_phone"}; $data{email} = $claim->{"n_email"} || ''; push \%data, $data{name}, $data{address}, $data{phone}, $data{email +}; } my $json = encode_json \%data; print Dumper $json;
    Thanks for helping!
Directory Monitor
1 direct reply — Read more / Contribute
by perlmad
on Jul 25, 2016 at 05:27

    Hi monks!!!

    I have been using File::Monitor perl module to monitor a directory and its working well when create or delete a file to a given directory ,if i update a file it shows nothing and i have referred all the links but i don't know what i did wrong

    Here is my code

    #! usr/bin/perl use strict; use warnings; use File::Monitor; use File::Monitor::Delta; use File::Monitor::Object; use Getopt::Long; use Config::IniFiles; use MIME::Lite; $file_list[$#file_list]=''; $temp=join('/',@file_list); $email_body=$email_body."\n File Name : $fil +e_name , Path : ".$temp."\n"; print "\n\n File Name : $file_name , Path : +".$temp."\n"; } $email_subject="Monitor Path : $path , Status : Added" +; &email; $email_subject=''; $email_body=''; undef @file_changes; undef @file_list; } if($file->files_deleted){ #print $file->files_deleted, " deleted\n"; print "Files Removed \n"; @file_changes=$file->files_deleted; $email_body="\nFiles Removed,\n"; for my $temp(@file_changes){ @file_list=split('/',$temp); my $file_name=$file_list[$#file_list]; $file_list[$#file_list]=''; $temp=join('/',@file_list); $email_body=$email_body."\n File Name : $fil +e_name , Path : ".$temp."\n"; print "\n\n File Name : $file_name , Path : +".$temp."\n"; } $email_subject="Monitor Path : $path , Status : Remove +d"; &email; $email_subject=''; $email_body=''; undef @changes; undef @file_list; } if($file->is_event('size')){ print "size change\n\n"; } if ($file->is_size) { my $name = $file->name; my $old_size = $file->old_size; my $new_size = $file->new_size; print "$name has changed size from $old_size to $new_size\ +n"; } } }

    Actual Output

    [abc@linux0059 monitor]$ perl --c config.ini Files Removed File Name : same1 , Path : /tmp/abc/ Email send : Files Created File Name : same1 , Path : /tmp/abc/ Email send : [abc@linux0059 monitor]$

    Monitor Directory

    [abc@linux0059 abc]$ ls folder log same1 same.txt sample2.txt simple [abc@linux0059 abc]$ rm same1 [abc@linux0059 abc]$ touch same1 [abc@linux0059 abc]$ echo "hai hello" > same1 [abc@linux0059 abc]$

    Response Much appreciated!!!

How to pass directory variable using IPC::System::Simple
2 direct replies — Read more / Contribute
by bill5262
on Jul 25, 2016 at 03:32

    I'm trying to automatically create a subdirectory with today's date and output the xxx files (this is done by a different program, I just pass on the parameters).
    This is what I have (with help from "haukex")

    #!/usr/bin/env perl use warnings; use strict; use IPC::System::Simple; my $savedir = foldername; system('GPIBShot.EXE','/HIDE','/12','/COLOR',"/NAME=$savedir/TEST1",'/ +SCREEN');

    A few questions that I wasn't able to find online (or I'm just miserable at googling)

    1. Is there a way to fetch today's date and assign it to my "savedir" variable?

    2. Assuming I did (or didn't) get the variable set up above, how do I pass it down using "system" in one of the parameters? The paramter "/NAME=" is where I set the directory, and the program manual doesn't specify whether sending a dir will work or not, which is what I'm trying to test eventually. But I'm getting an error from Perl as the following. Bareword "foldername" not allowed while "strict subs" in use at

Perl Cryptography - Seeking Resources
5 direct replies — Read more / Contribute
by ljamison
on Jul 24, 2016 at 10:46
    Greetings, Monks!

    Starting August 1st I'll be teaching a 2-week programming camp to high school students (9th, 10th, and 11th graders) which consists of two parts -- Mathematics and Programming. Another person is teaching the Math side but this year I am teaching the programming side. The topic of the camp this year is Cryptography.

    Are there any resources available which aid in teaching Perl-based Cryptography?

Apache Kafka prior use
No replies — Read more | Post response
by Wiggins
on Jul 23, 2016 at 14:52
    I am looking for advice from someone who has sailed a possibly mythical sea named 'Apache Kafka' in a Perl boat.

    Due to a thinness of published guidance for the Perl interface, I would like to know if a different Producer is needed for each 'different topic'? I have the producer and consumer each on separate threads. But now have to publish to 3 different topics. I know consumers a 1:1 with topics. Are publishers the same?


    I have ventured a small distance from shore and returned where I had hoped. I find that a single Producer can handle multiple ( =2 which is greater than 1) topics.

    It is always better to have seen your target for yourself, rather than depend upon someone else's description.

Linux::Inotify2 remove single watch
1 direct reply — Read more / Contribute
by agarrubio
on Jul 22, 2016 at 19:57
    What is the right way to remove a single watch. This doesn't work. After "canceling" only one watch, the the poll loop stops blocking.
    use strict; use warnings; use Linux::Inotify2; my $inotify=new Linux::Inotify2 or die "Unable to create new inotify object: $!"; my $watch_one= $inotify->watch("/var/tmp/one", IN_MODIFY , sub {print "one modified\n"} ) or die "one $!\n"; my $watch_two= $inotify->watch("/var/tmp/two", IN_MODIFY, sub {print "two modified\n"} ) or die "two $!\n"; $watch_one->cancel; # Canceling a single watch stops all activity 1 while $inotify->poll; print STDERR "loop stoped blocking\n";
remove last column of tab-delimited file
2 direct replies — Read more / Contribute
by mika6891
on Jul 22, 2016 at 13:29

    I'm loading and printing tab-delimited files in my perl script. However the last column of my input file ($table1) is empty and I don't want to print this in my output file ($table3). How and where should I do this? After the 'open' or at the end in the 'print $table3'? This is part of my script (... denotes code not important for this question)

    #! /usr/bin/perl use strict; use warnings; use Data::Dumper; local $Data::Dumper::Useqq = 1; use Getopt::Long qw(GetOptions);; ... open(my $table1,'<', $input) or die "$! - [$input]"; #input file open(my $table3, '+>', $output) || die ("Can't write new file: $!"); # +output file ... chomp( my @header_for_table1 = split /\t/, <$table1> ); print $table3 join "\t", @header_for_table1, "name1", "name2", "\n"; { no warnings 'uninitialized'; while(<$table1>){ chomp; my %row; @row{@header_for_table1} = split /\t/; print $table3 join ( "\t", @row{@header_for_table1}, @{ $lookup{ ... } // [ "", "" ] }), "\n"; } }

Add your question
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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (10)
    As of 2016-07-26 17:28 GMT
    Find Nodes?
      Voting Booth?
      What is your favorite alternate name for a (specific) keyboard key?

      Results (239 votes). Check out past polls.