Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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
Moo From Hell #1: How to proxy a sub from a non OO module
No replies — Read more | Post response
by karlgoethebier
on Aug 02, 2015 at 11:07

    Hi all,

    here is what did:

    The role:

    package Utils; use URI; use List::MoreUtils qw(uniq); use Types::Standard qw(InstanceOf); use Moo::Role; requires qw(list monks); sub my_uniq { my $self = shift; my @result = uniq @{ $self->list }; wantarray ? @result : scalar @result; } has 'uri' => ( is => 'lazy', isa => InstanceOf ['URI'], default => sub { my $self = shift; URI->new( $self->monks ) }, handles => [qw(host)], ); 1; __END__

    The class:

    package Karl; use Types::Standard qw(ArrayRef Str); use Moo; has list => ( is => 'lazy', isa => ArrayRef ); has monks => ( is => 'lazy', isa => Str ); with qw(Utils); 1; __END__

    And the script:

    #!/usr/bin/env perl use strict; use warnings; use feature qw(say); use Data::Dump; use Karl; my $monks = qq(; my $list = [qw(cuke cuke)]; my $nose = Karl->new( list => $list, monks => $monks ); dd $nose; my $uniq = $nose->my_uniq; say $uniq; my @uniq = $nose->my_uniq; say for @uniq; say $nose->host; __END__

    This works as expected and i hope that it is good practice. OK, i'm a bit unsure about this :-(

    But i wonder how to handle the call to uniq like the call to host.

    I would like to say $nose->uniq, instead of $nose->my_uniq.

    In other words: how can i proxy (or delegate) a sub from a non OO module?

    I must admit that i'm a bit confused for the moment.

    Best regards and thank you very much for any hint, Karl

    «The Crux of the Biscuit is the Apostrophe»

Table in Perl CGI
5 direct replies — Read more / Contribute
by alokranjan
on Aug 02, 2015 at 04:53
    Hi All,

    Can anyone please let me know how can I print the values of hash in tabular form in Pel CGI?

    Below is my code, which is not showing the table:

    print $cgi->header(), $cgi->start_html("Fruits and colors"), $cgi->table({-border=>1}, $cgi->Tr($cgi->th([" Fruit"," Color"])), for my $f (keys %tbl){ map { $cgi->Tr($cgi->td([$f,$tbl{$f}])) } sort keys %tbl } ), $cgi->end_html();
    Any help/assistance would be highly appreciated.



plotting roc curve using roc package
4 direct replies — Read more / Contribute
by numita
on Aug 02, 2015 at 02:56

    hello everyone,I tried to write a code for roc curve using perl module statistic:roc, but when i run the program it gives the error value out of range for table lookup (2):0.711752437,1 at line 16.

    use Statistics::ROC; open(fh,"<roc_tp_fp.txt"); while ( <fh> ) { ($a,$b)=split/,/; push @AoA, [ split ]; } for $aref ( @AoA ) { #print "[ @$aref ],"; } @curves=roc('decrease',0.95,@AoA); print "$curves[0][2][0] $curves[0][2][1] \n";

    input_file:roc_tp_fp.txt looks like as follows: 0.9883817,1 0.770431568,1 0.983195895,1 0.812109932,1 0.901505931,1 0.72431528,1 0.73553418,1 0.724572657,1

regex help
3 direct replies — Read more / Contribute
by Anonymous Monk
on Aug 02, 2015 at 01:03
    I have a text below and along with this there is some more text which is p tags, br tags. Please help me to remove entire sentence if contains 23456.
    <p>Find more business news at and Text BUSIN +ESS to 23456 for breaking business news text alerts on your mobile ph +one. Text JOBS to 23456 for job alerts.</p> <p>Want your news even faster? Text NEWS to 23456 to sign up for break +ing news text alerts. See for a complete list of alerts.</p> <br>SIGN UP FOR MOBILE NEWS ALERTS! Get your news on the go, text NEWS + to 23456
Help fixing this piece of code
4 direct replies — Read more / Contribute
by perlynewby
on Aug 01, 2015 at 17:28

    Hello Monks, I am interested in finding out how to get this specific subroutine to work.

    I have worked it out using other method but very interested in finding the method to this specific hash format below since I couldn't find the solution myself

    Since I couldn't fix it myself and still interested to see to see how you guys do it

    sub routine must find the MODE in a given set of numbers. must find the most duplicated number and pass the value.

    data 2 3 3 3 5 7 8 12 32 44 55 12 3 23 43 33 1 4 25 43 42 1 4 5 3 3 3
    sub mode_num{ my %count=(); %opt = %{shift @_ } ; #creating hash #dump \%opt; #check data coming in.. #need to get integrate count as I format data into hash(next task + to make it reable) for (sort{$a <=> $b} %opt{NUMBERS}){ #dump \$opt{NUMBERS}++; $opt{NUMBERS}++; } our @keys= sort{$opt{NUMBERS}->{$b}<=>$opt{NUMBERS}->{$a}} key +s %opt{NUMBERS}; return }
Why am I getting message «Operation "<=>": no method found,» ?
4 direct replies — Read more / Contribute
by RCH
on Aug 01, 2015 at 10:44

    Dear Perl Monks

    I'm getting the message

    Operation "<=>": no method found, left argument in overloaded package Class::Measure::Length, right argument in overloaded package Class::Measure::Length

    from what I thought was just a simple sort


    I found a work-around, by sprintf-ing the values in the hash - see code below - but I would like to (try to) understand what is going on.

    I'm on Ubuntu 12.04

    perl -v says

    This is perl 5, version 14, subversion 2 (v5.14.2) built for i686-linux-gnu-thread-multi-64int
    The following script gives the no method found message the second time round
    use strict; use Carp; use GIS::Distance; my $from_lat = 50.815717; my $from_long = 4.436326; my $to = { Montana => [ 46.536026, -111.935062 ], Correze => [ 45.341874, 1.79531 ], ParisFR => [ 48.850407, 2.347304 ], }; my %distance; foreach my $demo ( 'solution', 'problem' ) { print STDOUT "\n", "-" x 60, "\n\n"; print STDOUT "This is to demonstrate the $demo ...\n"; print STDOUT " Here comes the contents of the hash \%distance\n"; print STDOUT " (distances as calculated by GIS::Distance\n"; if ( $demo eq 'problem' ) { print STDOUT " and stored as that value)\n"; } elsif ( $demo eq 'solution' ) { print STDOUT " and stored as sprintf \"%25.15f\", value )\n"; } foreach my $place ( keys(%$to) ) { my $gis = GIS::Distance->new(); my $to_lat = $to->{$place}->[0]; my $to_long = $to->{$place}->[1]; my $km = $gis->distance( $from_lat, $from_long => $to_lat, $to_long, ); if ( $demo eq 'problem' ) { $distance{$place} = $km; } elsif ( $demo eq 'solution' ) { $distance{$place} = sprintf "%25.15f", $km; } print STDOUT " \$distance{$place} = $distance{$place}\n"; } print STDOUT "\n"; print STDOUT "And here comes the result of sorting the keys of that hash\n"; foreach my $place(sort {$distance{$b} <=> $distance{$a}}( keys(%distance))) { printf STDOUT "%10s is %25.15f km from here\n", $place, $distance{$place}; } }
Trying dirty trick with Imager and Inline C, but having difficulties. Can you help?
1 direct reply — Read more / Contribute
by vr
on Aug 01, 2015 at 04:00

    At some point in my program, it seems that easiest (fastest, etc.) thing to do is to directly replace raw image data with data from Perl scalar. Needless to say, Imager API (both Perl and C) don't provide for this, but I thought I'd get away with following subroutine (here it is with a test program that, of course, makes no sense):

    use strict; use warnings; use Imager; use Inline with => 'Imager'; use Inline C => << 'END_OF_C'; void _replace( Imager img, SV* str ) { unsigned char* ptr = SvPV_nolen( str ); myfree( img-> idata ); img-> idata = ptr; } END_OF_C my $i = Imager-> new( xsize => 99, ysize => 99 ); for ( 'A' .. 'F' ) { print "$_\n"; my $s; $i-> write( data => \$s, type => 'raw' ); my $j = $i-> copy(); # Here, it's rumored, some nasty things # happening, that involve $j and $s. # But maybe nothing happens. _replace( $i, $s ); } __END__ ~$ perl A B *** Error in `perl': double free or corruption (!prev): 0x0000000002d6 +7990 *** Aborted (core dumped)

    Unfortunately, my C is below-zero level. I understand I should prevent Perl from freeing memory that now is Imager responsibility, but don't know how. And, actually, replacement itself works OK. There's only error freeing memory, that happens just sometimes. This program seems to reproduce it always.

Difference between leftward and rightward list operators
2 direct replies — Read more / Contribute
by Firsov
on Aug 01, 2015 at 02:25


    I was reading perlop manual. And i don't understand one thing: What difference between rightward and leftward list operators?

    I guess that leftward list operator this is function that accept LIST (has higher precedence that comma operator). But what is a right ward list operator and why it has low precedence?

when i run a log file,will get some output in numbers say'12345' ,i would like to copy it in excel file?
2 direct replies — Read more / Contribute
by Skumar2015
on Jul 31, 2015 at 17:53
    #!/usr/local/bin/perl use strict; use warnings; my $filename = 'C:Desktop\data.log'; open(my $file1, '<:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!"; while (my $row = <$file1>) { chomp $row; print "$row\n"; } when i run this file i will get some '12345'as an output. Now I need to copy the log file output into existing excel sheet day by day. please help me.
A regex question.
2 direct replies — Read more / Contribute
by FeistyLemur
on Jul 31, 2015 at 17:49

    This is my first post here, I have a question about something I can't explain.

    I'm pretty sure I know what I did wrong, I'm just not 100% sure why it was wrong and wanted to clarify to better my understanding of regex in perl, as I'm fairly inexperienced and looking to improve.

    I was searching for vlan ids for removal in the output of "ip addr" in the following way.

    my $ethernet=`ip addr`; if ($ethernet!~/eth1\.\d{4}\@/gm){ print "No vlans exist on device.\n"; exit; } while ($ethernet=~/(eth1\.\d{4})\@/gm) { print `ip link del $1\n"; print "Removed $1\n"; }

    Doing this as above mostly worked, but it would always miss the first entry for no reason I can explain. So if there were 25 vlan entries in the string from 1001 to 1025 it would match and delete 1002-1025 without fail, and miss 1001 every time.

    Changing line 2 to:

    if ($ethernet!~/eth1\.\d{4}\@/){

    Does what I intended to do.

    I had the same problem with a similar line I was using to scrub IP addresses off the vlan devices again missing the first match, I just don't understand why the while loop breaks in the way it does because of the /gm flag on the preceeding if statements match, and was hoping someone could explain. Thanks in advance.

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!
  • 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 pondering the Monastery: (6)
    As of 2015-08-02 16:32 GMT
    Find Nodes?
      Voting Booth?

      The oldest computer book still on my shelves (or on my digital media) is ...

      Results (4 votes), past polls