Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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
reference to an undefined key
No replies — Read more | Post response
by exilepanda
on Oct 10, 2015 at 08:20
    Hello monks,

    Sorry I believe I have given a pretty poor title but I really can't think of any better one, and I think this is the fact though. I'd like to know if there's any chance I can have such code candies ?

    my $obj = bless { Root => $rootDir, UserDir => $obj->{Root} . "Usr/$userId/", UserAppData = $obj->{UserDir} . "$appName/", }, shift; # of cause this won't work
    instead of :
    $obj = bless { Root => $rootDir, UserDir => undef, UserAppData => undef, }, shift; $obj->{UserDir} = $obj->{Root} . "Usr/$userId"; $obj->{UserAppData} = $obj->{UserDir} . "$appName";
    In fact I have much more paths that I need to defined while the object is created, but I hope to state the logic of the properties in clear at the first place.

    I guess I am out of luck but still trying to ask because when the interpreter read {Root}, that should be some place already stored the value ( but I juz don't know how to ref to it), no?

optgroup with Perl CGI
1 direct reply — Read more / Contribute
by alokranjan
on Oct 10, 2015 at 07:30
    Hi All,

    I have a folder "items" under which I have files like: fruit and animal containing the names of fruits and animals respectively. Now I want to use optgroup select option.

    #!/usr/bin/perl print "Content-Type: text/html\n\n"; my @filename = split("\n", `ls items`); print <<HTML <html> <body> <select> HTML foreach(@filename){ print <<HTML <optgroup label="$_"> HTML open my $info, "items/$_" or die "Could not open $_: $!"; while( my $line = <$info>) { print <<HTML <option value="$line">$line</option> HTML } } print <<HTML </optgroup> </select> </body> </html> HTML
    Can anyone please suggest me what went wrong. Many thanks in advance !!

    Regards, Alok

does system() return a pid?
4 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 ?


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 about the Monastery: (2)
    As of 2015-10-10 13:36 GMT
    Find Nodes?
      Voting Booth?

      Does Humor Belong in Programming?

      Results (257 votes), past polls