Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

The Monastery Gates

( #131=superdoc: print w/ replies, xml ) Need Help??

Donations gladly accepted

If you're new here please read PerlMonks FAQ
and Create a new user.

New Questions
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
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.


[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 ?

Why is this comparison failing?
3 direct replies — Read more / Contribute
by johnrcomeau
on Oct 08, 2015 at 14:29
    My code is doing something I really can't understand. I'm performing some linear interpolation, and test the result against the number 75. I can get two different results depending on where the &adjust_hue subroutine is called. Here's the code
    use strict; use constant HUE_RANGE => 255; my @HUES = ( 0, 25, 50, 75, 120, 168, 195, 240, + 268, 315); # 0.0 0.1 0.2 0.3 0.4 0.5 + 0.6 0.7 0.8 0.9 1.0 &adjust_hue(0.3); my @hues = (0 .. 4); $_ *= 0.1 for @hues; for (@hues) { &adjust_hue($_); } sub adjust_hue { my ($scale) = @_; my @hues = @HUES; push @hues, HUE_RANGE unless $hues[-1] == HUE_RANGE; # warn 'hues ', Dumper \@hues; # warn "n hues ", (scalar @hues), "\n"; my $n_unique_hues = -1 + scalar @hues; my $scaled_index = $scale*$n_unique_hues; my $index = int $scaled_index; my $adjusted_hue = $hues[$index] + ($scaled_index - $index)*($hues +[$index + 1] - $hues[$index]); # $adjusted_hue = eval $adjusted_hue; # This shouldn't be necessary +. Somehow this sub was producing strings instead of numbers. Later, t +his strings would fail comparisons. if ($adjusted_hue == 75) { warn " adjusted_hue ($adjusted_hue) equals 75\n"; } else { warn " adjusted_hue ($adjusted_hue) does not equal 75\n"; } warn "scale $scale, scaled_index $scaled_index, index $index, adju +sted_hue $adjusted_hue\n\n"; $adjusted_hue; }
    The output is the following for me:
    adjusted_hue (75) equals 75 scale 0.3, scaled_index 3, index 3, adjusted_hue 75 adjusted_hue (0) does not equal 75 scale 0, scaled_index 0, index 0, adjusted_hue 0 adjusted_hue (25) does not equal 75 scale 0.1, scaled_index 1, index 1, adjusted_hue 25 adjusted_hue (50) does not equal 75 scale 0.2, scaled_index 2, index 2, adjusted_hue 50 adjusted_hue (75) does not equal 75 scale 0.3, scaled_index 3, index 3, adjusted_hue 75 adjusted_hue (120) does not equal 75 scale 0.4, scaled_index 4, index 4, adjusted_hue 120
    I really don't understand why the comparison to 75 is true in one case with the parameter 0.3 and false also with the parameter 0.3 Thanks, John
Help tracking changes in array
4 direct replies — Read more / Contribute
by reebee3
on Oct 08, 2015 at 13:52

    Hello! I am trying to write a script that detects SNPs and their position in DNA sequences. Simply put, A SNP is when there is a change in A, T, C, or G between sequences, therefore...

    A could change to T, C, to G

    T could change to C, G, or A

    G could change to C, T or A

    C could change to G, T or A

    For Example:

    Sequence 1: ATGGAT

    Sequence 2: ACGGAG

    There are 2 SNPs here.

    SNP 1 is the T --> C is position 2 of the sequence. SNP 2 is the T -->G in position 6 of the sequence.

    Criteria for the script:

    Only 2 sequences can be entered on the command line The 2 sequences must be equal length Find the SNP and the position of the SNP (starting from position 1 not 0) Print out data- for example using the sample above

    Pos 2: T => C

    Pos 6: T =>G

    Found 2 SNPs.

    This is what I have so far, but I am not sure how to proceed...

    #!/usr/bin/env perl # file: use strict; use warnings; use autodie; my $number_seqs = scalar @ARGV; if($number_seqs != 2){ print "Please provide two sequences \n"; } for (my $i=0; $i<$number_seqs; $i++){ if(length($ARGV[$i]) != length($ARGV[0])){ print "Please ensure the sequences are the same length \n"; } } my $var = $ARGV[0]; print $var,"\n"; my $var2 = my $var[0]; print $var2,"\n";
When the only tool you have is a screwdriver...
4 direct replies — Read more / Contribute
by ExReg
on Oct 08, 2015 at 11:10

    I am trying to extend a tool that looks at files, searching for certain patterns. The tool is in perl and makes copious use of regular expressions. I came across a new pattern that has me scratching my head. I am trying to match a certain expression e that can have more than one definition ( shown in much simplified form ):

    1. e =~ u 2. e =~ uae 3. e =~ edefe 4. e =~ ebe 5. e =~ ec

    As you can see, all but the first are recursive matches. I could write the above as

    e =~ u|uae|edefe|ebe|ec

    Here is what I have so far: if I had only 1 and 2 from above, I could write the combination of 1 and 2 as

    e =~ u(?:au)*

    Likewise, for 1 and 3, I could write

    e =~ udufu

    For 1 and 4, I would have

    e =~ ubu

    and for 1 and 5, I would have

    e =~ uc

    To go further, I would have to combine more than two together, and I think it could get horrifyingly complex. Is there a way to tackle a problem like this with dynamic regexes, or is there a method for substituting the above together?

    I have perl 5.6, so I am slightly limited on what I can do. I do not know dynamic regexes well enough to frame the above with those. I cannot use CPAN or anything other than the screwdriver I have.

how to remove empty pipe delimiters in a line which doesn't contain any data
8 direct replies — Read more / Contribute
by rpinnam
on Oct 08, 2015 at 10:04
    I have a input pipe delimiter file as:

    IFB Northpole||| Alaska||| 907-555-5555

    Walmart||| Fairbanks||| Alaska

    Chicken||| Anchorage||| Alaska||| 907-555-5555

    Beef||| Somewhere|||||Over the Rainbow|||907-555-5555

    I want my output to be :

    IFB Northpole| Alaska| 907-555-5555|

    Walmart| Fairbanks| Alaska

    Chicken| Anchorage| Alaska| 907-555-5555

    Beef| Somewhere|Over the Rainbow|907-555-5555

    Here is my code :
    #!/usr/bin/perl @FILES = glob("*.txt"); foreach my $file (@FILES) { open my $fh, '<', $file; (my $fileName = $file) =~ s/\.[^.]+$//; open(my $output, '>', $fileName.".csv") or die "Could not open fil +e '$fileName' $!"; my @category; my @Detail; print $output ""; while (my $line = <$fh>) { chomp $line; @tokens = split /\|/, $line; chomp(@tokens); $objectName=$tokens[0]; if($objectName ne ""){ my @objectFields; $size = scalar(@tokens); @tokens = @tokens[1..$size]; foreach my $token (@tokens){ $token =~ s/\r|\n//g; push @objectFields,$token; } if($objectName eq "IFB Northpole"){ @Detail=@objectFields; }elsif($objectName eq "Walmart"){ @category=@objectFields; }elsif($objectName eq "Chicken"){ @category=@objectFields; }elsif($objectName eq "Beef"){ my @item = (@Detail,@category,@objectFields); print $output join("|", @item,@removeemptylines)."\n"; } } } close $output; close $fh; }
    How can i achieve this in perl? Any help would be appreciated.
minicpan mirror in windows network
1 direct reply — Read more / Contribute
by porcelet
on Oct 08, 2015 at 07:43

    Hello everyone, I can't find the solution to a problem. I downloaded perl strawberry portable and ran minicpan to get my local copy of one miror (no problem, it works great). Now I want to make it available to people that have access to a shared folder on a VM on the network

    The constraint is that we are limited to windows as OS and I can't configure cpan correctly to use that mirror. Let's assume I put the miror copy in \\a_server\Shared Internal\porcelet\my_cpan_mirror

    Then I went in the cpan config file to edit 'urllist' I have the following line 'urllist' => [q\\\\a_server\\Shared Internal\\porcelet\\my_cpan_mirror] cpan tries to fetch with LWP but the address looks starts by the url I specified and it's then followed by /authors/01mailrc.txt.gz I have the same issue for packages details and modlist. It indicates that the url must be absolute....did I miss something? I'd like to use unix environnement, I know it's easier...but You can't always do what you want ;)


Slicing a hash reference from a collection of hash references within a hash reference
4 direct replies — Read more / Contribute
by nysus
on Oct 07, 2015 at 11:21
    This is an embarrassingly newbie question, but I have a mental block when it comes to references. Given the following simple data structure, how do I get a slice of the name data?
    $household = { 23 => { last => 'Smith', first => 'Mary' }, 22 => { last => 'Jones', first => 'Bob' } };
    Here's my newbie code that works:
    my $key = 23; my $person = $household->{$key}; my %name = %$person; my @slice = @name{'first', 'last'};
    How do I turn this into a one liner?


    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon";
    $nysus = $PM . $MCF;
    Click here if you love Perl Monks

[Moose] extends(...) throws "Subroutine redefined" warnings
3 direct replies — Read more / Contribute
by muba
on Oct 07, 2015 at 08:16

    I have a superclass (MyClass), which creates instances of a subclass on demand. MyClass implements a custom method "get_subclass_instance" for this. The subclass (SubClass) extends 'MyClass';, however, this generates a "Subroutine get_subclass_instance redefined" warning.

    Minimal example:

    # package MyClass; use SubClass; use Moose; sub get_subclass_object { return SubClass->new; } 1;
    # package SubClass; use Moose; extends 'MyClass'; 1;
    $ perl -MMyClass -e 0 Subroutine get_subclass_object redefined at line 5. $

    I understand that extends(...) tries to load the class(es) passed to it. Consequently, if I remove the Extends 'MyClass' line from, the warning disappears.
    But I don't want to remove that line, because SubClass does extend MyClass!

    Of course, I could simply ignore the warning, but that doesn't feel clean to me. So what am I doing wrong, and how can I have MyClass load and instantiate SubClass, while SubClass explicitly extends MyClass?

Dynamically create the hash reference
2 direct replies — Read more / Contribute
by k_manimuthu
on Oct 07, 2015 at 06:16

    Hi All,

    I am trying to create dynamic references in a recursion function. Below I place code which I try, and it return '{}' only. How to get the initial level reference in else part?

    use Data::Dumper; sub insert { my ($ref, $head, @tail) = @_; return unless ( @tail ); if ( @tail and $head ne 'break') { insert( \%{$ref->{$head}}, @tail +) } else { # Here $ref gives '{}' value only. # How to get values of previous head values (ie. f1,f2,f3) print "\n==>", Dumper $ref; # More commands . . . #insert( \%{$ref->{f1}}, @tail ) } } my %hash; while (<DATA>) { chomp and insert \%hash, split( '/', $_ ) ; } print "\nDump\n", Dumper %hash; __DATA__ f1/f2/f3/f4/break/f1/f2/f5/break/f1/f2/f6
New Monk Discussion
How can one find his previous preview page (uncreated)?
3 direct replies — Read more / Contribute
by u65
on Oct 03, 2015 at 11:48

    Is there a procedure for finding an uncreated preview node?

    A couple of times now I've started a post but got interrupted before I pushed "create" and so far have found no reliable way to easily find what I started. If I don't go too far astray, I can keep pushing the back arrow in my browser and find it, but if I log off or otherwise close the page I can't always find it again. I have not been able to find much searching about preview here except the threads on wanting a preview capability for updates.

    Note that when working on longer posts I've kept text in an offline file to edit and copy from, but that seems to be a pain for relatively small posts, but I guess I could make that a habit.


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 perusing the Monastery: (1)
As of 2015-10-10 08:23 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (255 votes), past polls