Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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
OK to Include CHI File Data in module Tests ?
No replies — Read more | Post response
by localshop
on Oct 20, 2018 at 00:52
    Trying to refine WebService::GoogleAPI::Client which uses CHI to cache resources pulled from Google API Discovery. In the package tests I'd like to maximise the coverage but don't wish to impose a whole bunch of HTTP requests every time tests are run. I was thinking that by bundling in a CHI::Driver::File directory for use by the tests I could default to using this and perhaps provide a switch to allow live requests for local dev testing where needed.

    I am concerned that the cache files won't be portable across platforms but I can probably catch that out.

    1. is this approach missing any obvious bad practice
    2. are any CPAN modules that do this that I could review?
    3. any advice on testing against network resources or cached data appreciated.

    Another option that occurred to me after posting this question is to create my own CHI::Driver subclass and use that for testing with some hard coded values.

    Many thanks for any advice.

glob an array?
2 direct replies — Read more / Contribute
by morgon
on Oct 19, 2018 at 21:10

    just for curiosity...

    If I have an array of filenames and a glob-pattern (as e.g. "*.hubba"), is there a way to "glob" the array (i.e. get the list of all elements matching the glob-pattern) using the same logic "glob" would use, or would I have to translate the gob-pattern into a regex and do a grep?

    Many thanks!

First attempt at bringing in file for input/output
7 direct replies — Read more / Contribute
by catfish1116
on Oct 19, 2018 at 15:48
    I am taking baby steps to understanding out to read in, modify file(s) and then send modified file to output. Here is my attempt to just read in a file and send it to output.
    #!/bin/perl use v5.12; use warnings; my $in = $ARGV[0]; if (! defined $in) { die "Usage: $0 filename"; } my $out = $in; $out =~ s/(\.\w+)?$/out/; if (! open $in_fh, '<', $in ) { die "Can't open '$in': $!"; } if (! open $out_fh, '>', $out) { die "Can't write '$out': $!"; }

    Here is the errors that I received:

    Global symbol "$in_fh" requires explicit package name at ./exer_9_2 li +ne 14. Global symbol "$out_fh" requires explicit package name at ./exer_9_2 l +ine 1

    What 'package' am I missing?

Solved! I would have expected a syntax error here
6 direct replies — Read more / Contribute
by roboticus
on Oct 19, 2018 at 12:05

    Thanks, guys! That was a quick resolution. Indirect Object syntax was what tripped me up. As haukex suggests, I'll have to start using the Yadda, Yadda operator ... to ensure that I don't miss that in the future.

    Using that operator and a comment, I can either let the program run and execute and die only if I reach unfinished code:

    sub find_symbol { ... # got to here today! (20180520) }
    $ perl Foo! Unimplemented at line 13.

    Or if I'd rather have the program just fail without running, I can prefix my original comment with the operator (so long as I don't use a semicolon as the first character in my comment:

    sub find_symbol { ... got to here today! (20180520) }
    $ perl syntax error at line 12, near "... got to " Execution of aborted due to compilation errors.

    Really, though, not one Monty Python reference? 8^)

    Original post follows:

    Hello, gang:

    When I write experimental bits of code and don't get a chance to finish, I'll frequently mark my position with a few words and no comment marker with the intent that if I run the program, perl will tell me that bit(s) of my code are unfinished, and let me know where the unfinished bits are. This morning I decided to adapt a working program I've been using all week to add some new functionality, and when I looked at the code, I saw this bit:

    sub find_symbol { got to here today! (20180520) }

    What the heck? The code is working, but that subroutine just can't be right. So I looked over the program--It doesn't look like the code is commented out anywhere (I normally use "=h1 foo" and "=cut" to comment out blocks of code I don't want to use). I ran:

    $ perl -c syntax OK

    OK, then, *something* has to be masking this syntax error, right? So I copied the code to and started removing chunk after chunk, eventually coming up with:

    $ cat #!env perl use strict; use warnings; find_symbol(); sub find_symbol { got to here today } $ perl -c syntax OK $ perl -MO=Concise 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 5 v:*,&,{,x*,x&,x$,$ ->3 5 <1> entersub[t2] vKS/TARG,STRICT ->6 - <1> ex-list K ->5 3 <0> pushmark s ->4 - <1> ex-rv2cv sK/STRICT,1 ->- 4 <#> gv[*find_symbol] s/EARLYCV ->5 - <;> ex-nextstate(main 7 v:*,&,{,x*,x&,x$,$ ->6 syntax OK

    So consider me baffled, I don't know what's going on here. Can anyone tell me what I'm not seeing?

    For the record:

    $ perl -version This is perl 5, version 26, subversion 2 (v5.26.2) built for x86_64-cy +gwin-threads-multi (with 7 registered patches, see perl -V for more detail)

    Interesting ... one more bit of information before I hit the preview button. I changed the contents of the subroutine to:

    sub find_symbol { I got to here today }

    and now I get what I expected to see:

    $ perl -c Bareword "today" not allowed while "strict subs" in use at li +ne 8. had compilation errors.

    Does anyone know what's happening here? My only (ill-informed) guess would be something to do with parsing the GOTO statement.


    When your only tool is a hammer, all problems look like your thumb.

diff vs diff -y
5 direct replies — Read more / Contribute
by MissPerl
on Oct 19, 2018 at 11:16
    I am not sure if anyone have the similar issue,
    $a = "/path/to/a/file"; $b = "/path/to/b/file"; $difference = system("$diff $a $b"); $difference2 = system("$diff -y $a $b");
    The $difference printed in html file only shows the difference - top & bottom (maybe 2 or 3 lines from 1000 lines).

    But the $difference2 printed in html file shows all the difference in 2 columns -left & right (1000 lines from 1000 lines).

    The output is actually fine, since I dont want to compare line by line, as sometime one has some extra lines while the other dont,

    So i want those extra lines which is in a and in b at the same file same time.

    Here, I am wondering if it is possible to have the sort of combination of both $diff and $diff -y, having 2 left/right columns of difference in some lines only (not showing every lines)
    if ($difference eq ''){ $difference = "Exactly Same.\n"; } push @table1, "<table id =\"Table1\">"; push @table1, "<tr><th>path A</th><th>A</th><th>path B</th><th>B</th>< +/tr>"; push @table1, "<tr><td colspan =\"4\">$difference</td></tr>";
    So that, $difference will shown in 2 columns with the line of differences in file.

    Any idea guys ?

Why not use a local variable for $self in Moose?
1 direct reply — Read more / Contribute
by nysus
on Oct 19, 2018 at 01:20

    This is probably a supremely dumb question, but that's never stopped me before. I feed off of the ridicule. :)

    So what are the potential problems with doing the following in Moose?

    use Moose; has 'why_pass_self' => { is => 'Str', default => 'dunno, is this ok?' +}; my $self; sub BUILD { my $s = shift; $self = $s; } sub _kool_func { print $self->why_pass_self; } sub kool_func_for_users { my $self = shift; $self->_kool_func; }

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

Question about tr
3 direct replies — Read more / Contribute
by harangzsolt33
on Oct 19, 2018 at 01:17

    I am trying to learn how tr really works, and I think I still don't get it. I remember, reading the said that the character following tr is a separator character, and everything between the separators are characters that will get replaced with a new set of characters defined in the second group. Anyway, I am trying to write a little program that replaces the first 52 bytes of a character map with letters. In other words, in this programming exercise, I am trying to replace binary codes with tr, but tr is not handling these codes very well. For example, it gives an error when I try to convert chr(8) to "I"

    Here is my sample code:

    #!/usr/bin/perl use strict; use warnings; for (my $i = 0; $i < 52; $i++) { $a = chr($i); $a =~ tr|\0\1\2\3\4\5\6\7\8\t\n\0x0B\r\0x0D\0x0E\0x0F\0x10\0x11\0x12 +\0x13\0x14\0x15\0x16\0x17\0x18\0x19\0x1A\0x1B\0x1C\0x1D\0x1E\0x1F !"# +$%&'()*+,-./0123456789|ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrst +uvwxyz|; print "chr $i -> $a \n"; } exit;
image resize error
1 direct reply — Read more / Contribute
by bigup401
on Oct 18, 2018 at 19:46

    i need some correction in my code. am getting Image::Resize->new(): usage error

    my $Newfile='0911'; my $copy_dir_qfn = 'avator/'; my $upload_fh = $CGI->upload("IMAGE"); my $image = Image::Resize->new($upload_fh); my $gd = $image->resize(100, 100); my ($file_extension) = $gd =~ /([^.]+)$/; my $rename = $copy_dir_qfn."$Newfile.$file_extension"; open OUTFILE,'>',$rename or die "$!"; binmode OUTFILE; while (<$upload_fh>){ print OUTFILE; } close OUTFILE;
regex not matching how I want it to :(
3 direct replies — Read more / Contribute
by glwa
on Oct 18, 2018 at 15:27
    usually I managed with regexes myself, but this time I am lost, so I decided to ask wiser people the question is why the following code:
    while ( $line=~/<a href=\"(.*?)\.htm\">/ig ) {
    on $line being
    <a href="test1.htm"> test1</a><br> <a href="test2.htm"> test2</a><br> +<a href="test3.htm"> test3</a><br>
    is matching
    test1"> test1</a><br> <a href="test2
    and not just
Runs from Command Line but NOT from Perl
3 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 18, 2018 at 11:24
    I have this code that prints out a command and also runs it from Perl. When I run the curl command from Perl the remote server, queried from the curl request, complains about an HTTP 400 error.

    When I run the same command, copied from the Perl print statement, it runs fine and returns a JSON string.

    This behavior happens on Linux and Windows with Strawberry Perl. Any ideas?

    Here is the code:
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $command = <<'END'; curl ' +' -H 'Origin:' -H 'Accept-Encoding: gzip, d +eflate, br' -H 'Accept-Language: en-US,en;q=0.9' -H 'User-Agent: Mozi +lla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like + Gecko) Chrome/69.0.3497.100 Safari/537.36' -H 'content-type: applica +tion/json' -H 'Accept: */*' -H 'Referer: +18-tax-reform-calculator/' -H 'Connection: keep-alive' --data-binary +$'{"filing_status": 1, "child_dep": 2, "nonchild_dep": 0, "ordinary_i +ncome1": 150000, "ordinary_income2": 0, "business_income": 0, "ss_inc +ome": 0, "qualified_income": 0, "401k_contributions": 0, "medical_exp +enses": 0, "sl_income_tax": 0, "sl_property_tax": 0, "interest_paid": + 0, "charity_contributions": 0, "other_itemized": 0}' --compressed END print $command; my $data = `$command` or die "$!\n"; print Dumper $data;

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 drinking their drinks and smoking their pipes about the Monastery: (4)
    As of 2018-10-20 11:02 GMT
    Find Nodes?
      Voting Booth?
      When I need money for a bigger acquisition, I usually ...

      Results (117 votes). Check out past polls.

      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!