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
Passing data to getopts::long
2 direct replies — Read more / Contribute
by neilwatson
on Jul 28, 2015 at 19:56

    Greetings, Is the following possible? I'm making a template for future scripts and programs. I thought it would be useful to separate the standard cli args (man, help, usage, etc) to a separate package. My first try returns an error. Why?

    #!/usr/bin/perl use strict; use warnings; use feature 'say'; use Getopt::Long; use Pod::Usage; use Data::Dumper; my $VERSION = 1; # Get standard options from reusable module my $stdopts = Std::Opts->new(); my $std_cli_arg_ref = $stdopts->get_standard_args; # Read, process, and validate cli args my $cli_arg_ref; GetOptions( $cli_arg_ref, $std_cli_arg_ref, # These two samples can be removed 'myarg=s', 'arg2=i', ); # # Modules # package Std::Opts; use strict; use warnings; sub new { my ( $class ) = @_; return bless {}, $class; } sub get_standard_args { my $self = shift; my $std_cli_arg_ref = { 'version' => sub { say $VERSION; exit + }, 'test' => sub { _run_tests(); exit + }, 'man' => sub { pod2usage( -verbose => 2, -exitval => 0 ) + }, 'dumpargs' => sub { say '$cli_arg_ref = '. Dumper( $cli_arg_ref ); exit }, 'help|?' => sub { pod2usage( -sections => ['OPTIONS'], -exitval => 0, -verbose + => 99) }, 'usage' => sub { pod2usage( -sections => ['SYNOPSIS'], -exitval => 0, -verbose + => 99) }, 'examples' => sub { pod2usage( -sections => 'EXAMPLES', -exitval => 0, -verbose + => 99) }, }; return $std_cli_arg_ref; } 1; Returns: $ ./foo.pl -du Undefined argument in option spec Error in option spec: "HASH(0x1772fc8)"

    Neil Watson
    watson-wilson.ca

User preference: To store params or not
1 direct reply — Read more / Contribute
by stevieb
on Jul 28, 2015 at 18:42

    My larger projects require params to be passed in via hashref for simplicity, but I'm wondering from a user perspective which is more convenient... storing the non-core params for continuous use, or reset the non vital ones so they have to be resent in.

    If they are stored, it requires a reset on the params that you want changed (ie. you have to reset a parameter to false on a subsequent run with the same object). What's your opinion on storing or wiping params?

Working with Confluence and LWP
3 direct replies — Read more / Contribute
by TStanley
on Jul 28, 2015 at 13:43
    My company recently bought the Confluence software from Atlassian. Confluence is a web based collaboration system, allowing users to upload
    documents and other content to their personal pages, and to provide for project teams to collaborate together and share documentation. The software
    also tracks changes to the documentation as well.

    I have been tasked with automating some tasks that occur on a daily basis, mostly updating verification reports of our various databases, and then
    publishing those to Confluence.

    Confluence has several APIs, and the easiest for me to use was the RPC-XML client, as I could do it with perl. A search of CPAN gave me the Confluence::Client::XMLRPC module. So I created the following short script, to make sure I could do what I want:
    #!C:\Perl64\bin\perl use strict; use warnings; use Confluence::Client::XMLRPC; my $version=2; Confluence::Client::XMLRPC::setApiVersion($version); Confluence::Client::XMLRPC::setRaiseError(1); Confluence::Client::XMLRPC::setPrintError(1); my $URL="https://jeeves.Mainoffice.Demoulas.Corp/rpc/xmlrpc/"; my $user="toms"; my $pass="password"; my $object = Confluence::Client::XMLRPC->new($URL,$user,$pass); $object->logout();
    which produced the following error:
    XML-RPC ERROR: Unable to connect to https://jeeves.Mainoffice.Demoulas +.Corp/rpc/xmlrpc at C:\Users\toms\Documents\Confluence\confluence.pl line 15
    After a great deal of trial and error, I got the code below working:
    #!C:\Perl64\bin\perl use warnings; use strict; use LWP::UserAgent; my $ua=LWP::UserAgent->new(ssl_opts=>{ verify_hostname => 0}); my $req = HTTP::Request->new(GET => 'https://jeeves.mainoffice.demoula +s.corp'); my $result = $ua->request($req); if($result->is_success){ print $result->content; }else{ print $result->status_line, "\n"; }
    The catch is where I create the LWP object, telling the agent to not verify the hostname of the server. If I remove the ssl_opts, it will say it can not connect due to failing the certificate verification. The Confluence client in the first bit of code uses LWP and LWP::Protocol::https to handle the http/https part of connecting to the server. What I need to figure out is how I can pass the ssl_opts.

    I am currently testing all of this on Windows 7 (64 bit), running ActiveState Perl 5.18.2


    TStanley
    --------
    People sleep peaceably in their beds at night only because rough men stand ready to do violence on their behalf. -- George Orwell
hex escape Question
1 direct reply — Read more / Contribute
by mfawbush
on Jul 28, 2015 at 13:30

    Hello, I've been beating on this for a while now that I'm just plumb confused now. I'm sure it's so simple and it's right in front of me. Need to send a five byte block to serial port. The snippet of code below works, however I need to replace all the values behind the hex escape "\x" with what the user puts in from STDIN strings. I've tried pack and hex with no success. Thank you in advance for your help.

    #Works $port->write("\x01\x42\x62\x00\x01");
use File::Path qw(make_path); fails with php 5.6.11
3 direct replies — Read more / Contribute
by windjammer
on Jul 28, 2015 at 11:07

    I have recently upgraded my php from 5.3.8 to 5.6.11 (security issues requirment).

    Previous: PHP 5.3.8 (cli) (built: Sep 19 2011 13:29:27) Copyright (c) 1997-2011 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies Current: PHP 5.6.11 (cli) (built: Jul 15 2015 21:26:53) Copyright (c) 1997-2015 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies use File::Path qw(make_path);
    Any idea why this fails?
Test::Exception error on Strawberry Perl 5.14.3 ('Bareword "xception" ...')
2 direct replies — Read more / Contribute
by 1nickt
on Jul 28, 2015 at 11:04

    Hello monks,

    Before I file an issue report I seek to check my sanity here. I just got a Fail from CPAN testers for a recent update to a module that implemented Test::Exception.

    ------------------------------ PROGRAM OUTPUT ------------------------------ Output from 'C:\strawberry5143\c\bin\dmake.exe test': C:\strawberry5143\perl\bin\perl.exe "-MExtUtils::Command::MM" "-e" "te +st_harness(0, 'blib\lib', 'blib\arch')" t/*.t t/10_obj_and_params.t ............... ok t/20_valid_drupal.t ................. ok t/30_build_and_validate_test_db.t ... ok Bareword "xception" not allowed while "strict subs" in use at C:/straw +berry5143/perl/vendor/lib/Test/Exception.pm line 4. Compilation failed in require at t/40_fetch_memberships.t line 14. BEGIN failed--compilation aborted at t/40_fetch_memberships.t line 14. t/40_fetch_memberships.t ............ Dubious, test returned 255 (wstat 65280, 0xff00) Failed 1/1 subtests t/50_term_object_params.t ........... ok t/55_term_object_functions.t ........ ok t/60_membership_object_params.t ..... ok t/65_membership_object_functions.t .. ok t/70_stats_object_parameters.t ...... ok t/75_stats_functions.t .............. ok t/release-pod-coverage.t ............ skipped: these tests are for rel +ease candidate testing t/release-pod-syntax.t .............. skipped: these tests are for rel +ease candidate testing Test Summary Report ------------------- t/40_fetch_memberships.t (Wstat: 65280 Tests: 0 Failed: 0) Non-zero exit status: 255 Parse errors: Bad plan. You planned 1 tests but ran 0. Files=12, Tests=36, 7 wallclock secs ( 0.04 usr + 0.12 sys = 0.16 C +PU) Result: FAIL Failed 1/12 test programs. 0/36 subtests failed. dmake.exe: Error code 255, while making 'test_dynamic'

    Notes:

    • Two tests before, and two tests after, which pass, also use Test::Exception.
    • The test report shows it is using Test::Exception v0.31, whereas the current version is 0.40.
    • All tests passed on Strawberry Perls 5.10.1, 5.16.1 and 5.16.2

    Any suggestions would be very welcome.

    The way forward always starts with a minimal test.
Consistent xml formatting
1 direct reply — Read more / Contribute
by Haloric
on Jul 28, 2015 at 10:05

    Hi Monks,

    I have many xml files that I am attempting to compare with either XML::SemanticDiff or XML::SemanticCompare.

    Is there a way to get the XML ordered in a consistent way before I start this comparison, without calling out to a separate tool. They both get confused by slight ordering changes

    For example, I have elements that are

    <sequence name="b" .. <sequence name="a" .. <sequence name="c" ..

    I would like them ordered by the 'name' attribute before the comparison starts.

    I have looked at XML::LibXML::PrettyPrint but can't make it do what I want.

    I can see SemanticDiff copes with missing attributes, but not sure how it would cope with a missing '<sequence name="b" ' element all together without thinking it was a difference in attribute value, rather than entirely missing.

    Thanks

perl: comparing words in the arrays
3 direct replies — Read more / Contribute
by GSperlbio
on Jul 28, 2015 at 09:19
    I have two sequences stored in two arrays as shown below: @seq1 = "ATGC TGCT GCTA CTAA TAAC" @seq2 = "GTCA" i want to compare string in @seq2 with each and every words in @seq1 and need to print the position and no of mismatches at that position. For example in the first index while comparing ATGC with GTCA there are 3 mismatches, and in the second index while comparing TGCT with GTCA there are 3 mismatches and so on. It would be grateful if anyone show me the code to do the above process. Thank you..
"used only once" for symbolic references
3 direct replies — Read more / Contribute
by irata73
on Jul 28, 2015 at 06:54
    Hi, the script:
    use strict; use Data::Dumper; { my( $key ) = 'Statistic::Test'; my( $val ) = [ 1, 2, 3, 4 ]; { no strict 'refs'; ${$key} = $val; } print Dumper( $Statistic::Test ); }
    outputs
    Name "Statistic::Test" used only once: possible typo at ref.pl line 12 +. $VAR1 = [ 1, 2, 3, 4 ];

    Is there another way than using "no warnings 'once'" to avoid the "used only once" warning?

    Thanks for your help!

How to call a perl function without its context?
7 direct replies — Read more / Contribute
by kitomer
on Jul 28, 2015 at 05:34
    I want to call a Perl function and be sure it cannot access any variables declared in its context, just its local variables and passed arguments. Is this possible?

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!
  • 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.