Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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
finding the correct integer
5 direct replies — Read more / Contribute
by YarNik
on Nov 30, 2020 at 09:44
    What other options are there for finding the correct integer? I tried but got an error:
    use POSIX; print int(0.6505 / 0.0001); print "\n"; print sprintf("%d", 0.6505 / 0.0001);
    -> 6504 need 6505
source a file in Linux using perl script
5 direct replies — Read more / Contribute
by perl_007
on Nov 30, 2020 at 02:08

    I'm new to perl scripting and trying to source .bash_profile file and then do cd to the directory. below is the content of the .bash_profile file:

    # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs export CTC_HOME=/Inf/work/ #PATH=$PATH:$HOME/.local/bin:$HOME/bin #export PATH

    below is the script i'm trying to create:

    #!/bin/perl # use strict; use warnings; if(-s "/cs/ctc/.bash_profile"){ `source ~/.bash_profile`; }else { print "The file doesnot exists","\n"; exit 1; } my $dir = "cd $CTC_HOME"; chdir($dir);

    But, getting below error:

    Global symbol "$CTC_HOME" requires explicit package name at ./program. +pl line 12. Execution of ./ aborted due to compilation errors.
    Any help is most welcome!!!
Understanding Devel::Peek output
3 direct replies — Read more / Contribute
by syphilis
on Nov 29, 2020 at 07:32

    On my 64-bit Windows 7 OS, I have:
    1) perl-5.32.0 built using a 32-bit gcc-8.3.0 compiler;
    2) perl-5.32.0 built using a 64-bit gcc-8.3.0 compiler.

    On both of those perls, the ivtype is the 8-byte "long long".

    On the 32-bit build I get:
    C:\>perl -MDevel::Peek -e "Dump 1 << 62;" SV = IV(0x1dc8d64) at 0x1dc8d64 REFCNT = 1 FLAGS = (PADTMP,IOK,READONLY,PROTECT,pIOK) IV = 4611686018427387904
    Note that the first line contains two occurrences of the same hex value - 0x1dc8d64 in this instance. Every time I run that command on that 32-bit build of perl, the two hex values are always equivalent - though, of course, they're not always 0x1dc8d64.

    When I run the same command on the 64-bit build, I get:
    C:\>perl -MDevel::Peek -le "Dump 1 << 62;" SV = IV(0x33d430) at 0x33d440 REFCNT = 1 FLAGS = (PADTMP,IOK,READONLY,PROTECT,pIOK) IV = 4611686018427387904
    For this build of perl, it is always the case that the first hex value is less than the second value by 0x10.

    How is this differing behaviour between the 32-bit build and the 64-bit build explained ?

    IIUC, on the 64-bit build, the amount of memory being allocated for the IV is 0x10 (16) bytes more than is necessary ... and I wonder what is responsible for that extravagance.
    Is it perl ? ... or the OS ? ... or the compiler ? ...

Formatting variables
8 direct replies — Read more / Contribute
by Bod
on Nov 29, 2020 at 06:24

    I have been rereading Re^3: Pointers and References and pondering variable names. It is clear from comments that different people have different conventions for how they name and structure their variable names. This is not something I have given much thought to until recently.

    With the exception of constants, in the past I have generally used all lower case with an underscore sometimes between words although omitted if the name is clearly discernable. Over the last year or so I've started capitalising the first letter of each word other than the first which works better I think with more than two words.
    For example $user_number and $userNumber.

    Last evening I was writing some code around CRM access permissions within different business units and ended up with some poor variable names within the Perl generated web form:

    crmr_5 Read access to business unit 5 crmw_3 Write access to business unit 3 crmn_1 Read and write notes access...

    I'm wondering how you do it, why you do it that way and if you think it matter a great deal.

transferring \n to linux from perl via serial port
3 direct replies — Read more / Contribute
by digital_newbie
on Nov 28, 2020 at 11:00

    I am trying to control an OMAP with Linux, from a Windows laptop running Perl via serial port. I could open serial port (COM1) and send commands, but not able to execute them by sending the enter key. I tried \n but it only moving the cursor to the next line, and not imitating enter key. Code is below. Any suggestions? Please help!!

    open( PORT, "+>COM1") or die "Cant open COM1 - $!"; $msg="ls\n"; print PORT $msg;

    The output comes as

    .#ls |

    but doesn't execute..


    Code tags and various other formatting corrected by Grandfather

[Win32] Escaping quotes in perl one liner
3 direct replies — Read more / Contribute
by syphilis
on Nov 28, 2020 at 01:48

    I run:
    C:\_32>type open(my $x,">&STDOUT"); C:\_32>perl C:\_32>perl -le "open(my $x,\">&STDOUT\");" >& was unexpected at this time. C:\_32>
    Why does the one-liner throw that error ?
    Having escaped both double quotes I expected the one liner to behave in exactly the same way as the script - ie to simply silently exit.

    I know that replacing the double quotes with singles works around the issue:
    C:\_32>perl -le "open(my $x,'>&STDOUT');" C:\_32>
    But is there a way to retain the use of double quotes and still get the one-liner to DWIM ?

mod_perl on apache works when symlinked to one directory but not to another
3 direct replies — Read more / Contribute
by Cody Fendant
on Nov 27, 2020 at 22:43

    This is kind of an embarrassing one. I'm sure I must be missing something obvious.

    I have a Catalyst based website running in mod_perl.

    There are two versions of the website, let's call them old and new (new was cloned from a remote git repo to the server, could that be causing the problem?)

    • The website works when the directory /var/www is symlinked to old, as in

      ln -s /path/to/old /var/www

    • The website does not work when the directory /var/www is symlinked to new, as in

      ln -s /path/to/new /var/www
      (removing the old link first of course).

    The reason it doesn't work, seen in apache error logs, is that it can't find the custom perl modules in /path/to/new. It says it can't find them in @INC and that @INC contains /var/www and /var/www/whatever/lib/Module/etc which is correct

    When I remove that symlink and re-link /path/to/old it works just fine.

    If I do perl -I/path/to/new -MSome::Module::In::New there's no problem, it finds the module just fine.

    I've checked permissions and made sure they're the same, or even less restrictive on the files in /path/to/new. The relative paths are all correct, as far as I can see my /path/to/old and /path/to/new directories are exactly the same, except that apache/mod_perl can see one and not the other.

    This is on an Amazon EC2 running linux by the way.

Extract information from several files in directory
5 direct replies — Read more / Contribute
by Sofie
on Nov 27, 2020 at 09:36

    Hi I am a beginner at Perl, trying to do some really simple things, but nothing seems simple.. I have a folder with a bunch of tab sep txt files. All the files have the same type of information, and I need to extract the information from one of the columns in each file and put in a new file. I have previously managed to do this with one file by readin gthe file into an array and iterating each row of the array, spliting it on tab and the taking the second column (where the data I want is) and printing this to a new file. But now I have several files. I started by using a while loop to open the directory and counting the number of files in the directory. But struggling to get to reading each file and extracting my data.. After lots of googling, there seems to be lots of different answers, but can't really understand them. I need something very simple so I can understand how it works.

    #!/usr/bin/perl -w use strict; #create file to write to open (LABNR, ">>Labnr_all.txt") or die "Could not open file"; #open the directory where files are located $dirname = 'Filer'; opendir (DIR, $dirname) or die "Could not open $dirname\n"; #count files $nrfiles = 0; while ($filename = readdir(DIR)){ $nrfiles ++ if $filename =~ /\.txt/; print "$filename\n" if -f $filename; #this is where I want to look into each file and extract the info... } print "The number of files in the folder: $nrfiles\n"; closedir(DIR)
2 direct replies — Read more / Contribute
by marcelv
on Nov 26, 2020 at 15:32

    Hi all,

    I currently have quite some scripts running on Perl 5.24 on a Microsoft Windows Server running IIS 10.0. On this server I use a certificate to for HTTPS connections. The scripts are all using the module to do CGI. As I do use Non-Parsed Headers (NPH), all my scripts start with the prefix "nph-" and have the following line to import the CGI module:
    use CGI qw/:standard -nph/;

    Now when a browser requests one of my scripts, these scripts are executed twice. I have made a Wireshark trace and I see the following happening:

    1. The browser does the TLS handshake
    2. The browser does a HTTP2 request to the script
    3. The server responds with HTTP_1_1_REQUIRED
    4. The browser does another request, but now uses HTTP/1.1
    5. The server sends a HTTP/1.1 response with the script output

    This is most evident when Firefox is used, as it does not remember it should use HTTP/1.1 for the request, where Chrome, Edge and Safari do remember.

    Now when I remove the "nph-" prefix from the script name (and do not change the contents), the following happens:

    1. The browser does the TLS handshake
    2. The browser does a HTTP2 request to the script
    3. The server sends the HTTP2 response with the script output

    Now I could change all my script names by removing the "nph-" prefix, but it seems that the scripts do not really do NPH anymore as the server is sending back a HTTP2 response, whereas with the "nph-" prefix the exact headers as generated by my script are received by the browser.

    Now my question: is it possible to HTTP2 via CGI using the CGI module or any other module?


Watch the value of hash key in perldebug
2 direct replies — Read more / Contribute
by frogsausage
on Nov 26, 2020 at 09:00

    Hello Monks, long time no see...!

    Here's my situation:

    • - I have a (very large) piece of code with many modules
    • - it is is read only

    I have a file that contains $hash{key}{key}{key} = "value".
    $hash is defined as our
    From the main code, it is read in a sub inside one of these modules.
    When eval'd in that sub, its content updates the value for $hash{key}{key}{key}

    I already figured out what was happening by reading the code, setting a fair number of breakpoints and using X var in the debugger.
    I believe I could have done it smarter and watching $hash{key}{key}{key} and voilą.

    I tried the following:

    DB<341> w $hash->{key}->{key}->{key}

    But it gives me this:

    Use of each() on hash after insertion without resetting hash iterator +results in undefined behavior, Perl interpreter: 0x677010 at /somepat +h/cpan/5.18.4/1/somebuild/lib/perl5/Devel/ line 108. at /somepath/cpan/5.18.4/1/somebuild/lib/perl5/Devel/ line +108. Devel::Symdump::_symdump(Devel::Symdump=HASH(0x9d20270), "main") c +alled at /somepath/cpan/5.18.4/1/somebuild/lib/perl5/Devel/ + line 43 Devel::Symdump::_doit(Devel::Symdump=HASH(0x9d20270), "main") call +ed at /somepath/cpan/5.18.4/1/somebuild/lib/perl5/Devel/ li +ne 37 Devel::Symdump::new("Devel::Symdump", "main") called at [REDACTED] + line 361 [REDACTED]

    Any idea?

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!
  • 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?

    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 lurking in the Monastery: (9)
    As of 2020-12-02 19:19 GMT
    Find Nodes?
      Voting Booth?
      How often do you use taint mode?

      Results (44 votes). Check out past polls.