Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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
Testing $! when use strict is in effect
2 direct replies — Read more / Contribute
by Bloehdian
on Oct 20, 2016 at 14:41

    Hi monks,

    probably a quite simple question for most of You, but I've just got stuck on it:

    In one of my progs I have the following

    if ( ! defined $test && $! == EWOULDBLOCK ) {

    which, since use strict is effect gives the error message

    "Bareword "EWOULDBLOCK" not allowed while "strict subs" in use"

    How can I test $! anyways?



sfv checker sting crc32
2 direct replies — Read more / Contribute
by reptizarx3
on Oct 20, 2016 at 12:59

    Hi guys! First of all newbie here, but want to learn, need help about one script sfv checker for crc checking on uploaded files on ftp server(wingftp): My steps: 1.Download and install Stawberry perl x64 run on win7x64, (installed into C:\Strawberry) 2.Restart 3.Download String-CRC32-1.5.tar.gz and unzip it to other partition d: Installation instruc says: "perl Makefile.PL" "make" "make test" "make install" 4.Open Perl cmd and input D:\String-CRC32-1.5\Makefile.PL 5.input make and get this error: 'make' is not recognized as an internal or external command, operable program or batch file. What i doing wrong? Here is the whole script and don't know even i get to work:

    #!/usr/bin/perl # Make sure this points to your perl executable # String::CRC32 is required you can install using cpan use String::CRC32; # Wing Server SFV Checker v1.00b - benny / # # I put this together because the sfv checker that comes with Wing Ser +ver isn't recursive # and wont check sub directories inside the specified sfv checking dir +ectory # # Apologies for the sloppy code I wanted a working sfv checker quick, +ill try and clean it # up and add more features when I have more time. # # Known bugs / Irrelevant issues? # - As of now this will only check files in a sfv against the files u +ploaded # to the directory that your uploading to, so if any file not in th +e sfv is uploaded # it will just be ignored and possible to just have misc unwanted f +iles laying around.. # # - The $home variable could cause issues with multiple domains and u +sers depending on your setup.. # on my setup i have all my files going to /usr/files and my one us +er having a home directory of /usr/files # so everything sent to this script would be /Dircreated/Files.. wh +ich would be # ($home/Dircreated/Files) -> /usr/files/Dircreated/Files # - I'll add a more flexable $home in the future! # # - If there is more than one sfv this script will just exit.. # # Using / Installing this # - You will need to run cpan and install String::CRC32 # - Put this perl script somewhere you can find it later # - chmod +x /path/to/where/you/put/checker # # - Goto the Wing Server http admin panel and click on a domain # - Click on Ftp Events # - Double Click OnFileUploaded # - Check mark "Enable Execute Program" # - Program Path is /path/to/where/you/put/checker you did earlie +r # - For Paramaters put: %Dir # - Press OK! # # Conclusion # - I would recommend having your ftp client set a priority on sendin +g sfv first but on a side # note even if you sent a few files first and then uploaded the sfv + how this scans files apon upload # it will check the previous files you uploaded and add them to the + cached files # # - Enjoy and let me know if you have troubles or like this! # my $home = "/usr/files"; my $sfiles = 0; my $tfiles = 0; # For creating empty missing/bad files sub touch { if ($#_ != 0) { return; } if ( -f $_[0] ) { return; } open tch, ">$_[0]"; close tch; } opendir(SFVCHK, ${home}.$ARGV[0]); my @SFVFIN = grep(/\.sfv/, readdir( +SFVCHK)); closedir(SFVCHK); opendir(SFVCHK, ${home}.$ARGV[0]); my @CMPCHK = grep(/^\[.*COMPLETE\s\ +]$/, readdir(SFVCHK)); closedir(SFVCHK); # If there isn't a sfv or more than 1 just exit if (@SFVFIN != 1) { exit; } # Keep a hidden verified file to prevent double checking and system lo +ad my @CHECKED = (); if ( -e "${home}$ARGV[0]/.chk" ) { open CKSFV, "<${home}$ARGV[0]/.chk"; while (<CKSFV>) { chomp($_); push(@CHECKED, $_); } close CKSFV; } # Open file for appending anymore valid files we might come across open CKSFV, ">>${home}$ARGV[0]/.chk"; # Open the sfv and log the files that are suppose to be here # before we run the sfv checker to compare open SFVFD, "<${home}$ARGV[0]/$SFVFIN[0]"; while (<SFVFD>) { chomp($_ +); push(@LINES, $_); } close SFVFD; foreach my $deldir (@CMPCHK) { rmdir "${home}$ARGV[0]/$deldir"; } foreach my $line (@LINES) { chomp($line); if ($line =~ /^;/) { next; } my @SP = split(/ /, $line); my $rel = uc($SP[1]); $tfiles++; $rel =~ s/(\W)//g; # Make sure the crc string doesnt contain a +ny control characters if ( -e "${home}$ARGV[0]/$SP[0]" ) { my $LFILE = $SP[0]; if (grep(/$LFILE/, @CHECKED)) { $sfiles++; next; } open(CRCK, "${home}$ARGV[0]/$SP[0]"); my $crc = crc32(*CRCK) +; close(CRCK); my $hex = sprintf "%x", $crc; $hex = uc($hex); if (length($hex) < 8) { $hex = "0".$hex; } if (length($rel) < 8) { $rel = "0".$rel; } if ($hex eq $rel) { if ( -e "${home}$ARGV[0]/$SP[0]-missing" ) { unlink("$ +{home}$ARGV[0]/$SP[0]-missing"); } if ( -e "${home}$ARGV[0]/$SP[0]-bad" ) { unlink("${hom +e}$ARGV[0]/$SP[0]-bad"); } print CKSFV "$LFILE\n"; $sfiles++; } else { if ( ! -e "${home}$ARGV[0]/$SP[0]-bad" ) { touch("${ho +me}$ARGV[0]/$SP[0]-bad"); } } } else { if ( -e "${home}$ARGV[0]/$SP[0]-missing" ) { next; } touch("${home}$ARGV[0]/$SP[0]-missing"); } } close CKSFV; my $newdir = sprintf "[ %iF %iF FILES - 0.00%% COMPLETE ]", $sfiles, $ +tfiles; if ($sfiles != 0) { $newdir = sprintf "[ %iF of %iF - %.1f%% COMPLETE +]", $sfiles, $tfiles, (($sfiles / $tfiles) * 100); } if ($sfiles == $tfiles) { $newdir = "[ ${tfiles}F COMPLETE ]"; } mkdir "${home}$ARGV[0]/$newdir";
Reference is experimental
6 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 20, 2016 at 10:16

    Hi, I keep getting the following error message: "shift on reference is experimental at beta_table_maker line 27. Not an ARRAY reference at beta_table_maker line 27, <FILEHANDLE> line 1". How can I solve this? Thanks in advance

    my($counter) = 0; my(@AoA); while (@ARGV){ my($filename) = $ARGV[$counter]; open (FILEHANDLE, $filename); my(@tmp) = <FILEHANDLE>; shift $tmp[0]; push @AoA, [ @tmp ]; close FILEHANDLE; $counter++; } my ($aref) = ''; foreach $aref (@AoA){ print "\t [ @$aref ],\n"; }
Can't locate loadable object for module HTML::Embperl in @INC
2 direct replies — Read more / Contribute
by tushar_linux
on Oct 20, 2016 at 10:07

    Hello Monks,I am new to perlmonks and struggling with my apache webserver.

    The htdocs of apache has ehmtl pages and perl scripts. I am able to launch the webserver with html pages. But getting below error while browsing ehtml pages.

    Thu Oct 20 09:45:37 2016 error Can't locate loadable object for module HTML::Embperl in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /opt/web/cgi-bin/ line 18, referer: Thu Oct 20 09:45:37 2016 error Compilation failed in require at /opt/web/cgi-bin/ line 18., referer: Thu Oct 20 09:45:37 2016 error BEGIN failed--compilation aborted at /opt/web/cgi-bin/ line 18., referer: Thu Oct 20 09:45:37 2016 error Premature end of script headers:, referer:

    It seems my is not able load perl modules.

    Any help would be highly appreciated.thanks in advance

    (edited for formatting by erzuuli)

How to get directory listing using Net::SFTP::Recursive
2 direct replies — Read more / Contribute
by goulmi01
on Oct 20, 2016 at 09:53
    I am needing to convert an old Perl program from using Net::FTP::Recursive to SFTP. I don't see anything in any documentation or examples on how to just create a file list to a file from the remote server to a local directory and file. In the FTP there was a method called rdir which I used. Thanks Michael Gould
How relevant is the order of 'use's ?
2 direct replies — Read more / Contribute
by Krambambuli
on Oct 20, 2016 at 07:47
    Dear Monks,

    I have two simple modules:
    package Demo1; use base qw/Exporter/; $SUCCESS = 1; BEGIN { use Exporter(); @ISA = qw(Exporter); @EXPORT = qw( $SUCCESS ) } 1;
    package Demo2; sub import { ${[caller]->[0].'::'}{$_} = ${__PACKAGE__."::"}{$_} foreach grep { not /^(ISA|isa|BEGIN|import|Dumper)$/ } keys %{__PACKAGE__."::"}; } use constant { SUCCESS => 0, }; 1;
    and a minimalistic test program, that is
    #!/usr/bin/perl use strict; use warnings; use Demo1; use Demo2; print "SUCCESS: ", SUCCESS, "\n"; print "\$SUCCESS: $SUCCESS\n"; exit;
    If I run the program as shown, I see an compile time error, like
    Bareword "SUCCESS" not allowed while "strict subs" in use at ./ + line 9. Execution of ./ aborted due to compilation errors.
    but if I simply change the order of the use instructions, i.e. I run
    #!/usr/bin/perl use strict; use warnings; use Demo2; use Demo1; print "SUCCESS: ", SUCCESS, "\n"; print "\$SUCCESS: $SUCCESS\n"; exit;
    then the displayed result is the expected one,
    I'd love to understand what's happening here - and would bve grateful to learn if there is a way to not have to use the two modules in a strict order in order to have the code working nevertheless.

    Many thanks in advance.
How to pass a Format Heading from a variable?
3 direct replies — Read more / Contribute
by ankit.tayal560
on Oct 20, 2016 at 01:01
    SCRIPT TO GENERATE REPORT : use warnings; format DATA2= ------------------------------------------------------------ @<<<<<<<<<<<<< @<<<<<<<<< @######### @######## $name $format $matches $runs ------------------------------------------------------------ . format DATA2_TOP= Records/Data of the trio ============================================================ Name Format of match matches played runs scored ============================================================ . open(DATA,"<C:/Perl/perl_tests/sports.txt"); @array=<DATA>; close(DATA); open(DATA2,">>c:/perl/perl_tests/blank.txt"); foreach(@array) { chop; ($name,$format,$matches,$runs)=(split(/!/)); write(DATA2); }

    My sports.txt file is as follows:

    sports.txt file : sachin tendulkar!ODI!434!12000 sachin tendulkar!Test!246!10900 sachin tendulkar!T20!189!5000 sourav ganguly!ODI!334!8000 sourav ganguly!Test!235!5000 sourav ganguly!T20!124!1800 rahul dravid!ODI!387!9000 rahul dravid!Test!212!5980 rahul dravid!T20!43!1345

    The formatted report which I am getting in blank.txt is the final report I want but here in this script the heading of the report i.e. "records/data of the trio" is directly given. how can I pass a variable in place of that title.?Any Help is appreciated!

Longest Increasing Subset
6 direct replies — Read more / Contribute
by gilthoniel
on Oct 19, 2016 at 17:11

    I have an array of numbers (2 6 5 7 4 3 9) and am trying to find the longest increasing subset (2 6 7 9) from these numbers. I'm using algorithm::combinatorics to find combinations of these numbers starting from the largest and essentially trying to keep the first combination that has all the numbers in ascending numerical order, but for some reason it's not stopping where I expect it to and giving the answer (2 6 7 4 3)

    until(@answer) { my $iter = combinations(\@array2,$Fcount); while (my $p = $iter->next) { until(@answer) { my @forward = @$p; my $number1 = 1; my $number2 = 2; if ($forward[$number1] < $forward[$number2]) { $number1++; $number2++; } else { last; } if ($number2 = (scalar(@forward)+1)) { push @answer, @$p; print "@$p\n"; last; } } } $Fcount = ($Fcount - 1); }
Is RegEx in Perl not NFA anymore?
4 direct replies — Read more / Contribute
by redbull2012
on Oct 19, 2016 at 13:57

    Hello Monks,

    I have some doubts about RegEx engine in Perl. The thing is, i know that there are fundamentally 2 RegEx engine NFA and DFA. The source of my info is in below link, i hope its not obsolete yet: master regular expression pdf - Chapter 4. So according to that, Perl is based on NFA engine. But when i check below matching:

    $_ = "The first recorded efforts to reach Everest's summit were made b +y British mountaineers "; /summit|Everest|mountain/; print $&; #Result is "Everest"

    For NFA the engine will move the control over the RegEx, encounter with the alternation, the engine will check it in turn, so "summit" is checked first and matched. At this point overall match is achieved and the engine should stop but no. The result is most likely from the DFA engine as it will move the control over the target text. First the engine will look into the target string "The first ....", since it find the "Everest" first and the RegEx satisfies that -> Overall match achived! Even if the engine will choose the leftmost match. Then "summit" should be evaluated once? So I try to use a second approach:

    $_ = "The first recorded efforts to reach Everest's summit were made b +y British mountaineers "; /summit(?{print "11"})|Everest(?{print "22"})|mountain(?{print "33"})/ +; print $&; #22Everest

    The result shows that "summit" is not evaluated! Did i misunderstand something here about the "alteration" or about the RegEx engine? Or Perl is not NFA anymore but Hybrid NFA + DFA? Dear Monks, please enlighten me.

    Thanks in advance!

Deriving File name convention with Look-ahead and look-behind assertion
2 direct replies — Read more / Contribute
by Magnolia25
on Oct 19, 2016 at 10:23

    I am trying to build a regular expression for below files.

    HIGH-LEVEL Status file for process (can be in below formats)

    server_load_<ipaddress>.<ipaddress>.SKIP server_load_<ipaddress>.xyz_<ipaddress>.SKIP Example. server_load_IP100.69.82.21.IP100.69.82.81.SKIP server_load_IP100.69.82.21.ny_IP100.69.82.81.SKIP

    Detailed Status file for process (can be in below formats)

    server_load_<ipaddress>.<ipaddress>.xyx_ABC-2.SKIP server_load_<ipaddress>.ny_<ipaddress>.ABC_load.SKIP Example server_load_IP100.69.82.21.IP100.69.82.81.ny_AMER-2.SKIP server_load_IP100.69.82.21.ny_IP100.69.82.81.AMER_load.SKIP

    My regular expression SHOULD NOT match any name from HIGH-LEVEL Status file for process. only that will work for Detailed Status file for process.

    Tried using Look-ahead and look-behind concepts, but not able to get my best.

    #!/usr/bin/perl use strict; use warnings; while (<DATA>) { if (/^(?<!server_load_IP100.69.82.21\s).*.SKIP/) {print "Good Match "; +} else {"Bad Match ";} print; } __DATA__ server_load_IP100.69.82.21.IP100.69.82.81.SKIP server_load_IP100.69.82.21.IP100.69.82.81.ny_AMER-2.SKIP
    Output =============== Good Match server_load_IP100.69.82.21.IP100.69.82.81.SKIP ==> Ideall +y this is HIGH-LEVEL Status file for process should be "Bad Match " Good Match server_load_IP100.69.82.21.IP100.69.82.81.ny_AMER-2.SKIP

    Any thoughts or guidance please. Thanks.

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 making s'mores by the fire in the courtyard of the Monastery: (4)
    As of 2016-10-24 01:02 GMT
    Find Nodes?
      Voting Booth?
      How many different varieties (color, size, etc) of socks do you have in your sock drawer?

      Results (302 votes). Check out past polls.