Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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
Finding what modules use and what uses the modules
1 direct reply — Read more / Contribute
by Lady_Aleena
on May 26, 2017 at 21:16

    As some of you know, I'm doing an audit of my code. Part of that audit is finding what modules my modules use and what modules I have written are used by other modules I have written. If I ever get things nice enough in some of my modules, I might one day put a few up on CPAN. However, I need to know what modules my modules use so I can include them in the install if the user does not already have. Like, if I use Lingua::EN::Inflect in a module (or group of modules), it would need to be installed if the user does not have it already. At least I think so.

    I came up with the following code to find the information but there might be a better way.

    #!/usr/bin/perl use strict; use warnings; use File::Find; my @files; sub wanted { my $file = $_ =~/.pm$/ ? $File::Find::name : undef; push @files, $file if $file; } my @directories = ('/home/me/Documents/'); find(\&wanted, @directories); my $module_directory = $directories[0].'www/files/lib/'; my $modules; for my $file (@files) { open my $fh, '<', $file or die "$file: $!"; my $file_convert = $file; $file_convert =~ s/$module_directory(.+)\.pm/$1/; $file_convert =~ s/\//::/g; while (<$fh>) { chomp; if ($_ =~ s/^use ((:|\w)+)(.+)/$1/) { push @{$modules->{$file_convert}{'uses'}}, $1; # what modules + the module uses push @{$modules->{$1}{'used by'}}, $file_convert; # what modules + use this module } } } my $in = shift; use Data::Dumper; print Dumper($modules->{$in});

    Here is example output.

    me@office:~$ perl Documents/scripts/ 'RolePlaying::R +andom' $VAR1 = { 'used by' => [ 'RolePlaying::CharacterMutation', 'RolePlaying::Random::Range', 'RolePlaying::Random::Time', 'RolePlaying::Random::SpecialAttack', 'RolePlaying::Random::Title', 'RolePlaying::Random::WildPsionics', 'RolePlaying::Random::Descriptor', 'RolePlaying::Random::Spell', 'RolePlaying::Random::SavingThrow', 'RolePlaying::Random::Color', 'RolePlaying::Random::Monster', 'RolePlaying::Random::GemMetalJewelry', 'RolePlaying::Random::Thing', 'RolePlaying::Random::Weapon', 'RolePlaying::Random::Size', 'RolePlaying::Random::Food', 'RolePlaying::Random::Misc', 'RolePlaying::Random::Event', 'RolePlaying::Random::Water', 'RolePlaying::Random::Class', 'RolePlaying::Random::MagicItem', 'RolePlaying::Random::Color::VisiBone', 'RolePlaying::Random::Body::Function', 'RolePlaying::Random::Body::Modification' ], 'uses' => [ 'strict', 'warnings', 'Exporter', 'List::Util' ] };

    If you know of a better way that will give me deeper or more sophisticated results, I am interested.

    No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
    Lady Aleena
How to properly use ExtUtils::MakeMaker PL_FILES attribute?
1 direct reply — Read more / Contribute
by glasswalk3r
on May 26, 2017 at 20:13

    Hello Monks

    The ExtUtils::MakeMaker Pod says the following regarding PL_FILES attribute:

    MakeMaker can run programs to generate files for you at build time. By default any file named *.PL (except Makefile.PL and Build.PL) in the top level directory will be assumed to be a Perl program and run passing its own basename in as an argument. This basename is actually a build target, and there is an intention, but not a requirement, that the *.PL file make the file passed to to as an argument.
    PL files are normally run after pm_to_blib and include INST_LIB and INST_ARCH in their @INC, so the just built modules can be accessed... unless the PL file is making a module (or anything else in PM) in which case it is run before pm_to_blib and does not include INST_LIB and INST_ARCH in its @INC.

    Since I'm looking for to generate a module after the Makefile.PL is executed (so all dependencies would be properly addressed), I would need this module before the pm_to_blib phase, but it seems we have some implicit control over there. Is there any way to guarantee when the PL files will be executed within the Makefile (before or after pm_to_blib)?


    Alceu Rodrigues de Freitas Junior
    "You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill
Calling a subroutine when part of call is a variable Contant
3 direct replies — Read more / Contribute
by Anonymous Monk
on May 26, 2017 at 18:13

    Dear Monks, Hoping you can help. I can call the subroutine first_name() like this:


    However I want to call it when 'first_name' is a constant. The following code doesn't work

    use constant { STUFF => { 'name1' => 'first_name', 'name2' => 'last_name', }, }; foreach my $val (keys ${\( STUFF() ) } ){ my $value = eval("$self->xml()->${\( STUFF() ) }->{ $val }()"); }

    I can't use a code ref like below because of how it needs to be called ( perhaps there is a way? ) Note: I cannot use Readyonly either. I've also tried various combinations of \&{$variable} but can't seem to get it to work.

    use constant { STUFF => { 'name1' => $self->xml()->first_name(), 'name2' => $self->xml()->last_name(), }, };

    Please help if you can. Thanks

HTML::Parser / Regex
4 direct replies — Read more / Contribute
by MissPerl
on May 26, 2017 at 16:30
    Hi fellow Perl Monks,

    I am trying to get text/number in a html file then store them into a variable. I know that HTML::TableExtract or some other module might have easier way to do this. But for now, I want to learn and apply HTML::Parser and regex first.

    This is part of my failed attempt perl script, it got errors like bareword found (might be runaway multi-line) and can't use global $1 in my. At the beginning of the script, it prompt user for input then store them into a variable. For now, I am writing the part for the script to be able to reads the $ca html file and find match. Then next part of the script will continue for the other states' html file.

    use HMTL::Parser; my $ca = "california.html"; open (my $f1, "<" , $ca) || die ("Can't open file : california.html"); while (<$f1>){ if (my $text =~ /Employee\sA</th><th>.\d</){ my $one = $1; }elsif (my $text =~ /Employee\sB</th><th>.\d</){ my $two = $1; }elsif (my $text =~ /Employee\sC</th><th>.\d</){ my $three = $1; } } close ($f1);
    Below are a few lines from two different html files.
Log On To Secure Site
3 direct replies — Read more / Contribute
by Kass
on May 26, 2017 at 11:41
    I would like to be able to log on to secure sites such as the Dallas Library and Optionsxpress programmatically in order scrape them. I've tried using Curl, but always get stuck. Help would be greatly appreciated. Jerry Kassebaum
PDF::API2::Outline functions
3 direct replies — Read more / Contribute
by Copacetic
on May 26, 2017 at 11:07

    I have a PDF which doesn't have an index (which I believe is called an "outline"). I'd like to create one for it with links to various pages within the document. I've looked at the PDF::API2::Outline docs but they don't describe the parameters for each function, or include any examples. For instance, I don't know what any of the parameters for new() should be, or how the functions here relate to $otls->outline in PDF::API2.

    I haven't posted any code, as there are lots of parameters I could use which don't work.

    Any help would be much appreciated.

Sorting files by 3 numbers in the name
7 direct replies — Read more / Contribute
by crusty_collins
on May 26, 2017 at 09:37
    I have a task that requires me to sort several files by 3 numbers in the file name. What I have so far is a sort by a single number (run).
    Question : How can I sort by 3 numbers
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; # sort by run then by dist then by copy then by total # run district copy t +otal # | | | | #ASR0004994_8958_ETSTexas_EOC052017P_0517_Candidate_RRD_178901_02_04_S my @files = qw( ASR0005336_8950_ETSTexas_EOC052017P_0517_Candidate_RRD_178904_01_0 ASR0004520_8960_ETSTexas_EOC052017P_0517_Candidate_RRD_178901_04_0 ASR0004994_8958_ETSTexas_EOC052017P_0517_Candidate_RRD_178901_02_0 ASR0005336_8950_ETSTexas_EOC052017P_0517_Candidate_RRD_178904_02_0 ASR0005154_8957_ETSTexas_EOC052017P_0517_Candidate_RRD_178901_01_0 ASR0005336_8959_ETSTexas_EOC052017P_0517_Candidate_RRD_178901_03_0 ASR0005336_8972_ETSTexas_EOC052017P_0517_Candidate_RRD_178902_01_0 ); # this sorts by the run number my @returnfiles = sort { ( $a =~ /^[^\d]*\d+_(\d{4})/ )[0] <=> ( $b =~ + /^[^\d]*\d+_(\d{4})/ )[0] } @files ; print Dumper @returnfiles;
    "We can't all be happy, we can't all be rich, we can't all be lucky and it would be so much less fun if we were. There must be the dark background to show up the bright colours." Jean Rhys (1890-1979)
Read Directory
4 direct replies — Read more / Contribute
by Nansh
on May 26, 2017 at 02:16
    Hi, My code is like this
    #!usr/bin/perl $path='/home/something/something/something'; opendir(DIR,$path) or die"couldnt open the directory\n"; while(readdir(DIR)) { if(-d BUS) { opendir(VEH,"BUS") or die"couldnt open the directory\n"; while(readdir(VEH)) { print "$_\n"; } } }

    In this path $path='/home/something/something/something'; many directories are there. say BUS, CAR, LORRY If the directory BUS exists then read that directory and display the files in that directory I am trying but i am not able to get that

Moose: Giving objects access to data of class that created it
4 direct replies — Read more / Contribute
by nysus
on May 25, 2017 at 15:28

    I'm curious to know how I might give objects created by a parent class access to the data in the parent class:

    { package parent 0.000001; use Moose; has 'changing_data' => (is => 'rw', isa => 'Str', default => ''); sub set_data { my $s = shift; $s->changing_data('foo foo foo'); } sub create_child { my $s = shift; my $data = shift; return child->new(changing_data => $data); } } { package child 0.00001; use Moose; extends 'parent'; } my $p = parent->new(); $p->set_data; my $child = $p->create_child($p->changing_data()); print $child->changing_data; $p->changing_data('bar bar bar'); print "\n"; print $child->changing_data; # desired output foo foo foo bar bar bar # actual output foo foo foo foo foo foo

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

Subroutine processes only one file in the directory
1 direct reply — Read more / Contribute
by sridhar56
on May 25, 2017 at 15:22
    I have a directory of text files and I want to process each file in that directory using the subroutine average to calculate the average for a file. However, my script processes the same file every time. I am not sure what is the mistake I am doing here. Below is the code I wrote:
    #!/usr/bin/perl #Naga_betrapally_6_3_16 use strict; use warnings; use Cwd; use FileHandle; my $name; + # Naming of variables, remembers the name of the +file my $cwd = getcwd(); + #Takes in the current directory of files my $opendir = "$cwd"; + #Used to open the current directory and remembers t +he location opendir (DIR, $opendir) or die $!; + #Open directory command my @directory = readdir DIR; + #Grabbing all the fas files stored in the location my $scalar = scalar(@directory); + #Counting the number of files and printing it in th +e next line #print "The number of sample files in this folder: $scalar \n"; # for (my $i=0; $i<scalar(@directory); $i++){ # $name = "$directory[$i]"; # print "The file read in: $name\n"; # my $digits = substr $name, 0, 4; + #Grabbing the first 4 characters to name the sample + run for MEGA # my $tmp = "$opendir/$directory[$i]"; # } for(my$i=0;$i<@directory;$i++){ print $directory[$i]; &average($directory[$i]); } #average(@directory); sub average { my $header; my @array_lines; my $n=0; my $filename=@_; + #Takes in the sequence file from the user inpu +t with the year in the header my @average_array; my $sum =0; my $avarage; next if($filename =~ /^\.$/); next if($filename =~ /^\.\.$/); open (FILE,$filename) or die "No input file provided or the input file + does not exist in the path, '$_' $!"; print "$filename\n"; while (my $file = <FILE>){ chomp $file; if ($file =~/^Name/){ $header = $file; } else{ $array_lines[$n] = $file; chop $array_lines[$n]; $n++; } } for(my $i=0;$i<@array_lines;$i++){ my @tmp = split(/\t/,($array_lines[$i])); #print $tmp[3]; push @average_array, $tmp[3]; my $scalar = scalar(@average_array); #print $scalar; } foreach (@average_array){ #print "$_\n"; my $tmp = $_; $tmp =~ s/,//; #print "$tmp\n"; $sum = $sum+$tmp; } #print "The sum of all the mapping is $sum\n"; $avarage = $sum/11; #print "THe average of the mapping is $avarage\n"; close (FILE); }
    All the files in that folder maintain the same structure as the sample file below:
    Name Consensus length Total read count Average coverage Re +ference sequence Reference length NSP4-E1 mapping 750 227760 64,516.84 NSP4-E1 750 VP7-G3 mapping 1062 303758 63,062.33 VP7-G3 1062 Rotarix-VP6 mapping 1356 161491 26,020.50 Rotarix-VP6 1 +356 Rotarix-NSP1 mapping 1559 114394 16,077.21 Rotarix-NSP1 + 1568 NSP2-N1 mapping 1059 80424 16,178.61 NSP2-N1 1058 NSP5-H1 mapping 664 75269 23,943.71 NSP5-H1 663 VP1-R1 mapping 3302 69677 4,542.15 VP1-R1 3302 VP2-C1 mapping 2717 149312 11,987.00 VP2-C1 2717 VP3-M1 mapping 2591 31795 2,632.39 VP3-M1 2591 RotaTeq-WI79-4-VP4 mapping 2359 78305 7,223.08 RotaTeq-WI7 +9-4-VP4 2359 NSP3-T1 mapping 1074 173210 34,910.87 NSP3-T1 1074

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 all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (7)
    As of 2017-05-27 10:53 GMT
    Find Nodes?
      Voting Booth?