http://www.perlmonks.org?node_id=479

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
Error when installing cpan's DBI module in Strawberry Perl
2 direct replies — Read more / Contribute
by underTheRadar
on Feb 22, 2019 at 22:54

    I'm trying to manually install the DBI module from CPAN using the command prompt on Windows 10.

    After I downloaded the zip file in CPAN, I extracted it to the C: directory. When I typed in C:>cpan Module::DBI in the command prompt, it shows this line of error:

    "Warning: Cannot install Module::DBI, don't know what it is"

    What am I doing wrong?

Importing constans and variables when "require"ing
4 direct replies — Read more / Contribute
by bliako
on Feb 22, 2019 at 16:48

    Good Monks warm salutations,

    I am trying to require a module and use a constant and a variable from it but I get an error and a warning respectively.

    This is the module:

    # XYZ.pm package XYZ; use strict; use warnings; require Exporter; our @ISA=qw/Exporter/; our @EXPORT_OK = qw/PI CONST/; our $PI = 3.14; use constant CONST => 10.10; 1;

    And this is how I require it:

    # example.pl use strict; use warnings; use lib '.'; require XYZ; print "PI=".$XYZ::PI."\n"; print "CONST=".XYZ::CONST."\n";

    I get an error:

    Bareword "XYZ::CONST" not allowed while "strict subs" in use at exampl +e.pl line 13. Execution of example.pl aborted due to compilation errors.

    Now, if I comment out the line using CONST then I get this warning:

    Name "XYZ::PI" used only once: possible typo at example.pl line 11. PI=3.14

    I have deliberately not used XYZ->import(qw/PI CONST/); as I prefer to use the full name of variables and subs from other modules. But it makes no difference as to the warnings and errors mentioned above.

    Can you please shed some light?

Moose in a module created with h2xs
1 direct reply — Read more / Contribute
by rpaskudniak
on Feb 22, 2019 at 14:38
    Hi again.

    (I was going to post on perlguru.com but some database issue seems to have gone wrong with that site. That aside..)

    Greetings again.

    2 years ago I wrote a module with utilities that invoke it. It all works well but it was not properly organized for CPAN. So the code is all there but I need to start fresh and transplant code from the older module as appropriate. However, I also want to use this as an opportunity to practice Moose and Function::Parameters and other trappings of "Modern Perl". And at the gate I have run into question on how to proceed. Here is the stub code that my h2xs invocation created; I have joined lines to keep it clear.

    package DBD::Informix::DBspaces; use 5.022001; use strict; use warnings; require Exporter; our @ISA = qw(Exporter); our %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = qw( ); our $VERSION = '0.01';

    (Yes, this is Informix oriented, for those who recognize the buzz words.) I'm planning three packages in here, with the "DBD::Informix::DBspaces" package referencing the other two. So now I have two questions:

    - Do I need to specify "use Moose" in every package I define in that .pm file? Or will the one "use Moose" in the first package suffice for the other packages as well. Or how about "use Moose" even before the first "package" directive?

    - All those "our" declarations and the "require", generated by h2xs: Do those need to be repeated in each package or is is sufficient to put them all up front, outside all the packages? I suspect that outside all packages will be fine but if it is not fine I will be greeted by a barrage of errors masking any syntax errors and other small stuff.

    So this misunderstood less-than-genius seeks the experience of those who have already played in these fields and humbly thanks thee. (What's plural for thee? :-)

User name character classes
2 direct replies — Read more / Contribute
by Zenzizenzizenzic
on Feb 22, 2019 at 14:04
    Is there a way to make a user defined character class, for instance, should I want to find words like "did" that are vowel, consonant, vowel, I could do this:
    print "CVC word found : $1\n" if (/\b([b-df-hj-np-tv-z][aeiouy][b-df-hj-np-tv-z])\b/i);
    But I'd prefer (esp. in more complicate examples) to do something like:
    print "CVC word found : $1\n" if (/\b([:consonant][:vowel][:consonant])\b/i);
    Thank you
Export and use different package in module
1 direct reply — Read more / Contribute
by chenhonkhonk
on Feb 22, 2019 at 13:42

    I have what sounds to me like a simple problem. I want to use a different 'package' in a module for brevity, and I want to export things in the normal package namespace. If I include the package My::foo; alone, my functions and variables export with @EXPORT_OK and our @EXPORT. Perfectly fine no problems. If I then put package barpackage; somewhere for making all the functions/variables not have barpackage::otherpackagestuff:: in front for the sub-package, all my exports are not found. I'm used to C just having extern or include'ing, and not needing to have tricks and hacks just so I can export things without typing 5x the characters for any value/function.

    I know it's "bad" programming to pollute a namespace, but it shouldn't be a broken kludge of the language to stop me from overlapping namespaces at all.

My server as intermediate FTP
1 direct reply — Read more / Contribute
by cristofayre
on Feb 22, 2019 at 10:05

    I've not started to develop this yet, but just thought I would see if it was technically possible.

    I know I can use PERL to read a photo file from a local SD card, and place it on my web hosting space. I fairly certain I can then use perl to OAUTH to another server, and upload said photo to their machine. However, as the photo file may be around 8mb, (and there could be 100 - 250 to process) I'm wondering if it would be THEORETICALLY possible to cut out the middle man. In otherwords, "pipe" the file I'm uploading directly to the OAUTH vendor's site. (I'm thinking an 8MB file read into shared server memory is going to be a bit of an overhead for the CPU)

    I'll admit, I've never used OAUTH and its username / tokens, and don't know if I would have to get a new token for each photo upload I do. That too is part of the learning curve.

Perl DBI escape reserved word in Ingres database
4 direct replies — Read more / Contribute
by jtech
on Feb 22, 2019 at 07:51

    Hi ladies and gentlemen

    Can you help me to scape a reserved double quoted column name from an Ingres database, please?

    The difference is the column name open is between double quotes, probably because it is a reserved word in the Ingres database. The SQL query works fine using double quotes or not, in the SQL terminal.

    The Perl code below works great:

    my $sql = "SELECT DISTINCT id FROM mytable WHERE active = ?"; my $ref = $dbh->selectall_arrayref($sql,undef,'yes'); @id = map{ $_->[0] } @$ref; die join (", ", @id), "\n";

    But, the Perl code below returns the error: "Argument "true" isn't numeric in subroutine entry at ./new.pl line 104."

    my $sql = "SELECT DISTINCT id FROM mytable WHERE open = ?"; my $ref = $dbh->selectall_arrayref($sql,undef,'true'); @id = map{ $_->[0] } @$ref; die join (", ", @id), "\n";

    So, I have escaped the quotes using the below chars without success:

    'open' '\open\' "open" "\open\" '\"open\"' \"'open'\" '"open"' "'open'" \'\"open\"\' \"\'open\'\"

    Also, it has been combined with q// and qq//

Creating a bin or script directory with h2xs
1 direct reply — Read more / Contribute
by rpaskudniak
on Feb 21, 2019 at 21:42
    (Edited about 45 minutes after initial submission)

    Greetings.

    It has been a long time since I had time on my hands to pursue more knowledge. Unfortunately, I have that time now. :-( That aside...

    Some years ago I posted a module to CPAN, along with a command-line utility that would use that module. As per Sam Tregar's sound advice I started with h2xs and created the usual hierarchy. But it did not include a directory for script files that are not part of the module. So here is the directory structure with incriminating names changed.

    drwxr-xr-x+ 1 RPaskudniak None 0 Nov 21 2013 blib drwxr-xr-x+ 1 RPaskudniak None 0 Nov 20 2013 lib -rw-r--r-- 1 RPaskudniak None 24074 Dec 11 2013 Makefile -rwxr-xr-x 1 RPaskudniak None 1297 Mar 3 2011 Makefile.PL -rwxr-xr-x 1 RPaskudniak None 172 Nov 20 2013 MANIFEST -rw-r--r-- 1 RPaskudniak None 568 Nov 20 2013 META.yml -rw-r--r-- 1 RPaskudniak None 500 Dec 11 2013 MYMETA.yml -rw-r--r-- 1 RPaskudniak None 0 Dec 11 2013 pm_to_blib -rwxr-xr-x 1 RPaskudniak None 15000 May 6 2012 README drwxr-xr-x+ 1 RPaskudniak None 0 Feb 5 2014 scripts <<<***>>> drwxr-xr-x+ 1 RPaskudniak None 0 Nov 20 2013 t

    Note that scripts directory: h2xs did not create that; I manually created that. And somehow, when I ran the final make install, the utility program ended up in the right place, /usr/local/bin.

    I have no recollection of how I got that to work. I likely asked someone but I just don't recall what I did right. I don't think I had to mess with the Makefile.

    So my bottom line question is:

    Is there an option to h2xs that makes allowances for a bin or scripts directory whence to place a stub for a Perl program related to the module? Or lacking such an option, is there an alternative to h2xs that does make such an allowance?

    This humble memory challenged peasant begs a memory kick-start to get restarted; I'm planning a module that will need at least 5 utilities that I can think of.

    This humble peasant (with some grand delusions) thanks y'all.

Confused by a couple of idioms
4 direct replies — Read more / Contribute
by nysus
on Feb 21, 2019 at 17:07

    Being Perlmonk's most clueless Vicar, I continually find new idioms that leave me baffled. I'm looking though this code. On line 33, I see:

    "o|c|out=s" => \my $csv

    What is the slash doing there before my?

    And on line 55:

    $csv and $xls = $csv;

    I've stared at this for a good 2 minutes and can't figure it out. I guess that's what keeps Perl so interesting. Thanks to the monks for your patience with me after all these years. :)

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

Using Cypher::CBC to encrypt fields in a file - Need Monk Help
2 direct replies — Read more / Contribute
by perlc1ph3r
on Feb 21, 2019 at 12:06

    I am running up against a wall and hitting it hard. I am needing to encrypt fields and not he entire file for data upload. I then need to use that encrypted field as a key in a system and then when extracted, I need to be able to decypt it. Below are sample lines from a fake text creator to illustrate my quandary."

    customerid registered_date first_name last_name address city state zip age primary_email
    689-08-2317 1518973472 Sascha Urian 85 Sunbrook Road San Antonio Texas 78250 55 surian0@archive.org
    201-25-0510 1515436776 Gilbertine Impy 3136 Thierer Trail Mc Keesport Pennsylvania 15134 49 gimpy1@edublogs.org
    616-54-5114 1518645090 Christabella Gunther 4 Autumn Leaf Junction Chicago Illinois 60619 26 cgunther2@nature.com

    I would need to read in the file, encrypt column 1 & 9, then output the file in the same format but with those values having encrypted values. I may be going down the wrong path here and really need a hash process but I have exhausted my working brain cells at this point and need some help. The end format would look like this below.

    customerid registered_date first_name last_name address city state zip age primary_email
    $#diDse*eES 1518973472 Sascha Urian 85 Sunbrook Road San Antonio Texas 78250 55 weSIDUsae34@#$%%#@@
    eSDE#24DSET 1515436776 Gilbertine Impy 3136 Thierer Trail Mc Keesport Pennsylvania 15134 49 sdies#@SERUSEeset
    OEDUse#@#$2 1518645090 Christabella Gunther 4 Autumn Leaf Junction Chicago Illinois 60619 26 mvxosef@#w32553S

    I have been trying to modify this base code

    #!/usr/bin/perl use strict; use Crypt::CBC; #unless (scalar @ARGV == 3) { # #die "Usage: $0 encrypt|decrypt|en|de \$mysecretkey \$file_to_den +crypt"; #} #my $type = shift @ARGV; #my $key = shift @ARGV; #my $file = shift @ARGV; my $type = "de"; my $key = "12345"; my $file = "C:\\perlinputfiletest\\fakedata.txt.encrypt"; die "The first ARGV should be one of de, en, encrypt, decrypt" if ($ty +pe !~ /^(en|de)(crypt)?$/); die "the file $file is not existence" unless (-f $file); my $DEBUG = 1; print "type is $type, key is $key, file is $file\n" if $DEBUG; my $cipher = Crypt::CBC->new( -key => $key, -cipher => 'Blowfish' ); local $/; open(FH, $file) or die $!; flock(FH, 2); my $data = <FH>; close(FH); my ($save_data, $save_file); if ($type =~ /^en(crypt)?$/) { $save_data = $cipher->encrypt($data); $save_file = $file . '.encrypt'; } else { $save_data = $cipher->decrypt($data); $save_file = $file . '.decrypt'; } open(FH, '>', $save_file) or die $!; print FH $save_data; close(FH); if (-e $save_file) { print "$type file $file to $save_file OK\n"; } else { print "failed without reason\n"; }


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.