Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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
Get latest file created or modified & matching part of file name :
1 direct reply — Read more / Contribute
by rahulme81
on Oct 21, 2016 at 09:49

    Hello Monks

    I have a directory with a bunch of files. I'm trying to find the latest file created or modified by time in a directory and using the following pattern

    opendir(my $DIRH, $DIR) or die "Error opening $DIR: $!"; my @files = map { [ stat "$DIR/$_", $_ ] } grep( ! /^\.\.?$/, readdir( $DIRH ) ); #This find me +all files not have dot in directory #How this grep can be accommodate for my regular expre +ssion ????? closedir($DIRH); sub latestFile { $b->[0]->ctime <=> $a->[0]->ctime } my @latest_files = sort latestFile @files; my @latest = @{$latest_files[0]}; my $name = pop(@latest); print "Latest file created fro $name\n";

    This is giving the latest file in the directory, but not the file which i need as per my regular expression.

    I need to parse the file and do something, which eventually I am able to achieve

    Facing difficulty with file name pattern match and get the latest file

Module for form validation and resubmission
1 direct reply — Read more / Contribute
by Dallaylaen
on Oct 21, 2016 at 07:33

    Hello, dear esteemed monks!

    tl;dr: I'm thinking of a module that (1) compiles a set of validation rules once and (2) for each hashref given to it later, returns another object containing valid data, errors, AND initial input for processing and/or resubmission.

    Is there such a module already? If not, should I roll my own? If yes, does the API described below seem sensible?

    Now the whole story. Some time ago there was a discussion here at Perlmonks pointing out that it would be nice to make create button act as preview if post content was edited. Can't find proofs now, but the idea impressed me much.

    Recently I started looking for a form validator. My initial idea was to build a permanent validator object once and keep throwing inputs at it later. And I found a module with almost the interface I imagined - Validator::LIVR.

    However, after trying to actually implement form validation & resubmission, I found myself juggling 3 hashrefs (valid data, errors, raw user input for reentry). This was not very convenient, so I decided to pack them into one object, adding is_valid() method on top. And I found a module on CPAN with almost the interface I imagined (but without the "compile once" part) - Data::CGIForm.

    So I crossed these two and got roughly the following API:

    # initialization # the hash describes regexps, requiredness, and other checks # per input key my $validator = My::Class->new ( { ... } ); # later when processing request my $form = $validator->validate( { get => "params" } ); if ($form->is_valid) { do_something( $form->data ); redirect( "/somewhere" ); } else { show_form_again( display_errors => $form->error, input_defaults => + $form->raw ); };

    Also error content (and thus is_valid return value) can be modified, just like in Data::CGIForm:

    if (!load_user($form->data->{user})) { $form->error( user => "No such user in database" ); };

    Unfortunately, the Data::CGIForm has a fatal flaw: error() without arguments acts like my is_valid(), while separate errors() method returns all errors as hash. Error/errors is prone to typoes in my opinion, but maybe I should shut up my ego and stay compatible to an existing API instead?

    Thank you

Automating site having iframes using Selenium
2 direct replies — Read more / Contribute
by 9mohit2
on Oct 21, 2016 at 07:28

    Hi can someone please help me with some documentation for working with site having iframes using selenium in Perl. I am using Selenium::Chrome module. But I am not able to get elements inside the iframes also not getting syntax to switch back to default page content.

write to Disk instead of RAM without using modules
4 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 21, 2016 at 07:04
    I am trying to compare multiple files simultaneously for similar pattern search. I have multiple files with each file of size 500MB approximately. I have written a script which works well when I run with some of my files but when I increase the number of files, script hangs. I think it is due to memory limitation. Is there any other way of storing hash in Disk rather than RAM without using any of perl module.
First word
2 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 21, 2016 at 07:00

    I would like to get the first word from each row of my @AoA[][] and push it into the array @kmers. Can anybody help me get there? This is what I've done so far. Thanks in advance everybody!

    my(@kmers); my($x) = 0; my($y) = 0; for ($x=0, $x<7, $x++){ for ($y=0, $y<$AoA[$x], $y++){ my($firstword) = ''; $firstword = $AoA[$x][$y] =~ /^(\w+)/; push @kmers, $firstword; print $firstword."\n"; } }
Filter array of hashes for specific keys
2 direct replies — Read more / Contribute
by ddominnik
on Oct 21, 2016 at 05:35

    Say I have an array of hashes like this:

    @response = [ { 'name' => 'foo', 'id' => 'bar', 'self' => 'baz' }, { 'name' => 'foo2', 'id' => 'bar2', 'self' => 'baz2' }, ]

    Now I have a subroutine called filter which I call like this:

    my $responseref = \@response; my $filtered_response = filter ($responseref, 'name', 'id');

    How would the subroutine need to look to get an array reference as return value of which the array would look like:

    @response = [ { 'name' => 'foo', 'id' => 'bar', }, { 'name' => 'foo2', 'id' => 'bar2', }, ]

    Ideally it should work without installing any additional modules
    It should also be pretty generic, it should for example be able take a different amount of fields to filter through every time

crawl a full webpage one time
2 direct replies — Read more / Contribute
by liuweichuan
on Oct 21, 2016 at 03:56

    using Perl, how to get a full web page (include ajax web) once like phantomjs ? And what kind of brower useragent can do this?

Batch FIle Operations
4 direct replies — Read more / Contribute
by ankit.tayal560
on Oct 21, 2016 at 01:29

    Hi Monks, This is not a query related to some specific code/script but in a general scenario. suppose if I am having a script in which I have defined several (3-5) paths. I want to run this script through a batch file so I make a batch file.

    perl @pause

    Now if I want to pass the paths required through batch file only what should I do how can I pass paths through batch file directly into my script. any help or suggestions is appreciated.

    Thanks in advance

Attempt to reload LWP/Protocol/
2 direct replies — Read more / Contribute
by UMonk
on Oct 20, 2016 at 16:20
    Hi Monks,
    Am trying to hit a https - Vendor site using our proxy and trying to download a xls file, the scripting instructions are given by the Vendor themselves. Unfortunately am facing Error GETing : https://xxxxx :Attempt to reload LWP/Protocol/
    I have the old version of perl installed in the machine where it is missing some modules, so I have extracted the latest in a folder along with necessary modules and created a dummy env file which am sourcing before running the script.
    use strict; use File::Basename; use Getopt::Long; use WWW::Mechanize; use URI qw( ); my $thisScript = basename($0); print "==START $thisScript==\n"; my ($help, $proxy, $username, $password); my $urlsToBeDownloaded = "file-url-list.txt"; my $url = "https:xxxxxxxxxxxxx"; usage() if (@ARGV < 2 or ! GetOptions('proxy:s' => \$proxy, 'username=s' => \$username, 'password=s' => \$password) or defined $help); if ($username eq "" || $password eq "") { usage(); } my $mech = WWW::Mechanize->new(); if($proxy ne ""){ $mech->proxy(['http', 'https'], $proxy); } $mech->get($url);
    Am getting error right at this point I have another team who has implemented this automation successfully. Any knowledge or suggestion will be greatly appreciated.
Testing $! when use strict is in effect
2 direct replies — Read more / Contribute
by Bloehdian
on Oct 20, 2016 at 14:41

    Hi monks,

    probably a quite simple question for most of You, but I've just got stuck on it:

    In one of my progs I have the following

    if ( ! defined $test && $! == EWOULDBLOCK ) {

    which, since use strict is effect gives the error message

    "Bareword "EWOULDBLOCK" not allowed while "strict subs" in use"

    How can I test $! anyways?



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 exploiting the Monastery: (12)
    As of 2016-10-26 16:46 GMT
    Find Nodes?
      Voting Booth?
      How many different varieties (color, size, etc) of socks do you have in your sock drawer?

      Results (344 votes). Check out past polls.