Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
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
using File::Find to find recently-installed modules
2 direct replies — Read more / Contribute
by Datz_cozee75
on Jun 24, 2016 at 01:11

    Hello Monks,

    I'm working through the exercises in the alpaca book, to wit where the castaways have specified a local::lib and now seek to write a script that gives useful time data associated with these files. My needs are not exactly what the book calls for in any one exercise but range over several, so I used File::Find for the first time instead of relying on windows explorer to tell me where and what things are. (Uggh.)

    In the last couple weeks, I've torn out and replaced an overgrown perl install with this machine, so I wanted to pay some attention to make this install a bit more robust, and consequently have strawberry perl. I had a false start on local::lib, which I hope won't matter in the scheme of things, which I only state so that no one could say "why didn't you tell us you had a screwy, abortive, previous attempt at creating a local::lib?" It may matter, may not. That's part of what I'm learning. Here's what I have now:

    use strict; use warnings; use utf8; use 5.014; use File::Find; my @directories_to_search = ('.', 'C:\Users\Fred\Desktop'); my @files; find( \&find_module, @directories_to_search ); sub find_module { if ( $_ =~ m/.pm$/ ) { my $name = $File::Find::name; push( @files, $name ); } } for my $file (@files) { my $mtime = (stat $file)[9]; # mtime via slice my $when = localtime $mtime; print "$when: $file\n"; }

    Output:

    ... Thu Apr 30 10:24:40 2015: ./alpaca/template_stuff/html1.pm Sun Jun 5 14:32:45 2016: ./alpaca/template_stuff/html2.pm Sun Jun 5 14:08:18 2016: ./alpaca/template_stuff/html3.pm Thu Apr 30 10:24:40 2015: ./alpaca/template_stuff/utils1.pm Wed Jun 9 08:59:19 2010: ./perl5/lib/perl5/HTML/ElementGlob.pm Wed Jun 9 08:24:01 2010: ./perl5/lib/perl5/HTML/ElementRaw.pm Wed Jun 9 13:56:58 2010: ./perl5/lib/perl5/HTML/ElementSuper.pm Wed Jun 9 16:17:31 2010: ./perl5/lib/perl5/HTML/ElementTable.pm Thu Feb 15 07:35:15 2007: ./perl5/lib/perl5/HTML/Extract.pm Thu May 21 09:22:21 2015: ./perl5/lib/perl5/HTML/TableExtract.pm Mon Nov 1 07:04:16 2010: ./perl5/lib/perl5/Prompt/Timeout.pm Wed Jun 24 04:07:26 2009: ./perl5/lib/perl5/WWW/Mechanize/GZip.pm

    It appears that cpan modules are getting stored in a place that seems to have 'perl5' in it twice. Hope that's okay. Scripts are able to find these so far. Otherwise, I see that, unlike the professor, mtime is not what I'm looking for, as the time I'm interested in is when they got saved to this locale. Is there a perl way to make sure I own these? (The dot is 'Documents', so I would think so indeed.)

    My quest for a "born-on" timestamp continued with reading up on stat, so I worked up versions of the same material using atime and ctime. I don't think I see the difference between the two, so I wonder which one works better as a "born-on" date in this context:

    use strict; use warnings; use utf8; use 5.014; use File::Find; my @directories_to_search = ('.', 'C:\Users\Fred\Desktop'); my @files; find( \&find_module, @directories_to_search ); my @time =localtime; say "local time is @time"; sub find_module { if ( $_ =~ m/.pm$/ ) { my $name = $File::Find::name; push( @files, $name ); } } for my $file (@files) { my $atime = (stat $file)[8]; # atime via slice my $when = localtime $atime; print "$when: $file\n"; }

    atime output:

    local time is 13 44 19 23 5 116 4 174 1 Mon Jun 13 19:12:19 2016: ./alpaca/template_stuff/html1.pm Mon Jun 13 19:12:19 2016: ./alpaca/template_stuff/html2.pm Mon Jun 13 19:12:19 2016: ./alpaca/template_stuff/html3.pm Mon Jun 13 19:12:19 2016: ./alpaca/template_stuff/utils1.pm ... Wed Jun 22 22:09:13 2016: ./perl5/lib/perl5/HTML/ElementGlob.pm Wed Jun 22 22:09:13 2016: ./perl5/lib/perl5/HTML/ElementRaw.pm Wed Jun 22 22:09:13 2016: ./perl5/lib/perl5/HTML/ElementSuper.pm Wed Jun 22 22:09:13 2016: ./perl5/lib/perl5/HTML/ElementTable.pm Wed Jun 22 22:07:17 2016: ./perl5/lib/perl5/HTML/Extract.pm Wed Jun 22 22:08:24 2016: ./perl5/lib/perl5/HTML/TableExtract.pm Wed Jun 22 22:12:21 2016: ./perl5/lib/perl5/Prompt/Timeout.pm Wed Jun 22 22:06:40 2016: ./perl5/lib/perl5/WWW/Mechanize/GZip.pm

    This has the useful information I need, and seems the same on inspection as the same script with the change of

     my $ctime = (stat $file)[10];  

    My intent is to make comparisons of time values part of the search criteria. I found many of these values astonishing and unexpected compared to the reading. Again my final question for moving forward is whether atime or ctime would serve as a better born-on date. Thank you for your comment.

Problem with using file handle in hash
2 direct replies — Read more / Contribute
by sophate
on Jun 24, 2016 at 00:23

    Hi, I am having problem with using a file handle in a hash. For example:

    ############################### my %FileHandles; my $Dir = '/tmp'; my $FileName = 'test'; open $FileHandles{$Dir}{$FileName}, "$Dir/$FileName" or die "Cannot op +en $Dir/$FileName: $!"; while (<$FileHandles{$Dir}{$FileName}>) { print $_; } $FileHandles{$Dir}{$FileName}->close; ###############################

    The script above doesnt' print the contents in the test file. I only get a line like "GLOB(0xdfa4440)" instead. Any idea?

Regex question
1 direct reply — Read more / Contribute
by DivKay
on Jun 23, 2016 at 19:36

    I am trying to do a pattern match and having issues.

    The Main string: this is my teststring. LoadStore.SGE.\sr1_addr_reg_tmp_reg[0]

    Regexp: LoadStore\.SGE\.\\.r._addr_reg_tmp_reg

    I am positive the regexp is correct but it still doesnt match.

    Also, I am getting an 'Unrecognized escape \s passed through' when I am passing my main string to a variable. The \s is not a space but a literal \s

    Appreciate the help.

unintentional conversion of signaling NaN to quiet NaN
2 direct replies — Read more / Contribute
by pryrt
on Jun 23, 2016 at 16:48

    Harkening back to my experiments to make an Expand.pm to interpret IEEE754 doubles, I found when making my tests that as I converted a signaling NaN bit string into a double then back to the bit string, it became a quiet NaN. At first I thought I just had a bug in my conversion, so then I tried BrowserUK's code from Re^2: Exploring IEEE754 floating point bit patterns., along with a new function to convert it back.

    outputs

    ... ie, it fails on just the signaling NaN, appearing like a quiet NaN bitstring.

    I tried under an ancient CentOS 4.6 perl 5.8.5, and under a recent Strawberry 5.22.1 32bit, and get the same quieting of the sNaN. Am I doing something wrong, or does perl just automatically quiet signalling NaN values?

Modules in Use
4 direct replies — Read more / Contribute
by Anonymous Monk
on Jun 23, 2016 at 16:25
    Which modules are still being part of your major work and new developments?
RESTful Services
2 direct replies — Read more / Contribute
by LarryDMoss
on Jun 23, 2016 at 15:26

    Taking our web to the next level. Need to create RESTful web services to work with Backbone. I have found documentation on how to consume services, but not to create them. Does anyone have a map to the treasure?

Why do i get an extra (duplicate) array element?-
4 direct replies — Read more / Contribute
by rickman1
on Jun 23, 2016 at 14:02

    I am trying to populate an array with dollar amounts from a text file via a while loop. I read them in line by line using substr. When I print out the array contents the last amount is displayed twice:

    my $row; my $val; while ($row = <IN>) { $val = substr($row,0,1); @tranAmount = substr($row,68,17); foreach (@tranAmount) { print "$_\n"; } }

    Output:
    10.000
    10.000
    10.000
    -30.000
    -30.000 <--why is this happening? there are only 4 values within text file(10, 10, 10 and -30)

    My goal is to somehow stuff all neg values within one array and pos values within another, so i can then do math with them, but i am running into this from the get-go, one hurdle at a time i guess. Thank you.

Mocking a method defined in a Moo Role
2 direct replies — Read more / Contribute
by PopeFelix
on Jun 23, 2016 at 13:44

    I have a method, let's call it "foo" defined in a Moo Role.

    package 'MyRole'; use Moo::Role; sub foo { return 'blah'; }

    In a consuming class, I have some behavior implemented in an "around" modifier:

    package MyClass; use Moo; with 'MyRole'; around foo => sub { my ($self, $orig) = @_; if ($self->$orig eq 'baz') { return 'bak'; } return $self->$orig; }

    I would like to test this behavior, but I can't figure out how to mock MyRole::call. If I mock MyClass::call, the around() modifier isn't called. How do I test this around() behavior?

    Here's what I'm trying:

    use MyClass; use Test::Most; use Test::MockModule; my $mock = Test::MockModule->new('MyRole'); $mock->mock('foo' => sub { return 'baz' }); my $obj = MyClass->new; # Does not work is $obj->foo, 'bak', 'Foo is what it oughtta be';
Escaping special characters in Password prompt to be passed onto SAP-CRM
3 direct replies — Read more / Contribute
by rajrev-snps
on Jun 23, 2016 at 12:09

    I'd like to parse and escape any special characters in the Password and pass the literal text to a SAP-CRM system for logging-in the user. Of course, I'm trying to achieve this in Perl.

    Here is the regular expression that I'm using

    /[\~]+|[\!]+|[\@]+|[\#]+|[\%]+|[\&]+|[\-]+|[\_]+|[\:]+|[\;]+|[\']+|[\"]+|[\<]+|[\>]+|[\,]+|[\.]+|[\\]+|[\+]+|[\*]+|[\?]+|[\^]+|[\$]+|[\{]+|[\}]+|[\(]+|[\)]+|[\|]+|[\/]+|[\]]+|[\[]+|[\t]+/g

    Almost every character gets escaped except for the "$" character

    Please advise?

Open() : Not reading all lines in my text file
4 direct replies — Read more / Contribute
by Anusha_26
on Jun 23, 2016 at 06:10

    I have a text file(like posted below),

    anusha ramegowda <perl programming1234> "23rd June 2016" #Thursday, 11:05am# <perl programming5678> problem encountered

    I'm trying to read the contents of the text file(titlefile) using

     open (my $fp, "<", $titlefile) or die "Can't open <$titlefile: $!";  

    It reads all contents except the ones within <chevrons> for e.g. I don't read <perl programming1234> and <perl programming5678>.

    I want to read the string within chevrons to able to distinguish between various values. Can anybody please help?


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!
  • 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?
    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 rifling through the Monastery: (3)
    As of 2016-06-25 04:51 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My preferred method of making French fries (chips) is in a ...











      Results (323 votes). Check out past polls.