Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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
Redefined import method and EXPORT not working
1 direct reply — Read more / Contribute
by Anonymous Monk
on Dec 22, 2014 at 01:13

    I redefined the import method of a module

    package M1; use parent qw(Exporter); our @EXPORT; sub func{print "Hello"} sub import{print "Imported";@EXPORT=qw(func)} 1;

    And EXPORT is not working

    perl -I. -le 'use M1;func()' Imported Undefined subroutine &main::func called at -e line 1.

    But when I don't redefine import function, EXPORT works fine

    package M1; use parent qw(Exporter); our @EXPORT; sub func{print "Hello"} #sub import{print "Imported";@EXPORT=qw(func)} 1;
    perl -I. -le 'use M1;func()' Hello
    How can I get both redefining import and EXPORT to work?
comma(or fat comma) in 'use' directive
3 direct replies — Read more / Contribute
by Anonymous Monk
on Dec 21, 2014 at 23:20

    Like this sentence:

    use Test::Simple tests => 1;

    Sometimes I see there is a fat comma on use directive Of course I would think they are functions if it looked like:

    use Foo::Bar qw(func1 func2);

    But in the first case, '1' cannot be a function name. So what is that '1'? Is it a parameter to the function 'tests' or a parameter to the module Test::Simple or what???

Seeking regexp @ARGV arrays Wisdom
4 direct replies — Read more / Contribute
by Anonymous Monk
on Dec 21, 2014 at 10:24

    Thank you in advance. I am writing a short program, a game, that tests your knowledge of GNU/Linux commands and programs (with a whatis entry). I want it to put like flash cards: present the name of the command, wait for the user, then print the description, wait for the user, ad infinitum. The code below shows the first attempt (SLATHERED WITH COMMENTS):

    #!/usr/bin/perl #bashflash #tests your memory of bash/gnu-linux commands and cli programs #USAGE: #cd to the directory containing the binarys of the programs #you want to test your knowledge of (DO NOT NEED TO BE ROOT) #pass perl bashflash -`whatis --wildcard *` # use strict; use warnings; #The following loop utilizes a shift-and-push loop #to shift the zeroth element of @ARGV out of the array, into a scalar, #then push the scalar back into the array as the final element. #effectively, this process lets one cycle through the output of #`whatis --wildcard *` indefinitely, *while separating the #command names from their descriptions, like flashcards.* #*In it's current implementation, @ARGV stores the output of `whatis` #word by word: such that elements are delineated by spaces. my $currentcard; #this variable holds onto strings shifted off of @ARG +V #, prints them, then pushes it's contents #back onto the end of ARGV while (1) { #infinite loop START $currentcard = shift; #shift first element #of @ARGV into $currentcard print $currentcard; push (@ARGV, $currentcard); #push $currentcard #(previously element 0) back onto #@ARGV as the last element <STDIN>; #Wait for user } #infinite loop END exit; #the script never gets here, #but I always explicitly exit on the end of my Perl. #and then an empty comment line #

    This program puts this to STDOUT:

    Note, each line after "-bash" had to be summoned by the return key, meaning that (as explained in the comments) @ARGV has stored the command line argument into new elements by word. (This is my current understanding, please correct me.)

    #1, the negative testing while

    #2, the while controlled by internal positive testing if

    Above: an attempt, two of many, to get the "flash card" output I desire. My plan is to instead push the shifted elements of @ARGV onto an Array in a regexp-conditional loop, to attempt to match the "- " in the last element of Array, if it does match "- ", then the code should exit the loop and print everything it collected up to and including the "- ", then pushing it's content back onto the end of @ARGV, and waiting for the return key press. Or the logical equivalent, test positive for iteration with the lack of "- ", and keep adding to Array until shift adds a "- ". As above.

    I have tried dozens of variations with positive and negative regular expressions, I even tried /.*/, but every time I try to group the output such that iterations of print are delimited by "- ", just I get blank lines.

    Am I using push and shift incorrectly?

    Prithee, great archons of perl, behelpeth me.

Loosing variable content after regular expression
3 direct replies — Read more / Contribute
by metty
on Dec 18, 2014 at 11:23


    I cannot figure out why my variable looses its content after applying a regular expression on it. The program is running in a mod_perl environment using Perl 5.14.2. The following code snippet is part of an object method. An interesting fact: sometimes its working, sometimes its not.

    if (defined($$l_Name_ref)) { print STDERR "1 $$l_Name_ref\n" if (defined($$l_Name_ref)); print STDERR "2 $l_Name_ref\n"; if ($$l_Name_ref =~ /^[a-zA-Z]\w+$/) { print STDERR "3 $l_Name_ref\n"; print STDERR "4 $$l_Name_ref\n"; ...

    Here is the output from the apache log files

    1 MMP_MODULES 2 SCALAR(0x7f80ab361968) 3 SCALAR(0x7f80ab361968) 4 MMP_MODULES 1 TMP_MODULE_HASH 2 SCALAR(0x7f80ad04ed68) 3 SCALAR(0x7f80ad04ed68)
    Use of uninitialized value in concatenation (.) or string at + line 1061.

    The error message indicates the line storing the print command with number 4.

    During the first run, referencing the value 'MMP_MODULES', the program is working fine. The debug code send to STDERR, which is passed to the Apache log files, prints the expected result. During its second run, using the value 'TMP_MODULE_HASH', the regular expression is validated fine, but the referenced value gets undefined. I printed the pointer value. Interesting enough, the pointer to the memory block does not get modified. Just the value gets lost. Any idea why?

    I can fix this by creating a local copy before applying the regular expression:

    if (defined($$l_Name_ref)) { my $l_Name = $$l_Name_ref; if ($l_Name =~ /^[a-zA-Z]\w+$/) { ...

    Using this code, it is always working. As the code is in a central part of my program, which is executed hundreds of times during a web page call, I would like to avoid copying a value without a need.

    How can a regular expression check modify the content of a variable?

How to install perl in custom directory on solaris.
3 direct replies — Read more / Contribute
by Ankur_kuls
on Dec 18, 2014 at 00:30

    Hi, I am facing many issues while installing perl modules with the default perl which comes with sun OS 10. Now I want to install perl in any custom directory. what I know and did is as below

    downloaded the latest perl in any directory (say /home/abc) unzipped and untar it. cd /home/abc/perl-5.20.1 ln -s /usr/sfw/bin/gcc /usr/bin/gcc ln -s /usr/ccs/bin/make /usr/bin/make ./Configure prefix=/customperl/cac make make test make install

    all three make command didn't show any failure but still I didn't get my perl installed in custom /customperl/cac directory. Could any one please tell me where I am wrong or provide me the complete steps to install this...thanks.

Occasional Read Timeout with Mech
3 direct replies — Read more / Contribute
by pirkil
on Dec 17, 2014 at 06:17

    Dear monks, my script has to fetch some data from web, the mechanism looks like this:

    for my $try (1 .. 5) { debug("fetching HTML source: try $try of 5\n") if $debug; my $mech = WWW::Mechanize->new( autocheck => 0, ssl_opts => { verify_hostname => 0, SSL_version => 'TLSv1', }, timeout => 60, ); $mech->proxy('https', $args_hr->{proxy}); # Try::Tiny try { $mech->get( $url ); } catch { $err .= $_ if $_; }; my $text = $mech->content; $err .= "Can't fetch HTML source from $url!\n" if !$mech->s +uccess(); ... sleep 30; # before next try - if download was not succ. }
    I run this script on a server (with cron job). The URL is always the same. Sometimes I got en error, variable $text contains: read timeout at /usr/local/share/perl5/Net/HTTP/ line 268. Other runs are OK, so I am not sure what the problem is and how to avoid it. Thanks for help!
Unaltered child return code on Windows
1 direct reply — Read more / Contribute
by salva
on Dec 16, 2014 at 11:48
    I am wrapping an external executable on Windows that can exit with codes bigger than 255. The issue I am facing is that Perl $? seems to be limited to the combination of one byte for the signal and another byte for the errorlevel. For instance, when the program exits with code 1000 (0x3E8), $? becomes 59392 (0xE800).

    Perl has also the variable ${^CHILD_ERROR_NATIVE}, but it seems that on Windows it gets the same value as $?.

    Does anybody known how to get the real exit code without reverting to using an external module as Win32::Process to start and control the slave processes? (on the other hand, I am already using Win32::API, so solutions using it would be acceptable).

getopt::long treating option as an argument
2 direct replies — Read more / Contribute
by harpreetsammi
on Dec 16, 2014 at 10:43

    Hi Experts,

    I am running a perl script which uses GetOpt::Long -name mike -long

    where, -name option ("name=s") needs a mandatory string argument

    -long is optional flag if set, will provide more details of the user named mike

    If i miss the user name while running as below then it does not error out - -name -long

    In this case, its treating -long as user name and DOES NOT error out. I want that in above case it should error out and say "Option name requires an argument".

    FYI. in the below scenario it error out as expected if i use -name at the end of command line - -long -name Option name requires an argument

    Can someone please help how can i error out correctly in the below scenerio which is missing name mike - -name -long

    I don't want to use "=" as - -name= -long

    Thanks and Regards,

Why doesn't this print when i omit the newline?
5 direct replies — Read more / Contribute
by karlgoethebier
on Dec 16, 2014 at 08:07

    Hi all,

    i,m i'm banging my head against the wall with this:

    use strict; use warnings; use IO::All; # v5.18.2 my $io = io $0; while ( my ( $index, $value ) = each @$io ) { print qq($index\t$value); print qq($index\t$value\n); } __END__

    Only the second print works.

    I wonder why.

    Thank you very much for any hint and best regards,


    «The Crux of the Biscuit is the Apostrophe»

New Meditations
Authentication with U2F Two-factor keys
No replies — Read more | Post response
by cavac
on Dec 19, 2014 at 07:43

    I just uploaded the first Alpha version of Crypt::U2F, which allows you to handle the server side cryptography of the FIDO alliance's Universal 2nd factor authentication method. See also here.

    This is the same one used by Google services and fully supported in Google Chrome.

    Internally, Crypt::U2F requires Yubico's libu2f-server library installed on your system. I implemented this in two Perl modules: Crypt::U2F is the low level module (sand subject to change), that let's you play around with the underlying library. Crypt::U2F::Simple is the one you should use in most cases.

    Let's have a look into the two examples provided with the tarball. For this to work, you need to install libu2f-server and also install libu2f-host, because we need the u2f-host binary to talk to the actual USB dongle. (I'm currently in the process of making a Perl module for libu2f-host as well, but this will only finish after the hollidays.)

    The whole thing is a two part process: First you have register a new key once, then you can authenticate as often as you like. Each part (registering, authentication) itself is a two-part process as well, first you generate a challenge and send it to the client, then you have to validate the response.

    Ok, let's start with registering a key. For this example, we pass around files to and from u2f-host and also save the registered keyHandle and public key into files as well. In a real world scenario, you will probably use HTTP and Javascript to communicate with the key and save keyHandle and the public key into a database. Here's the code:

    The reason we use Base64 is simple, yet annoying: Everything except the public key is either some sort of text or even ASCII JSON. The public key on the other hand is a binary blob. It's just a matter of convenience to turn it into Base64, because that we it works in textfiles and text columns in databases as well. It don't convert directly in the library, because that might make it problematic to cooperate with other implementations of U2F authentications that also use the original C library (which delivers a binary blob), including the u2f-server example binary that comes with it.

    All of the calls to Crypt::U2F::Simple may fail for one reason or another (including new() and DESTROY()), so make sure you check all the return values!

    Let's tackle the authentication part. We'll use the keyHandle.dat and publicKey.dat generated in the previous step:

    As you can see, the process is quite similar: We load the keyHandle.dat and publicKey.dat (the second one we decode_base64()) and initialize Crypt::U2F::Simple with it. Then we generate a challenge and verify it.

    If you want to make sure the verification step actually works, you can comment out the call can try to fuss the result of u2fhost in authReply.dat. Or just comment out the call to u2fhost after you you did one successfull authentication, this one should give you a u2fs_authentication_verify (-6): Challenge error.

    Limitations and Bugs: Currently (Version 0.10), each Challenge/Verify combo has to run in the same instance of the module. I'm still working on finding out how to fix that. Also, sometimes the USB keyfob seems to be in a strange state after plugging in, returning wrongly calculated authentication replies (at least mine does). Unplugging and replugging solves that problem.

    "For me, programming in Perl is like my cooking. The result may not always taste nice, but it's quick, painless and it get's food on the table."
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: (12)
As of 2014-12-22 17:00 GMT
Find Nodes?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?

    Results (121 votes), past polls