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
Storing the log file name as key/value in hash
2 direct replies — Read more / Contribute
by Magnolia25
on May 28, 2017 at 02:39
    Hi. I need a help on parsing the file name and save the file names contents into a hash as key/value. Below are the representation of my log file looks stored in an array
    ULTRIX.CW18.72.0.3.IP-HOST1.log DEC_DECSTATION.CW180.72.0.3.IP_HOST_AL.log DEC_DECSTATION_ADDR.CW180.72.0.3.IP_HOST_al2.log FOR_VISITORS23_HOST.HL617.253.1101.2.IP_HOST_hostinfo.log FOR_VISITORS24_HOST.HL617.253.1101.2.IP_HOST_hostinfo2.log FOR_VISITORS25_HOST.HL617.253.1101.2.IP_HOST_webform3.log
    File name format : <String(alphanumeric,_)>.<UniqueIdentifier(alphanumeric,.)>.<String(alphanumeric,_)>.log

    for example for below set of files

    DEC_DECSTATION.CW180.72.0.3.IP_HOST_AL.log DEC_DECSTATION_ADDR.CW180.72.0.3.IP_HOST_al2.log
    I need CW180.72.0.3 as my key, and DEC_DECSTATION , DEC_DECSTATION_ADDR as my values.

    specifically I want to store the result as below and write them later to a file as key and value.

    $HASH1 = { 'CW18.72.0.3' => ['ULTRIX'], 'CW180.72.0.3' => ['DEC_DECSTATION','DEC_DECSTATION_ADDR'], 'HL617.253.1101.2' => ['FOR_VISITORS23_HOST','FOR_VISITORS24_HOST','FO +R_VISITORS25_HOST'] };
    Please help on this. Thanks.
Is there a "standard" way to link to other modules in POD for a CPAN module?
4 direct replies — Read more / Contribute
by atcroft
on May 27, 2017 at 14:33

    Earlier this week, I was looking for information on a module on search.cpan.org. While reading through the module's documentation, there were several related modules mentioned, but the links referred to the module on metacpan.org.

    Curious, I viewed the module's POD, and found they had used L<text|url> POD linking syntax. When I later viewed the POD of a few other modules that referred (linked) to other modules in a way I expected, I found those modules using the L<module> / L<module/section> / L<text|module> / L<text|module/section> syntax.

    To me, this violated the Principle of Least Astonishment (Surprise), but I can see a scenario where the author may not have noticed the issue, such as if (for instance) they use metacpan.org as their preferred interface for finding modules on CPAN. I considered contacting the module's author regarding the linking, but before I potentially made (more of) a fool of myself (than normal) by doing so (and because I am still (at best) a novice with respect to POD), I wanted a "second opinion" (as it were). So, my question is:

    Is there a "standard" manner for linking to other modules within CPAN I should recommend, or should I just ignore it as being my issue, not an actual problem worthy of disturbing the author?

    My thanks in advance for your shared wisdom and consideration.

How do you run a perl script using a custom perl build from an AppleScript?
3 direct replies — Read more / Contribute
by nysus
on May 27, 2017 at 13:54

    I'm creating commands that I can execute by voice for my Mac. In order to do this, I want to create AppleScripts that call my perl script. For example, here's an AppleScript that will call a Perl script which moves files off the Desktop and into Documents folder:

    do shell script "/Users/stevied/bin/clean_desktop.pl"

    The Perl script works fine when run from the command line, but when executing the script from my AppleScript, the modules I use aren't found and I get this error:

    Can't locate Modern/Perl.pm in @INC (you may need to install the Moder +n::Perl module) (@INC contains: /Library/Perl/5.18/darwin-thread-mult +i-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 +/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 .) at /Users/stevied/bin/clean_desk +top.pl line 3. BEGIN failed--compilation aborted at /Users/stevied/bi +n/clean_desktop.pl line 3.

    I have perlbrew installed. The current shebang line for the perl script is #! /usr/bin/env perl. I tried adding the following line to my perl script:

    /Users/stevied/perl5/lib/perl5;

    Running the AppleScript now yields this horrific looking error:

    dyld: lazy symbol binding failed: Symbol not found: _Perl_xs_handshake Referenced from: /Users/stevied/perl5/lib/perl5/darwin-thread-multi- +2level/auto/Mac/SystemDirectory/SystemDirectory.bundle Expected in: flat namespace dyld: Symbol not found: _Perl_xs_handshake Referenced from: /Users/stevied/perl5/lib/perl5/darwin-thread-multi- +2level/auto/Mac/SystemDirectory/SystemDirectory.bundle Expected in: flat namespace

    Is there anything I can do to get this working?

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

Web Works
3 direct replies — Read more / Contribute
by Sandy
on May 27, 2017 at 11:52
    I am currently committed to helping a bunch of math profs learn perl, because they are using a program called WebWorks, which is perl based.

    I looked at web-works, and it is a web-based tool that requires apache/mod_perl etc. to work.

    webwork

    However...

    I was hoping to just install the perl, so that I can play around with the perl classes without having to worry about web-servers etc (and to minimize space on my home computer).

    Has anyone done this? Any advice?

    Sandy

Finding what modules use and what uses the modules
4 direct replies — 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/find_module_use.pl '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)?

    Thanks!

    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:

    $self->xml()->first_name();

    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
5 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
4 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.


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.