Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Perl: the Markov chain saw
 
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
run shell command as another user
2 direct replies — Read more / Contribute
by ox1d0
on Apr 16, 2014 at 20:59

    Hello Monks Sorry for my babyperl , I had happy perl learning, but I find a little Wtf! issue. I try to get db2versions, db2instances and list of data bases without modules. I have The "root powEr!" but When I trying "su - $user -c 'command to get DB list' " I get some like this.

    Product instance full path /opt/wheredb2/Version Current instance Userid
    but the command doesn't execute

    This is my code ...

    use warnings; use diagnostics; use strict; use 5.010; use Cwd; use DB2::Admin::Constants; ############################## + + + + ###Get Version and instances## + + + + ############################## + + + + sub check_for_instance{ open(EXEC, '-|', 'db2ls | awk \'!/Install/ && !/----/ \'') +or die "Can't exec: $!\n"; # Now read the output just like a file + + + + while(my $line = <EXEC>) { chomp $line; if($line=~/(\/db2\/V\d.\d+)/){ print "Product instance full path\n $`"."$1\n"; foreach ($line) { my $Path="$`"."$1"."/instance/"; chdir($Path) or die "Cant chdir to $Path $!"; my $dir = getcwd; open my $cmd, '-|', './db2ilist'; while ($line = <$cmd>) { say "Current instance \n$line"; my $user="$line"; my $action="db2 list db directory"; ######################### ### This doesn't works # ######################### system ("su -l - $user -c \'$action > DBlist.txt && cat + DBlist.txt\'"); ########################### # # # this works ! XD # ########################### #system ("su -l - MyRealuser -c \'$action > DBlist.txt && cat DBlist. +txt\'"); ####### } close $cmd; } } } close(EXEC); } &check_for_instance;

    btw ,I tried to use sudo - $user -c 'command', sudo su - $user "command", sudo - $user -c "command". but when i use directly the userid su - "Myrealuser" 'comman' works as i waiting.

    I hope someone can give me some light

Can Perl read another terminals STDOUT?
4 direct replies — Read more / Contribute
by westrock2000
on Apr 16, 2014 at 17:32
    We have a machine at work that runs diagnostic in a command tool. This machine is on Solaris 5.8 and uses Perl 5.8.8

    What I am wondering is if I could use Perl to watch the STDOUT of the command tool that runs the diagnostics so that I could save/parse certain data automatically? I only want to passively listen. I realize this would constitute some security issues as a general behavior, so I am expecting you to say that terminal sessions have limited access. But I thought I would ask.

    I have to be able to watch the terminal from another terminal session, because the program is started by using a shortcut. I can't run the perl script before hand in the same session nor execute it from Perl. The diagnostic program has to be executed from the shortcut.
$SIG{ALRM} to just log something?
4 direct replies — Read more / Contribute
by philkime
on Apr 16, 2014 at 15:54
    I am trying to use alarm() to log/send email/open ticket after a process has been running for a certain amount of time:
    local $SIG{ALRM} = sub { write_log('TIMEOUT ALERT'); }; alarm 20; my ($run_ok, undef, $full_buf, undef, undef) = run(command => $cmd); alarm 0;
    This is using IPC::Run to run the command. I am running many commands, some taking more than 20 seconds but the message "TIMEOUT ALERT" never appears in the log and all commands run to completion as normal. I don't want to kill anything (contrary to most alarm() examples I find), I just want to let the command run but send some warning somewhere if it takes X amount of time. Is there something obviously wrong here?
OLE::Storage_Lite problems
1 direct reply — Read more / Contribute
by alvinstarr
on Apr 16, 2014 at 15:14
    I am trying to use OLE::Storage_Lite. I read an OLE and write the file using the following code
    my $oOl = OLE::Storage_Lite->new("test.story"); my $oPps = $oOl->getPpsTree(1); print "\n"; $oPps->save("savefile.story");
    the resulting file is not the same size as the original file and for any complex files the data seems to be corrupt even by the '/usr/bin/file' command. Any help would be appreciated.
python YAML output, perl Loader, which version?
2 direct replies — Read more / Contribute
by mgwmgw
on Apr 16, 2014 at 12:05

    I am writing tests in perl 5.10.1 of a part of a product written in python 2.6.6.

    The python, which I think is using PyYAML, outputs YAML which is acceptable to pylint, but not acceptable to the YAML Loader in perl. I find multiple different examples of this. I think the perl is using YAML::Old.

    I suspect that the best solution would involve moving to a newer YAML Loader in perl, but I seek advice about which one. The PyYAML port to perl supposedly is not finished yet.

    Thanks.

How to get the path coverage (or lines of code executed) after running a perl test file
2 direct replies — Read more / Contribute
by perl_help26
on Apr 16, 2014 at 11:11

    Hello Everyone, I wrote a test file using LWP::Simple. How can I know the path coverage of my test file or the lines of code being executed in my program? Is there an easy way? Preferably not with Devel::Cover since it just won't install using cpan!... (there's a test that's failing aeval_merge.t does anyone know why? Thank you...

Help required in find command.
6 direct replies — Read more / Contribute
by maestromani
on Apr 16, 2014 at 09:36
    Hi There, I got a log file which is as follow.
    ###################################################################
    Name -> Name of the person,Path -> include \\path1\\path2\file.txt
    #user: machinename\name
    #log forward
    #Date setup: 12/01/2010
    # can be n number of line
    Name -> Name of the person
    ###################################################################
    writing a perl script which accept file name as input and match file name with the path and if matches I need to print the name in the last. log file will have n number of block like that and file name can match n number of time. how to proceed. Don';t like to use $flag, looking for best option. my piece of code looks like $file_name is command line parameter. while (<DATA_FILE>) { $str = $_ ; if ($str =~ /include/) { if ($str =~ /\b$file_name\b/) { $flag = "TRUE" ; } } if ($flag eq "TRUE") { if ($str =~ /View Name/) { print $str ; ($junk,$view_name) = split(\->\,$str) ; print $view_name ; $flag = "FALSE" ; } } #print "$_\n"; }
First Question, why something works? (-M)
1 direct reply — Read more / Contribute
by i255d
on Apr 16, 2014 at 08:26
    I am new to Perl and working my way through one of the more popular Learning Perl Books. It usually takes me a while, but I can usually figure out why a certain piece of code in the Example sections works, but this time I am just not sure why. Here is the code:
    #!/usr/bin/perl use strict; use warnings; use 5.010; die "No file names supplied!\n" unless @ARGV; my $oldest_name = shift @ARGV; my $oldest_age = -M $oldest_name; foreach (@ARGV) { my $age = -M; ($oldest_name, $oldest_age) = ($_, $age) if $age > $oldest_age; } printf "The oldest file was %s, and it was %.1f days old\n", $oldest_name, $oldest_age;
    How does $age know to apply -M to the file going through the foreach loop?
Duplicate emails using MIME::Lite with smtp
1 direct reply — Read more / Contribute
by sandor.farkas
on Apr 16, 2014 at 05:36

    Hello,

    I have an issue sending emails via MIME::Lite module. I have to set some header parameters (return-path and reply-to) therefore I have to use $msg->send('smtp', $SMTP_HOST);

    If I understand right in this case the message now sent via Net::SMTP. Unfortunately it results that if an email address is included in both to and cc fields the message is delivered twice. I'm googling for a while now, but I can't figure out what is the reason behind. Can anyone please give me a hint on this?

    Thank you in advance!
    Sandor
Comparing results of math operations
8 direct replies — Read more / Contribute
by stangoesagain
on Apr 16, 2014 at 05:12
    Little real life problem
    #!/usr/bin/perl use strict; use warnings; use 5.010; my $x=0.95*806; my $y=1.3*589; say $x <=> $y; $x == $y ? say 'equal' : say $x.' '.$y; say $x cmp $y; $x eq $y ? say 'equal' : say $x.' '.$y; #now with simpler math $x=2*1.25; $y=5*0.5; say $x <=> $y; $x == $y ? say 'equal' : say $x.' '.$y; say $x cmp $y; $x eq $y ? say 'equal' : say $x.' '.$y;
    gives
    -1 765.7 765.7 0 equal 0 equal 0 equal
    Why? What's wrong?

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
  • Outside of code tags, you may need to use entities for some characters:
            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 web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (7)
    As of 2014-04-20 07:07 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (485 votes), past polls