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
Pattern matching and deriving the data between the "(double quotes) in HTML tag
6 direct replies — Read more / Contribute
by sp4rperl
on Dec 04, 2016 at 22:29
    #/usr/bin/perl my $timeLimit = '<timeLimit endTime="2016-12-28T23:59:59" startTime="2 +016-09-30T00:00:00"></timeLimit>'; $timeLimit =~ m/startTime="(.*?)"/g; my $startTime = $1; chomp($timeLimit); if ($timeLimit =~ m/endTime/) { $timeLimit =~ m/endTime="(.*?)"/g; my $endTime = $1; chomp($endTime); } print "[$startTime],[$endTime]\n";

    Hello fellow monks!! I want to get the date that is enclosed in between the "". Please help me in understanding what is the change that is needed in the code to obtain the desired output. desired output: [2016-09-30T00:00:00],[016-12-28T23:59:59] unexpected output obtained: [2016-09-30T00:00:00],[]

adding modules from CPAN to "packaged" mod_perl2
1 direct reply — Read more / Contribute
by glasswalk3r
on Dec 04, 2016 at 21:19

    Hello fellow monks

    I did some researching but I'm not being able to find how to add new modules from CPAN to a "packaged" (compiled and distributed, like DEB or RPM packages) but couldn't find anything.

    If I want to install mod_perl2 from scratch that's easy, because I can do it from the perl installed from source code. But for package mod_perl2, I believe the interpreter won't be available. Or I'm guessing it wrong?

    Or I can assume is the same perl that is available system wide (and controlled by root) and could try to install those new modules to a different location (and configure mod_perl2 to search for those modules there)?

    Thanks in advance!

    Alceu Rodrigues de Freitas Junior
    ---------------------------------
    "You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill
How to reuse a http::request object?
3 direct replies — Read more / Contribute
by lmocsi
on Dec 04, 2016 at 17:20
    Hi,
    Is it possible to reuse a http::request object with a new url?
    Or do I have to construct a new one?
    use strict; use LWP::UserAgent; use HTTP::Request; use utf8; my ($content,$decoded,$req,$res); my $url = "http://something"; my $ua = LWP::UserAgent->new; $req = HTTP::Request->new("GET", $url); $req->header('Content-Type' => 'application/json', 'Accept' => 'application/json, text/javascript, */*; q= +0.01' ); $res = $ua->request($req); $content = $res->content(); # First request done # New url $url = "http://something_else"; # What next?
How do I determine if a variable contains a type glob?
4 direct replies — Read more / Contribute
by dpchrist
on Dec 04, 2016 at 15:52

    I would like to write a subroutine is_typeglob($) that returns true when passed a type glob and returns false when not passed a type glob. (I already have a subroutine is_typeglob_ref($).)

    This is a scratch program I wrote to experiment with some ideas:

    2016-12-04 12:35:05 dpchrist@debian ~/sandbox/perl $ cat is_typeglob.pl #!/usr/bin/perl use strict; use warnings; use Data::Dumper; $Data::Dumper::Indent = 0; use Scalar::Util qw(blessed openhandle reftype); { no warnings 'once'; open (FH, '<', $0) or die "ERROR: open(): $!"; } for ( q(*ARGV ), q(*ARGV cmp '*ARGV' ), q(*ARGV cmp "*ARGV" ), q(*ARGV cmp '*::ARGV' ), q(*ARGV cmp "*::ARGV" ), q(ref *ARGV ), q(blessed *ARGV ), q(openhandle *ARGV ), q(reftype *ARGV ), ) { my $code = $_; my $r = eval $code; print Data::Dumper->Dump([$code, $r, $@], [qw(code r @)]), "\n"; } # $Id: is_typeglob.pl,v 1.1 2016/12/04 20:35:04 dpchrist Exp $

    Here is a sample run:

    2016-12-04 12:42:14 dpchrist@debian ~/sandbox/perl $ perl is_typeglob.pl $code = '*ARGV ';$r = *::ARGV;$@ = ''; $code = '*ARGV cmp \'*ARGV\' ';$r = 1;$@ = ''; $code = '*ARGV cmp "*ARGV" ';$r = 1;$@ = ''; $code = '*ARGV cmp \'*::ARGV\' ';$r = 1;$@ = ''; $code = '*ARGV cmp "*::ARGV" ';$r = 1;$@ = ''; $code = 'ref *ARGV ';$r = '';$@ = ''; $code = 'blessed *ARGV ';$r = undef;$@ = ''; $code = 'openhandle *ARGV ';$r = undef;$@ = ''; $code = 'reftype *ARGV ';$r = undef;$@ = '';

    How do I determine if a variable contains a type glob?

sane way to configure perlbrew and h2ph on Ubuntu
1 direct reply — Read more / Contribute
by glasswalk3r
on Dec 04, 2016 at 13:53

    Hello fellow monks!

    Meanwhile I'm working to release a new module to CPAN, I find out that Ubuntu and h2ph don't play nice with each other (at least when I using perlbrew).

    After running h2ph as documented, I find out that the perl was expecting to find the ph files in different locations that they are stored in /usr/include. Some research led me to find that this is because change to a "multiarch", whatever that means). Unfortunately, I didn't find anything that would help fix the issue.

    I was getting errors like:

    # Error: Can't locate bits/syscall.ph in @INC (did you run h2ph?) + (@INC contains: /home/me/Projetos/Linux-NFS-BigDir/.build/MHr69O96uB +/blib/lib /home/me/Projetos/Linux-NFS-BigDir/.build/MHr69O96uB/blib/a +rch /home/me/perl5/perlbrew/perls/perl-5.24.0/lib/site_perl/5.24.0/x8 +6_64-linux /home/me/perl5/perlbrew/perls/perl-5.24.0/lib/site_perl/5. +24.0 /home/me/perl5/perlbrew/perls/perl-5.24.0/lib/5.24.0/x86_64-linu +x /home/me/perl5/perlbrew/perls/perl-5.24.0/lib/5.24.0 .) at /home/me +/perl5/perlbrew/perls/perl-5.24.0/lib/site_perl/5.24.0/sys/syscall.ph + line 9.

    In order to fix such things, I searched for the corresponding ph file generated by h2ph in $Config{'installsitearch'} and created a symbolic link to the directory by using the expected alias, for instance:

    ln -s /home/me/perl5/perlbrew/perls/perl-5.24.0/lib/site_perl/5.24.0/x +86_64-linux/x86_64-linux-gnu/bits /home/me/perl5/perlbrew/perls/perl- +5.24.0/lib/site_perl/5.24.0/bits

    That works... but I had to repeat that three times to fix all the errors. An educated guess is that I'm going to repeat the process for every instance of Perl installed with Perlbrew (I have several).

    Is there a cleaner way to do that? Maybe a different options when compiling perl?

    Thanks in advance,

    Alceu Rodrigues de Freitas Junior
    ---------------------------------
    "You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill
portability / system / elegance
3 direct replies — Read more / Contribute
by skendric
on Dec 04, 2016 at 08:19

    I'm writing semi-portable code (Linux / Windows).

    I use 'system' to execute an external binary ('tshark', part of the Wireshark collection).

    I handle slashes like this:

    use File::Which qw(which); $tshark_binary = which('tshark'); $tshark_binary =~ s/\\/\\\\/g if $tshark_binary =~ /\\/;

    The last line functions essentially as a "If this is Windows, escape the slashes in the path name", such that 'C:/Program Files/Wireshark/tshark.EXE' turns into 'C://Program Files//Wireshark//tshark.EXE'.

    And then, when I actually execute tshark, I use the following:

    system("\"$tshark_binary\" -r $pcap -e frame.number -e frame.time_epo +ch -e ip.src -e ip.id -T text -T fields -E separator=, > $temp_file") +;

    The key 'portability' change being the escaped quotes around $tshark_binary, which aren't necessary in Linux but are needed under Windows, to dodge the: "C:\Program" is not an executable errors.

    This is fine, and it works. But is there a more elegant / common way to accomplish these two tasks?

    --sk
perl DBI statement
5 direct replies — Read more / Contribute
by cbtshare
on Dec 03, 2016 at 23:30

    I have a small script which uses perl DBI to connect to two databases and transfer content of one table(prod) to another table in another DB(prod).I can connect and select data, but the insert statement doesn't work and I get no errors, help please,.

    I am getting data in the variable @array

    my $table_results= $dbh2->prepare("SELECT * from $feed_table WHERE ent +ry_time >= $time"); $table_results->execute(); #Prepare insert statement $sth_insert = $dbh3->prepare("INSERT IGNORE INTO $feed_table (id_code, +entry_time,parent_id_code,author_name,author_code,author_url,author_i +mage_url,entry_url,entry_types,status_code,entry_text,entry_data,last +_update,pull_time,queue_code) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? +, ?, ?, ?, ?)") or die $dbh3->errstr; while (my @insert = $table_results->fetchrow_array()) { $sth_insert->execute(@insert) or die $dbh->errstr; #I get no errors }

    My data structure looks like the following

    Warning: Using a password on the command line interface can be insecur +e. *************************** 1. row *************************** id_code: 10153812429735_10154227424399735 entry_time: 2016-10-24 21:13:32 parent_id_code: 28193we4734_10153815104343735 author_name: Chweweles+Lawevin author_code: 102057rw81019807 author_url: https://www.facebook.com/1020dfdfw81019807 author_image_url: http://graph.facebook.com/102fdf1019807/picture entry_url: https://business.facebook.com/2819gf4734/posts/101gf +g815100339735?comment_id=101db8699735 entry_types: PHOTO COMMENT status_code: MOD entry_text: dfdppe+Lafdvin entry_data: {"is_hidden":false,"can_hide":true,"can_remove":true +,"can_comment":true} last_update: 0000-00-00 00:00:00 pull_time: 2016-11-16 19:07:27 queue_code: NULL

    My statement @insert = $table_results->fetchrow_array() does have data, but it doesn't seem to enter it in the DB in staging. If I do a print of @insert, I get :

    Use of uninitialized value $insert[14] in join or string at /home/anw/ +rds/rdscopy.pl line 99. 862010910596315 2016-10-28 23:32:50 78i404507 +1669_861799288022 Sally+Hill+Deehan 10154kjip13891 https://www.facebo +ok.com/101o8ukuk9002413891 http://graph.facebook.com/10154o8jkjy41389 +1/picture https://business.facebook.com/2819iukjk734/posts/86176kkjkj +288022?comment_id=862010910596315 REMOTE MENTION COMMENT MOD Go+Cubs% +21 {"is_hidden":false,"can_hide":false,"can_remove":false,"can_commen +t":false} 0000-00-00 00:00:00 2016-11-16 18:53:39 Use of uninitialized value $insert[14] in join or string at /home/aw/r +ds/rdscopy.pl line 99.

    I also have tried entering the data manually and it works, so there seems to be an issue with just the insert statement. Is there a way to print the insert statement being issued to the DB with the values so I can see what is being passed? My insert statement is below

    mysql -hicuc-staging.czrsyfffvbk.us-west-2.rds.amazonaws.com -uroot -pftrol^16 -e "INSERT INTO sstaging_db.FACEBOOK_table34 (id_code, entry_time, parent_id_code, author_name, author_code, author_url, author_image_url, entry_url, entry_types, status_code, entry_text, entry_data, last_update, pull_time, queue_code) VALUES (\"1013555100339735_101542277569735\", \"2016-10-24 21:13:32\", \"281944734_1015384444339735\", \"Cles Lang\", \"10205451019807\", \"https://www.facebook.com/10205449807\", \"http://graph.facebook.com/1020565651019807/picture\", \"https://business.facebook.com/28193yty4734/posts/10153815100339735?comment_id=1015422888888699735\", \"PHOTO COMMENT\", \"MOD\", \"Ph+Lang\", \"{"is_hidden":false, "can_hide":true, "can_remove":true, "can_comment":true}\", \"0000-00-00 00:00:00\", \"2016-11-16 19:07:27\", NULL)"
One-liner search and replace with multiple files...on Windows.
3 direct replies — Read more / Contribute
by Anonymous Monk
on Dec 03, 2016 at 15:30
    Hi Monks.

    After find the following node, One-Liner for In Place Text Replacement of Multiple Files, I was wondering if there was a solution for Windows users? For example,

    perl -pi.bak -e "s/cheese/soup/g" *.txt


    ...was unsuccessful so I tried the following:

    perl -pi.bak -e "for(<*.txt>) {s/cheese/soup/g}"


    ...but Perl displays the following response:

    -i used with no filenames on the command line, reading from STDIN.

    Assuming this is possible on a Windows machine, any help would be appreciated.

    Thanks.

    Sarah.
Strawberry CPAN
4 direct replies — Read more / Contribute
by LloydRice
on Dec 03, 2016 at 15:17
    What's the best way to load CPAN modules with Strawberry. I tried cpan console Things looked OK for a while, then it said Cannot install conosole, don't know what it is From there, things went downhill. Is there a better way?
Not able to execute perl sub routine
4 direct replies — Read more / Contribute
by sachin raj aryan
on Dec 03, 2016 at 01:38

    I have two files one unzipping.pl and another Main_Start_File.pl. I am calling unzipping.pl from Main file ... problem is that when i uncomment the commented part of Main file it is not executing unzipping.pl whereas when i run it with commented part it is going to unzipping and unzipping all file below is my code

    main_file.pl #!/usr/bin/perl use strict; use warnings; use Net::FTP::Recursive ; require "unzipping.pl"; my $host = "xx.xx.xx.xx"; my $user = "tt"; my $password = "tt"; my $basedir = "locations"; my$reportfldr = "reports"; my $filename = 'List.txt'; my $date = "20161128"; #my $brchid="b00330"; ############Fetching report from Server#################### open(my $fh, '<:encoding(UTF-8)', $filename)or die "Could not open fi +le '$filename' $!"; while ( my $brchid = <$fh>) { chomp $brchid; print "$brchid\n"; # system "mkdir $brchid"; # chdir($brchid); # system "mkdir $date"; # chdir ($date); # my $dir = join"/",$basedir,$brchid,$reportfldr,$date; # my $f = Net::FTP::Recursive->new($host,Debug => 0) or die "Can't + open $host\n"; # $f->login($user, $password) or die "Can't log $user in\n"; # $f->binary(); # $f->cwd($dir) or die "Can't cwd to $dir\n"; # $f->dir(); # $f->rget(); # $f->quit; # print " I am entering into subroutine \n"; &unzipping($brchid,$date); # sleep 5; # chdir "../.."; } unzipping.pl #!/usr/bin/perl use strict ; use warnings ; use IO::Uncompress::Gunzip qw(gunzip $GunzipError); sub unzipping{ my $brchdir = $_[0]; my $date = $_[1]; my $dir1 = "Transaction_Reports"; my $dir2 = "Reports_On_Request"; my $dir3 = "Other_Reports"; my $dir4 = "MIS_Reports"; my $dir5 = "Letters_Reminders"; my $dir6 = "Exception_Reports"; my $dir7 = "Concurrent_Audit_Reports"; my $dir8 = "Adhoc_Audit_Reports"; my $dir = $brchdir."/".$date; print "I m in unzipp dir $dir \n"; my $dirf=$dir."/".$dir1; my $dirs = $dir."/".$dir2; my $dirt = $dir."/".$dir3; my $dirft = $dir."/".$dir4; my $dirfv = $dir."/".$dir5; my $dirsx = $dir."/".$dir6; my $dirsn = $dir."/".$dir7; my $direi = $dir."/".$dir8; print "I m here \n" ; print " $dir"; for my $input ( glob "$dir/*.gz" ) { print " I m inside for $dir \n"; my $output = $input; $output =~ s/.gz// ; gunzip $input => $output or die "Error Uncompressing '$input': $GunzipError\n"; } } 1;

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.