Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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
does system() return a pid?
3 direct replies — Read more / Contribute
by sciguy
on Oct 09, 2015 at 23:31
    Does the system() function return a value (outside of the expected output to stdout)? and is that value the PID of the child process?
File handles with forked processes
2 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 09, 2015 at 19:20
    Hi PerlMonks I would be very grateful for some advice.

    I have a perl program running in windows that forks a child process. The child process opens a file handle $fh and starts some work with the file.

    A short while later the parent forks a second child process. Much to my surprise the file opened in the first child is also opened in the second child according to process explorer (although I doubt the file handle created in the first child is shared) The first child finishes its work and closes the file handle and finishes. Annoyingly though the file stays open till the second child has completed and finished. I don't want this. The files are temporary and I need to be able to delete them without another child locking them.

    I know that file handles are passed from parent to child processes. I wasn't expecting anything created in 1 child being locked by the second though.

    Any help much appreciated.

Is there a way to open a memory file with binmode :raw?
2 direct replies — Read more / Contribute
by stevieb
on Oct 09, 2015 at 16:54

    Instead of using a temporary disk file, I thought I'd try to use a memory file (scalar ref), but I need to be able to use the handle with binmode $fh, ':raw' (or equivalent). Is this possible?

    use strict; use warnings; my $mf; my $fh; # mem write open $fh, '>', \$mf or die $!; print $fh "hello\n"; close $fh; # mem read open $fh, '<', \$mf or die $!; binmode $fh, ':raw'; while (<$fh>){ print "mem: " . (unpack "H*", $_) . "\n"; } close $fh; # file write open $fh, '>', 'file.txt' or die $!; print $fh "hello\n"; close $fh; # file read open $fh, '<', 'file.txt' or die $!; binmode $fh, ':raw'; while (<$fh>){ print "file: " . (unpack "H*", $_) . "\n"; }

    Output... note the difference after the 'f'. I need the mem file to reflect that of :raw like the value for file.

    mem: 68656c6c6f0a file: 68656c6c6f0d0a
how to remove a string from end of a line
3 direct replies — Read more / Contribute
by rpinnam
on Oct 09, 2015 at 14:58
    My input :


    I want my output as:


CPAN on El Capitan
5 direct replies — Read more / Contribute
by rhumbliner
on Oct 09, 2015 at 11:58
    Apple's newest release of OS X, El Capitan, includes a security feature that hobbles some of the capabilities of root: SIP. This includes preventing root from changing /usr/bin. This also prevents cpan from installing a new version of CPAN as seen here:
    {s10:~} whoami root {s10:~} cpan CPAN CPAN: Storable loaded ok (v2.41) Reading '/var/root/.cpan/Metadata' Database was generated on Thu, 08 Oct 2015 10:41:02 GMT Running install for module 'CPAN' Running make for A/AN/ANDK/CPAN-2.10.tar.gz CPAN: Digest::SHA loaded ok (v5.84_01) CPAN: Compress::Zlib loaded ok (v2.069) Checksum for /var/root/.cpan/sources/authors/id/A/AN/ANDK/CPAN-2.10.ta +r.gz ok CPAN: File::Temp loaded ok (v0.23) CPAN: Parse::CPAN::Meta loaded ok (v1.4417) CPAN: CPAN::Meta loaded ok (v2.150005) CPAN: Module::CoreList loaded ok (v3.03) Building A/AN/ANDK/CPAN-2.10.tar.gz Checking if your kit is complete... Looks good Generating a Unix-style Makefile Writing Makefile for CPAN Writing MYMETA.yml and MYMETA.json <snip> All tests successful. Files=30, Tests=701, 28 wallclock secs ( 0.39 usr 0.06 sys + 18.83 cu +sr 3.16 csys = 22.44 CPU) Result: PASS ANDK/CPAN-2.10.tar.gz /usr/bin/make test -- OK Running make install Manifying 2 pod documents Manifying 16 pod documents !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +!! ERROR: Can't create '/usr/bin' Do not have write permissions on '/usr/bin' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +!! at -e line 1. make: *** [pure_perl_install] Error 1 ANDK/CPAN-2.10.tar.gz /usr/bin/make install -- NOT OK {s10:~}

    I'm looking for suggestions on how to best work around this. It is possible to disable SIP but I'd prefer not to.


REGexp match query
5 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 09, 2015 at 09:24

    I am trying to match this output.

    Queue counters: Queued packets Trans packets Dropped pak 0 258520344 520344 0 1 11006822 1006822 0 2 832114 0832114 0 3 8294677 8294677 0 4 5416057 5416057 0
    I want to check , whether Queue counters: is 0 and there are digits for both Queued packets and Trans packets and Dropped Pak is 0. Similarly for other Queue counters till 4
    $rh->cmd('show class-of-service interface et-0/3/0 com +prehensive | find " Queue counters:"');--> this displays the above ou +tput, internal command my $output = $rh->get_response; my @outputlist = split /\n+/, $output; foreach (0..$#outputlist) { if (($outputlist[$_] = /^0/) && ($outputlist[$ +_] = /^0\s(\d+)\s(\d+)/)) { print " match is $outputlist[$_]\n"; } } }

    In the above code for sample, i tried to match Q 0 and space and then digit for the Queued packets & Trans packets respectively. Looks to me its not matching. anything missing here

[Resolved] strange list operator
5 direct replies — Read more / Contribute
by xiaoyafeng
on Oct 09, 2015 at 04:25
    I found the list operator(..) in 32bit perl is very mysterious. Take a look below codes:
    #1: perl -E"my @aa = 1..20_000_000; my @bb = 1..20_000_000;" #error! out o +f memory! #2: perl -E"my @aa = 1..20_000_000; my @bb = @aa;" #works well #3: perl -e"my @cc = 1..40_000_000" #error! out of memory #4: perl -E"my @cc; for(my $i = 1; $i<40_000_001; $i++){ push @cc, $i;} pr +int scalar @cc" #works well. #5: perl -e"push @cc,$_ for 1..40_000_000; print scalar @cc;" works well
    Could anyone tell me how implement range operator in perl source? I can't figure out how example 1 and 4 is broken.

    UPDATE1: add example 5

    UPDATE2: choroba perfectly answer my question, thankss

    I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

[Linux] Controlling 32-bit mode vs 64-bit mode
1 direct reply — Read more / Contribute
by syphilis
on Oct 09, 2015 at 01:58
    When I first built perl on a powerpc64 box (running Debian Wheezy) it built perl in 32-bit mode - ivtype of int, ivsize of 4, ptrsize of 4.
    But I wanted a 64-bit perl, and I discovered I could achieve that by simply adding -Dcc="gcc -m64" to the configure args I had used.
    That worked fine - perl then had ivtype of long, ivsize of 8 and ptrsize of 8.

    However, did I choose the correct way of forcing 64-bit mode ? I've always been a bit concerned that the "-m64" should be in cflags, not part of the name of the compiler.
    And, although that approach has worked fine for me for the last couple of years, I'm striking a problem compiling the (optional) gfortran elements in the PDL (perl) module ... and I'm beginning to think that problem might be due to the presence of "-m64" in $Config{cc}.

    If I have chosen the wrong means to the end, then what's the correct/recommended means ?

Passing file location in a subroutine
3 direct replies — Read more / Contribute
by Rapazzini
on Oct 09, 2015 at 01:31
    Dear All I am trying to pass a file location in a subroutine to perform a file operation but I am unable to do so. Please have a look I am providing the code. package alter; sub onPerform { my $nec = shift; my $ipic1 = org::item::essential($nec, "Input Picture"); my $(ipic) = $nec->locateAbsolutepath($ipic1); How to call the method and pass the file location for "Input Picture".
File::Tail can not get output
3 direct replies — Read more / Contribute
by leostereo
on Oct 08, 2015 at 16:27

    Hello monks: I was tryng to feed a file with last ip granted by my dhcp server. Trying to inspect dhcpd.log file using tail, I tryed a simple code from documentation.

    #!/usr/bin/perl use File::Tail; $file=File::Tail->new("/var/log/dhcpd.log"); while (defined($line=$file->read)) { print "$line"; }

    Here dont get any output nor error so, then I tryed other piece of code that works ok.

    #!/usr/bin/perl -w use File::Tail; sub find_aaa_name { return '/var/log/dhcpd.log'; } my $file = File::Tail->new( name =>'/opt/dhcp_tail/dummy.txt', interval => 1, maxinterval => 1, resetafter=> 5, name_changes=> \&find_aaa_name, ignore_nonexistant => 1 ); while (defined($line = $file->read)) { print $line,"\n"; } 1;
    Can someone give me a clue? What is the difference ; why test1 dont work and dont print even an error message and test2 works ok ? I hope it can be usefull to someone. Regards, Leo.

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 surveying the Monastery: (5)
    As of 2015-10-10 07:12 GMT
    Find Nodes?
      Voting Booth?

      Does Humor Belong in Programming?

      Results (255 votes), past polls