Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

The Monastery Gates

( #131=superdoc: print w/ replies, xml ) Need Help??

Donations gladly accepted

If you're new here please read PerlMonks FAQ
and Create a new user.

New Questions
open and read file using cgi perl
2 direct replies — Read more / Contribute
by vivek.vivek
on Sep 29, 2014 at 11:29

    Hi, I have 2 files as below

    a.txt: LOCK xxx yyy 123 1.1 fff LOCK nnj hhh 789 2.2 uuu
    b.txt: LOCK xxx yyy 567 1.1 fff LOCK nnj hhh 000 2.2 uuu LOCK YUI hhh 520 2.3 ujk

    I am comparing both files by browsing it through a webpage and parsing the file. I need to compare the lines if only it starts with word LOCK and output the data. The code is doing the comparison, but my problem is that the output is repeated with the count of number of lines in each file.

    Eg: if a is compared with b, output repeted twice. if b is compared with a, output is repeated 3 times.

    my @files = ($file,$file1); open(INOUT1,"<", $file1) or die "cant open output file "; + while ($linea = <INOUT1>){ open(INOUT,"<", $file) or die "cant open output file"; while ($line = <INOUT>){ chomp $linea; next unless $linea =~ m/^LOCK /; chomp $line; next unless $line =~ m/^LOCK /; foreach $file_n (@files){ $old = $files[0]; $new = $files[1]; if ($old eq $file_n){ @words=split(/\s+/,$line); $feature1[$i]=$words[1]; $ver1[$i]=$words[3]; $exp1[$i]=$words[4]; $no1[$i]=$words[5]; $i++; } else{ @words=split(/\s+/,$linea); $feature2[$ii]=$words[1]; $ver2[$ii]=$words[3]; $exp2[$ii]=$words[4]; $no2[$ii]=$words[5]; $ii++; } } }close INOUT; }close INOUT1; print "<table border =1 align='left' style='font-family:Georgi +a;'> for ($b=0;$b<@feature1;$b++) { $test=0; for($a=0;$a<@feature2;$a++) { if($feature1[$b] ne $feature2[$a]) { $test++; } if($test == @feature2) { print "<tr><td>$feature1[$b]</td><td>$ +ver1[$b]</td><td>$exp1[$b]</td><td>$no1[$b]</td></tr>"; #$test++; } } } print "</table>"; print "<table border=1 align='left' style='font-family:Georgia +;'> for ($b=0;$b<@feature2;$b++) { $test=0; for($a=0;$a<@feature1;$a++) { if($feature2[$b] ne $feature1[$a]) { $test++; } if($test == @feature1) { #print $test; print "<tr><td>$feature2[$b]</td><td>$ver2 +[$b]</td><td>$exp2[$b]</td><td>$no2[$b]</td></tr>"; } } } print "</table>";}
Choosing an AWS S3 CPAN module
1 direct reply — Read more / Contribute
by sm@sh
on Sep 29, 2014 at 04:54
    I have a simple requirement to read a file from an S3 bucket. We'll be using access key + secret IAM authentication. I can find these modules on CPAN: * Net::Amazon::S3 * Amazon::S3 * AWS::S3 The first of these seems to be most common, and well-maintained. The w +eight of dependencies isn't a major problem given we already have man +y of them in our app. However, they are all quite bloated for my requirements, and from the +AWS docs, it is simply a case of generating an auth token to access t +he file. Can anyone recommend any other modules, or has written their own acces +s code?
How to make a timer with breaks
2 direct replies — Read more / Contribute
by xdfreaknikdx
on Sep 28, 2014 at 19:06

    (Hi I need to add countdown timer to my perl program which starts from 00:01:00(1 minute)when I start running the script and in every 00:00:10 (10 seconds) the timer stops and the program will asks if you want to continue or stop the countdown. Thanks in advance.)

Variable not set properly in perl
3 direct replies — Read more / Contribute
by sar123
on Sep 28, 2014 at 11:50
    I have the following code:
    #!/usr/bin/perl -w use warnings; use diagnostics; open $fh , "<", "$ARGV[0]" or die "Could not open file: $!"; sub getsub{ my $sub = $_[0]; print "sub entered for $sub\n"; while (<$fh>) { if (/\.subckt $sub/../\.ends/) { print ; } } } while (<$fh>) { if ($_ =~ /^xa1/) { $line = $_; print "line found to be $line\n"; while ((my $nxt = readline($fh)) =~ /^\+/) { $line = $nxt; print "line changed to $line\n"; } $line =~ s/\s+$//; print "last line is $line\n"; my $sub = (split '\s', $line)[-1]; print "subcircuit found is $sub in $line\n"; getsub($sub); } }
    Here I am trying to print some text between two patterns inside the `getsub` routine. But when I try to run this I enter the subroutine but doesn't enter the if block inside the subroutine. I am trying to run it on the following file:
    .subckt a1 x y z xa a b c1 xb c d e1 xc f g h1 .ends .subckt c1 x y z xa a b f xb c d e xc f g h .ends .subckt e1 x y z xa a b c1 xb c d k1 xc f g h1 .ends xa1 a s f a1
    I want to print the contents of the file between ".subckt a1" till ".ends".
    I know this could be done by the one liners of perl on the command line, but I want to create some generalized script for different files so I need to go this way only. What is wrong with the above code.
Best practices for handling errors
7 direct replies — Read more / Contribute
by v_melnik
on Sep 27, 2014 at 07:31

    Dear colleagues,

    I think, it's a matter of religion, but I'd like to get to know more on how other people, more experienced, prefer to handle errors/exceptions in respect to the structure of your programs.

    Let me describe how I'm doing it now and, if you have some time to share your experience, I'd be very grateful to you for describing how do you prefer to do it.

    My own "rules" for myself are quite simple.

    1. Don't die() while executing a subrotine or method. Only the main module can die() if something goes wrong. Nobody can predict where the class will be used, so an unexpected die() can break the caller's logic.
    2. If I've got an exception inside of a subroutine, the subroutine may return(undef). If everything's fine, it return's some value (it can be true or false - no matter), but if some error has been occuried (e.g. if we can't get data from the database), the undef shall be returned.

    That's okay, but how to let the caller know what's happened with the subroutine? As I think, the caller must have some explaination to be able to write something to the log-file or to show the error message to the operator. So, there is one more rule.

    1. Any class may have the "errstr" attribute, so if its' methor returned undef, the caller may get the explaination from this attribute.

    So, usually it looks like this:

    package SomeClass; #... sub some_method { # ... eval { die("Oops!"); }; if($@) { $self->{'errstr'} = "Something has gone wrong: $@"; return(undef) } # ... } #... package main; #... my $result = $obj->some_method; unless(defined($result)) { die("Can't SomeClass->some_method(): $obj->{'errstr'}"); } #...

    And, when something goes wrong, I can get something like that:

    Can't SomeClass->some_method(): Can't AnotherClass->another_method(): Can't OtherClass->other_method(): Can't open(): No such file at line 666.

    Frankly speaking, I have a persistent feeling that there are some other, much more elegant way to do it. And I hate how the final error message looks like. Just like "can't A, because can't B, because can't C, because f*** you". Ugh... :(

    And there is another annoying thing: I have to use die() in the constructor of an object (I mean new()), because if the constructor returns undef, the caller doesn't have an access to the object's "errstr" attribute at all (as we don't have the object bless()ed). So I have to always call constructors from eval()-blocks and get the explaination from $@.

    package SomeClass; #... sub new { # ... eval { die("Oops!"); }; if($@) { die("Something has gone wrong: $@"); } # ... } #... package main; #... my $obj = eval { $obj->new }; unless(defined($obj)) { die("Can't SomeClass->new(): $@"); } #...

    I absolutely hate it, but I don't see better ways to let the caller know why the object hasn't been blessed.

    Maybe I should consider using some global variable to keep the reference to the stack of errors occuried? What do you think?

    Maybe I should always die() (or confess() - as a better way to get to know who has called whom) inside of any method and call every method inside of eval()- or try()-block?

    I'll be very grateful to each of you for sharing your best practices on this matter. It really makes me feel unsatisfacted. :)

    Have a nice time!

Taint and Shellshock
5 direct replies — Read more / Contribute
by kennethk
on Sep 26, 2014 at 20:59

    Obviously, the correct way to deal with Shellshock is update bash. However, given that running under Taint mode already requires you to clean up pathing variables before external calls, would it be reasonable to shift best practice to invoking local %ENV = (PATH => '/usr/local/bin'); rather than piecemeal cleanup? Is there any good reason to not wipe the whole %ENV hash before an external call in web context?

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

find unique values from a file by comparing two columns
3 direct replies — Read more / Contribute
by sreeragtk86
on Sep 26, 2014 at 07:08

    I am trying to get the unique values from the combination of columns 2 & 3 (: as delim) .

    eg; below is the sample file
    D1:111:92111 D2:112:92111 D3:111:92111 D4:112:92111 D5:111:90222 D6:112:90222 D7:111:90222 D8:112:90222
    The output should be

    92111 has unique values 111,112

    90222 has unique values 111,112

How to create relocatable perl 5.20.1 ?
3 direct replies — Read more / Contribute
by szabgab
on Sep 25, 2014 at 16:40
    As I was building a relocatable Perl I bumped into some problems with some of the values in %Config not automatically being updated. A bit of searching led me to this patch but I could not find anything related to that in the sources of perl 5.20.1.

    I described the issue here more in details, but in a nutshell $Config{perlpath} (and a few other entries in %Config) keep pointing to where prefix installed perl, even after I move the installation tree.

    Do I need to give some other parameter during the building of perl to change this behavior? Is there some script I need to run after I moved the installation tree?

Die-ing with object with overloaded stringification
1 direct reply — Read more / Contribute
by perlancar
on Sep 25, 2014 at 09:03


    Since die() accepts a reference, I thought I'd use an object for a "more proper" exception system. Below is code example:

    BEGIN { package Err; require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(err); use overload q("") => sub { my $e = shift; "ERROR $e->[0]: $e->[1] +" }; sub new { my $class = shift; bless $_[0], $class } sub err { __PACKAGE__->new([@_]) } } package main; BEGIN { Err->import } use Carp::Always; sub f { g(); } sub g { die err(403, "Permission denied"); } f();

    It prints "ERROR 403: Permission denied" as expected. However, it doesn't print stack trace even under Carp::Always. What should I do to produce stack trace?

Connecting to a database using a socks proxy?
No replies — Read more | Post response
by Skeeve
on Sep 25, 2014 at 05:31

    In my environment I use a socks proxy to connect to my database. This works fine for programs like the Java based DBVisualizer. I simply define the proxy in the connection's properties.

    But how could I achieve this with perl? I have no clue which additional modules could help here. Or is there, maybe, no module required and a socks-connection is already possible?

    Many thanks in advance for any useful hint.

bleach question
5 direct replies — Read more / Contribute
by roadtest
on Sep 24, 2014 at 15:56


    I am maintaining an old system with some monitoring scripts written in perl. The fun part is all these scripts are encoded and I could not read them to figure out why they failed from time to time. Here is script text and hexadecimal header. With several hours google search, I understand these are the bleached scripts and people say it is useless to hide source in such way and mention it is easy to decode it. I tried and it says script is not bleached. I wonder whether it is possible to decode this script. No need to be accurate or runnable, just information of what it is checking, how it is checking. Thanks for any suggestions!
    #!/usr/bin/perl $_=<<'';y;\r\n;;d;$_=pack'b*',$_;$_=eval;$@&&die$@;$_ root# od -cx check_services 0000000 # ! / u s r / b i n / p e r l \ +n 2123 752f 7273 622f 6e69 702f 7265 0a6c 0000020 $ _ = < < ' ' ; y ; \ r \ n ; +; 5f24 3c3d 273c 3b27 3b79 725c 6e5c 3b3b 0000040 d ; $ _ = p a c k ' b * ' , $ +_ 3b64 5f24 703d 6361 276b 2a62 2c27 5f24 0000060 ; $ _ = e v a l ; $ @ & & d i +e 243b 3d5f 7665 6c61 243b 2640 6426 6569 0000100 $ @ ; $ _ \n \t \t \t \t \ +n 4024 243b 0a5f 0909 2020 0920 2020 0a09 0000120 \t \t \t \n \t \t \t 2020 2020 2009 0920 0a09 0909 0920 2020 0000140 \t \t \n \t \t \t \t \t \n \t \ +t 2009 0a09 0920 0909 0920 2009 0a20 0909 0000160 \t \t \t \n \t \t \t \t \t \t 2009 0920 2020 0a09 0909 0920 0909 2009 0000200 \t \n \t \t \t \n \t 0a09 2020 0920 2020 0920 0a09 0920 2020 0000220 \t \t \t \n \t \t \t \t \t \t \ +n 2009 0909 0a20 0920 0909 0920 2009 0a09 0000240 \t \t \t \t \n \t \t \t 0909 2009 2009 2020 0a20 2020 0909 2009
Displaying HTML with perl
4 direct replies — Read more / Contribute
by Hramyzn
on Sep 24, 2014 at 15:25

    Hello, I'm starting a project and I need a bit of help. The problem is, how do I print HTML in a browser without putting the HTML code directly inside the Perl code. I'm familiar with the print "Content-type: text/html\n\n"; way of doing things, but I'm looking for a way to have and webpage.html files and by typing the URL which leads to the webpage.html is displayed. I want to separate the program logic (the .pl file) from the display (the .html file). Any ideas?

New Meditations
The importance of avoiding the shell
5 direct replies — Read more / Contribute
by jhourcle
on Sep 25, 2014 at 07:34

    For those who haven't heard, there was a Bash exploit announced yesterday. Although a patch did come out (4.3.25), there are reports that it does not fully fix the problem.

    Using variations of the test string that was posted to slashdot, it looks as if perl makes your system invulnerable:

    sh-3.2$ env x='() { :;}; echo vulnerable' sh -c "echo this is a test" vulnerable this is a test sh-3.2$ env x='() { :;}; echo vulnerable & echo' perl -e 'system "echo + test"' test sh-3.2$ env x='() { :;}; echo vulnerable' perl -e 'print `echo test`' test

    ... but unfortunately, perl only protects you when you either pass system a list. In other cases, if it sees a shell meta character in your string, you're still vulnerable:

    sh-3.2$ env x='() { :;}; echo vulnerable' perl -e 'print `echo test;`' vulnerable test sh-3.2$ env x='() { :;}; echo vulnerable' perl -e 'system "echo test;" +' vulnerable test sh-3.2$ env x='() { :;}; echo vulnerable' perl -e 'system qw(echo test +;)' test;

    Your main attack vector is CGIs -- anyone can set their user-agent, or pass in a query string, and the webserver will set environmental variables automatically. Should your scripts shell out, they're exploitable.

    So, the moral of the story: always use the list form of system, and avoid backticks if you can. If you have to do strange things w/ redirecting output, look at IPC::Open2 and IPC::Open3 which can also take list inputs.

New Monk Discussion
Daily Best Thread?
4 direct replies — Read more / Contribute
by QM
on Sep 29, 2014 at 05:01
    Being a somewhat erratic reader here, I find the Daily/Weekly/Monthly Best Nodes helpful to find high quality content in my limited time. (AFAIK, Daily Best does not have it's own page, but is derived from Best Nodes, and there's more info at About the Daily Best Nodelet.)

    But I often feel I'm missing out on the OPs that have lots of good discussion, while not making it into Best Nodes.

    My question: Is there a "Best Thread" mechanism? If not, is there enough interest to consider making one?

    Of course, this begs the question of what a "Best Thread" is, because we don't vote on threads. Given the current scheme, we'd have to compute some thread score, such as Sum_of_Node_Votes / Number_of_Nodes. Perhaps throw in a multiplier for unique responders, or unique readers.

    I did a quick search here, but of course "thread" comes up with the other kind, and I didn't find anything relevant. Please point me to any previous discussions.

    Quantum Mechanics: The dreams stuff is made of

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 contemplating the Monastery: (11)
As of 2014-09-30 17:23 GMT
Find Nodes?
    Voting Booth?

    How do you remember the number of days in each month?

    Results (378 votes), past polls