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

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
How to use this subroutine
4 direct replies — Read more / Contribute
by bisimen
on Oct 16, 2017 at 10:35

    I can't figure out how to apply this subroutine (pretty_print) to my data:

    #!/usr/bin/perl use warnings; #use strict; ($sfile_1, $sfile_2) = @ARGV; ($id1, $seq1) = read_fasta($sfile_1); ($id2, $seq2) = read_fasta($sfile_2); pretty_print(\$id1, \$seq1); pretty_print(\$id2, \$seq2); sub read_fasta { ## reads a single sequence from a fasta file my $seqFile = shift @_; my $seq = ""; my $id = ""; open(my $in, "<", $seqFile) or die "unable to open $seqFile $!\n"; while(<$in>){ chomp; if($_ =~ /^>(\S+)/ ){ last if(length($id)); $id = $1; next; } if(length($id)){ $seq .= $_; } } return ($id, $seq); } sub pretty_print { my($seq, $colno) = shift @_; # We want to start from 0, and increment the starting position. # for this we use a classical loop. for (my $b=0; $b < length($seq); $b += $colno){ print( substr($seq, $b, $colno), "\n"); } } exit;

    The two data files if needed: https://justpaste.it/1ch48 and https://justpaste.it/1ch4b

"no warnings 'uninitialized'" failing
5 direct replies — Read more / Contribute
by jest
on Oct 16, 2017 at 10:05

    An older part of our codebase has a function for converting a data structure to XML, using XML::Simple (for some legacy reason), and saving it to a directory.

    At some point, we upgraded XML::Simple to 2.24, and it started throwing "Use of uninitialized value" errors. Whatever caused this was irrelevant to our purposes, so I put this in a block with "no warnings 'uninitialized';", and all was good.

    For no reason that I can tell--I didn't upgrade any package, or my Perl version (5.16.3 for this)--our test suite is now throwing the error again, though the "no warnings" is right there:

    use XML::Simple; my $xml; { # Suppress warnings from XML::Simple 2.24 no warnings 'uninitialized'; my $xmlout = XMLout($data, NoAttr => 1, RootName => undef, Suppres +sEmpty => 0); $xml = '<xml version="1.0" encoding="UTF-8">' . "\n$xmlout</xml>"; }
    Result from test suite:
    Use of uninitialized value at /[path/to]/Data.pm line 205. Use of uninitialized value at /[path/to]/Data.pm line 205. Use of uninitialized value at /[path/to]/Data.pm line 205. [...]
    Where line 205 is the "my $xmlout..." from above.

    Can someone explain why I am getting a warning that I explicitly shut off on the immediately preceding line? And how I stop this? The tests pass, but it's distracting as hell to get 50 lines of this error every time we run it.

XML::Smart - undesired decoding of special XML characters
3 direct replies — Read more / Contribute
by NeedForPerl
on Oct 16, 2017 at 04:34

    Hi, Monks I'm using XML::Smart and running into some problems if there a special characters in the XML document:

    If i create an XML::Smart object that encapsulates an XML document which contains special characters like "" or "" the function data() uses Base64 encoding but i don't want that. So i decided to use the function with the argument "decode => 1".

    After that change everthing works fine unless there are special XML characters like "<", ">" or "&" inside an XML element . I guess that the call of data(decode => 1) results in encoding "&amp;" to "&" for instance. Is it possible to avoid that behaviour?

    I used the function set_binary('FALSE') but somehow it didn't work:

    my $log = `svn log http://... --xml --revision 123`; my $test = XML::Smart->new($log); $test->{log}->{logentry}[0]->{msg}->set_binary('FALSE'); print $test->data();

    I'm using version 1.78 of the module.

    I have tried to contact the author via mail using the e-mail address which can be found on the FAQ of XML::Smart. The E-Mail address doesn't exist anymore.

    Many thanks in advance.

Invalid Reference Use - warning Noob
3 direct replies — Read more / Contribute
by jorba
on Oct 15, 2017 at 15:10
    I am trying to construct a moose class that represents a single record retrieved from the db. I'm getting a problem which I'm pretty sure is from the attempt to assign values to a hash "property" of the class. But I could be wrong ;o)

    package AXRecord; # Our libraries use lib 'C:\Users\Jay\Desktop\SBS DEV\CODE\perl\Utilities'; use AXControl; use AXSQL; use Moose; use DBI; # Attributes has 'Name' => (is => 'rw', isa => 'Str', required => 1); has 'Fields' => (is => 'ro', isa => 'Hash'); has 'FieldCount' => (is => 'ro', isa => 'Num'); has 'Changed' => (is=>'rw', isa => 'Boolean'); has 'Where' => (is => 'rw', isa => 'Str'); has 'ControlObject' => (is => 'rw', isa => 'Object', required => 1); has 'Keys' => (is => 'rw', isa=>'Array'); # Contains a single record sub BUILD # Constructor { my $self; $self = shift; if (defined $self->Where) { $self->Select(); } } # Insert the record sub Insert { } #Delete using the keys in the record sub Delete { } #Update using the keys and values in the record sub Update { } #Save the record sub Save { } sub Select { my ($SQLStr, $Cnt, $sql, @Values, $self, $Col, @Flds, $i); $self = shift; $SQLStr = "SELECT * FROM " . $self->Name . ' ' . $self->Where; $sql = AXSQL->new(ControlObject => $self->ControlObject, SQLString + => $SQLStr); #construct a hash using the metadata and the data from the actual +table $self->Fields = {}; @Values = $sql->Fetch(); #Get field Names $sql = AXSQL->new(ControlObject => $self->ControlObject, SQLString + => "show fields from '" . $self->Name . "'"); @Flds = $sql->fetch(); # Construct the hash for ($i..$#Flds) { $self->Fields{$Flds[$i]} = $Values[$i]; } $self->Keys = (); $sql = AXSQL->new(ControlObject => $self->ControlObject, SQLString + => "SELECT column_name FROM information_schema.`key_column_usage` WH +ERE table_name = '" . $self->name . "' order by ordinal_position"); while (($Col) = $sql.fetch()) { push $self->Keys, $Col; } $Cnt = $self->Fields; $self->FieldCount = $Cnt; $self->Changed = -1; }

    The error messages I am getting all seem to be caused by what I'm doing wrong in this statement

    $self->Fields{$Flds[$i]} = $Values[$i];

    All of the declaration errors are about variables that work fine before that line. The hash Fields is a property of the class and I'm trying to populate it with fieldname/value pairs Here are the error messages (moose warnings excluded)

    C:\Users\Jay\Desktop\SBS DEV\CODE\perl\Utilities>perl -w AXRecord.pm push on reference is experimental at AXRecord.pm line 83. syntax error at AXRecord.pm line 76, near "->Fields{" Global symbol "$self" requires explicit package name (did you forget t +o declare "my $self"?) at AXRecord.pm line 79. Global symbol "$sql" requires explicit package name (did you forget to + declare " my $sql"?) at AXRecord.pm line 80. Global symbol "$self" requires explicit package name (did you forget t +o declare "my $self"?) at AXRecord.pm line 80. Global symbol "$self" requires explicit package name (did you forget t +o declare "my $self"?) at AXRecord.pm line 80. Global symbol "$Col" requires explicit package name (did you forget to + declare " my $Col"?) at AXRecord.pm line 81. Global symbol "$sql" requires explicit package name (did you forget to + declare " my $sql"?) at AXRecord.pm line 81. Global symbol "$self" requires explicit package name (did you forget t +o declare "my $self"?) at AXRecord.pm line 83. Global symbol "$Col" requires explicit package name (did you forget to + declare " my $Col"?) at AXRecord.pm line 83. Global symbol "$Cnt" requires explicit package name (did you forget to + declare " my $Cnt"?) at AXRecord.pm line 86. Global symbol "$self" requires explicit package name (did you forget t +o declare "my $self"?) at AXRecord.pm line 86. Global symbol "$self" requires explicit package name (did you forget t +o declare "my $self"?) at AXRecord.pm line 87. Global symbol "$Cnt" requires explicit package name (did you forget to + declare " my $Cnt"?) at AXRecord.pm line 87. Global symbol "$self" requires explicit package name (did you forget t +o declare "my $self"?) at AXRecord.pm line 88. syntax error at AXRecord.pm line 90, near "}" AXRecord.pm has too many errors. C:\Users\Jay\Desktop\SBS DEV\CODE\perl\Utilities>
Adding values in a variable
3 direct replies — Read more / Contribute
by bisimen
on Oct 15, 2017 at 14:23
    Hi, Monks. I'm very new to Perl and programing. But I'm stuck right now. First, the code:
    #!/usr/bin/perl use warnings; no warnings 'uninitialized'; #Calculating Protein Mass ($input) = $ARGV[0]; open ($in, "<", $input) || die "Can't open \"$input\".\nError = $!.\n" +; $protein = <$in>; my(%mass_values) = ( 'A' => 71.03711, 'C' => 103.00919, 'D' => 115.02694, 'E' => 129.04259, 'F' => 147.06841, 'G' => 57.02146, 'H' => 137.05891, 'I' => 113.08406, 'K' => 128.09496, 'L' => 113.08406, 'M' => 131.04049, 'N' => 114.04293, 'P' => 97.05276, 'Q' => 128.05858, 'R' => 156.10111, 'S' => 87.03203, 'T' => 101.04768, 'V' => 99.06841, 'W' => 186.07931, 'Y' => 163.06333, ); my $mass = ""; for(my $i=0;$i<length($protein);$i+=1) { $codon = substr($protein,$i,1); $mass .= "$mass_values{$codon}\n"; } print $mass; ($result) = $ARGV[1]; open ($out, ">", $result) || die "Can't write to \"$result\".\nError = + $!.\n"; print $out "$mass"; close "$out"; exit;

    This program is for this problem: http://rosalind.info/problems/prtm/

    In order to run it, you need a protein string file, but you need to be registered to get one from rosalind, but here is the data string I'm using: https://justpaste.it/1cfuo

    Now, this program I've written is just a modification of some codes I found here and there (And I kinda understand how it works). There is probably better ways of doing it, so just say if you see a quicker and easier solution!

    Anyway, I'm stuck at the part were I have to sum up all the numbers. Right now, if I run it, I will just get a long list of numbers like this:

    103.00919

    131.04049

    113.08406

    57.02146

    186.07931

    128.09496

    156.10111

    97.05276

    etc.

    But I've no idea how to sum it all up. I've tried to make it into an array, but then I get errors saying "this is not a number", because I think perl thinks all of the different numbers is one element, and not 1000 different elements...

    Anyway, I did complete the problem by "writing"(stole and modified) a separate program to sums it all up:

    #!/usr/bin/perl use strict; use warnings; # given a file with a number on each row, print the sum of the numbers my $sum = 0; my $filename = "result.txt"; open(my $fh, "<", $filename) or die "Could not open '$filename'\n"; while (my $line = <$fh>) { $sum += $line; } print "The total value is $sum\n";

    (I did try to put the above code into the first program, so that the program writes to the results file, then opens the results file, and then does the addition. But for some reason, I get the wrong answer doing it this way, like it skips adding some numbers for some reason...

    But I feel that this is something that can be done easily with just one perl program. But it was just my way of bodging it.

    Anyway, hope someone can correct me and explain my mistakes!

comparing ranges of numbers between 2 files
2 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 15, 2017 at 06:31
    Hello everyone!
    I have 2 text files with some number ranges, e.g.:
    100000313-100001222
    in one file and 100000391-100000947 in the other.
    My goal is to take each line from each file and see if it is somehow "contained" in the other. As you can see from the example, the range 100000391-100000947 is within the range 100000313-100001222.
    Can you provide me with some help as to how I can deal with such a problem?
    Is there some module or something?
    Thank you very much for your time!
Matching regular expression over multiple lines
3 direct replies — Read more / Contribute
by Maire
on Oct 15, 2017 at 05:38

    Hi Monks,

    I'm new to Perl, and I have run into a problem with a regex.

    Essentially, I am working with HTML files that take the format of the extract exemplified below:

    <blockquote> <p><b>Joos van Cleve</b> - Lucretia (detail)</p> </blockquote> <p>beautiful</p> </blockquote> <p>indeed I am</p> <footer><a href

    I want to capture and print the contents of the line (minus the HTML tags) proceeding the <footer> line (so, in this case, I want to print the words "indeed I am"). I am using the following script to try and do this:

    open(FILE, "C:/Users/li/data_collection/posts/165644996453.html"); while (<FILE>) { if ( /(?<=<p>)(.*)(?=<\/p>\s+<footer>)/s ) { print "$1\n"; } }

    However, when I run the script, nothing is printed. I am almost certain that the error arises from the way in which I've tried to get the regex to work over multiple lines. I've tried several fixes that I've found on various websites, some of which are reproduced below, but nothing solves the problem

    ( /(?<=<p>)(.*)(?=<\/p><footer\>)/s ) ( /(?<=<p>)(.*)(?=<\/p>(<footer>))/m ) ( /(?<=<p>)(.*)(?=<\/p>\s+<footer>)/gm ) ( /(?<=<p>)(.*)(?=<\/p>\n<footer)/g )

    Any advice would be greatly appreciated.

    Cheers!

Filehandle with DKIM::Verifier
2 direct replies — Read more / Contribute
by nifu
on Oct 14, 2017 at 19:32
    Hello, thanks for reading my question and sorry for my bad english. I use a perl script to analyze differed headers from emails (saved as txt file). The script search every $x hours via cronjob in the directory for new emails. Like perl script.pl $email.filename.txt The perl script must now check for a valid dkim signature. I did some tests with Mail:: DKIM:: Verifier. But it only works when i read the file from stdin like perl dkim.pl < email.txt. I dont know how to use the perl module with $dkim->load(*STDIN);. I have tried to load the file with a filehandle like $dkim->load(<fh>); or $dkim->load($filename);. But it doesn't work. Has someone an idea how i can solve this?
Almost certainly a module documentation issue
3 direct replies — Read more / Contribute
by dd-b
on Oct 13, 2017 at 17:37

    The Email::Sender::Transport::Print module has very sparse documentation. It does say you can pass a file-handle object in as the attribute "fh". Looking at other Email::Sender::Transport implementations, it appears to me that passing a hash to new with the attributes as name/value pairs is the way it's done (there's a rather startlingly complete shortage of examples of Email::Sender::Transport::Print usage on the web).

    The code is:

    open (my $sm, '>> :encoding(UTF-8)', 'out.txt') or die "Failed to open out.txt: $!"; $transport = Email::Sender::Transport::Print->new({ fh => $sm, });

    ($transport is declared outside the bit quoted, but it is declared.)

    When I do that, I get the error "isa check for "fh" failed: GLOB(0x806897840) is not blessed". This seems to imply that it noticed that I was in fact passing something as attribute fh, at least!

    In the debugger, it seems to me that $sm, the thing passed in as the value of attribute fh, is in fact blessed.

    DB<3> x $sm 0 GLOB(0x806897840) -> *main::$sm FileHandle({*main::$sm}) => fileno(7) DB<4> n isa check for "fh" failed: GLOB(0x806897840) is not blessed at (eval 7 +8)[/home/ddb/perl5/lib/perl5/Sub/Quote.pm:3] line 59, <$if> line 2. eval {...} called at (eval 78)[/home/ddb/perl5/lib/perl5/Sub/Quote +.pm:3] line 58 Email::Sender::Transport::Print::new("Email::Sender::Transport::Pr +int", HASH(0x80213c8e8)) called at /home/ddb/smbshare/Documents/Fando +m/Minicon/Mc53/mc53_pr1_001.pl line 178M

    I am now wondering WTF. A preliminary attempt to look at the source code of the module foundered; it appears there is none, it's generated on-the-fly using moo or something? Which I'm not familiar with, and while in theory I could study it up and then figure out for sure what's expected, that's starting to look like a large amount of work.

    Ideas?

Questions regarding mixing up all the data from Image::ExifTool
3 direct replies — Read more / Contribute
by Buttonzz
on Oct 13, 2017 at 16:18

    Hello everybody and thanks for reading my problem.

    Im new in here and need some advice to overwhelm some trouble programming my exif-editor Tool with Perl and Perl-Tk.

    Im dealing with two problems.

    I)

    I have a scrollable GridColumn with 2 columns. Before Im creating it, Im creating a menu where you can pick a file or directory. So I cant call $gc->refresh() in the menu's subs.

    If I start the application and pick a file, the gridcolumns data sofore wont update.

    Is there any way creating an event that "watches" if $file or $folder swap from undef to something else? - I 'overread' some sites dealing with events but cant find anything that really fits. (Or I didnt get deep enough.

    II)

    Dealing with Image:ExifTool - is there any 'easy' way to get all the info of more than one file (all files with exif info in a specific directory) together?

    If I manage those 2 problems I might eb able to stick to my appointment next friday. Its an exercise I chose myself for the last course before my bachelor thesis.

    Unfortunate my kid had lots of trouble with teeth - so we with sleep - and after that we all got ill, so it gets close to the deadline now and Im missing some things. Im sure my lecturer would give me another week. But I wont use this if I dont have to.

    In case you need more information, feel free to ask. I wanted to keep it quite theoretically because I dont want anyone to do my work - I just need some 'slaps on the back of my head' as we German say. :) Have a nice evening and thx for anyone dealing with my post. :)


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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the universe expands...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (4)
    As of 2017-10-21 04:07 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My fridge is mostly full of:

















      Results (269 votes). Check out past polls.

      Notices?