Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Cool Uses for Perl

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

This section is the place to post your general code offerings -- everything from one-liners to full-blown frameworks and apps.

New stable MCE 1.842 and MCE::Shared 1.842 releases
No replies — Read more | Post response
by marioroy
on Jul 21, 2019 at 23:13

    Update: Added results for Parallel::ForkManager 2.02. The results mentioned in the POD documentation ran slower due to an unaware background job at the time. I reran again on all 5 platforms.

    Dear fellow Monks,

    I am pleased to annouce MCE 1.842 and MCE::Shared 1.842 (both stable). MCE now includes MCE::Channel and MCE::Child recently.

    This weekend, added Parallel::ForkManager-like demonstration to the POD section in MCE::Child and MCE::Hobo. The results were captured on a Macbook Pro (late 2013 model - 2.6 GHz ~ 3.6 GHz with Turbo Boost).

    To run, one may direct standard output to :nul or /dev/null depending on the platform. Or better yet, I commented out the print line in the on_finish handler. Unfortunately, Parallel::ForkManager 2.02 suffers from memory leaks on the Windows platform ($^O eq 'MSWin32') and the reason why slower than Cygwin.

    These days, there are other ways and instead have workers persist (i.e. pull items from a shared queue or channel) or perhaps via MCE's input and chunking capabilities.

    Regards, Mario

2d field of view, vision algorithm in grid (ray casting)
1 direct reply — Read more / Contribute
by Discipulus
on Jun 24, 2019 at 15:05
    Hello community!

    who knows me is aware I'm writing a game (engine?) and I asked here for the precious monks wisdom about circular area in a coordinates grid (AoA).

    But now I'm progressing and I discovererd that the above illuminate function is not enough. I found a big resource of Roguelike_Vision_Algorithms and I choosed the simplest one (second example) and I ported it to Perl ( field_of_view sub in the below code ).

    Impressed by this shiny exemple I wrapped into an interactive program to show my proof of concept:

    Chatting in the perl irc channel daxim also implemented a semi-transparency feature I'd like to add to my game. Here daxim's patch (and a big thank to him):

    have fun!


    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Listener Crossword #4321 solitaire
No replies — Read more | Post response
by GrandFather
on Jun 21, 2019 at 06:19

    The Times newspaper features an occasional "The Listener Crossword" which is in fact a numerically based logic puzzle in the form of a crossword. A friend of mine introduced me to the genera with #4321 which is a puzzle of two parts. The first part consists of populating the playing grid with hexadecimal numbers. The second part consist of using the populated grid to play a game of solitaire which, when played correctly on a correctly constructed grid ends up spelling out three words. There is a certain amount of trial and error involved in finding the solution!

    So to aid playing the game in the second part of the puzzle I wrote the following script. The gameGrid is configured for a partial solution of the game. A feature of the code is that you can "save" the game state at any point then paste the saved gameGrid in place of the current grid to explore possibilities from that point.

    As far as I can tell developing tools of this sort is all part of the solution domain for the puzzle. They are very much one off puzzles as each "crossword" is a puzzle of a completely different nature, so it is very unlikely that this tool will be useful for another "Crossword Puzzle". But it is a cool use for Perl!

    Note that a few shortcuts have been taken in the code. In particular global variables are used, which I usually avoid. The rendered grid is not very pretty and the layout generally is rough, but good enough for the task at hand.

    Play consists of clicking on a "peg" (piece to be moved) then an "empty" cell ("_") skipping over one intervening piece. The skipped piece is removed and added to the "skipped" string. Moves can be undone back to the starting state. For instructions beyond these you will need to find the original puzzle instructions and create the starting grid.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Tk ASCII Draw on Canvas
4 direct replies — Read more / Contribute
by Discipulus
on Jun 03, 2019 at 06:57
    Hello folks!,

    another Tk CUFP from my part! It is working and is also a draft for a bigger project (you all know what I'm working on nowadays.. ;)

    This is an ASCII drawing program expoiting the best I'm able to from Canvas and their precious feature: tags.

    I left some commented code because I have some question in case some Tk expert has answers:

    -1 about binding modifiers: I planned the draw action when <Button1-Motion> is on: ie. when button 1 is pressed (modifier) and the pointer is moveing over the Canvas. No luck. The program below now uses <Control-Motion> and perhaps is even better (less mouse->less pain)

    -2 I noticed some strange behaviours with some key: £ aka sterling in Tk world and ° aka degree if I use one of them (now commented in the code) I get back a multichar: uppercase A with caret above plus the degree symbol, for instance.

    -3 I wonder how can I implement an export coordinates range in my Canvas: something like: when SHIFT modifier is on and Motion is on too I should tag tiles with something like selceted use a different color for them and haveing two buttons for export corrdinates and select nothing Seems this the right way?

    PS now you can draw fancy things like:


    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Coordinate validator
2 direct replies — Read more / Contribute
by timpoiko
on May 17, 2019 at 05:41
    Dear monks. Some years ago I write small program which can validate coordinates (locations on the Earth) from user input. I didn't to restrict format of input and I wanted support English as well as Finnish.
Flat File Database (random access indexing)
2 direct replies — Read more / Contribute
by erichansen1836
on May 04, 2019 at 10:16

    UPDATE Monday June 3, 2019... I tested filling a 128-GIG flat file (containing 137_438_953_472 bytes) with 68,430 complete copies of the KJV Bible, at 2_008_451 bytes/Bible. I was able to get 8 times as many records (over 2 billion records) when switching to variable-length delimited records where I additionally compacted the data by 50% more by storing the text as random seeded Word-to-Code key mapped text using 1, 2, and 3 character codes (1/4 million of them to randomly choose from) generated solely from the character set {A-Z, a-z, 0-9}. Additionally, I used SHA-2 (256) digital signature generation on 1 copy of the Bible (2_008_451 bytes) to compare against real-time digital signature generation each time a different copy of the Bible is accessed through the DB GUI front-end. A sector/block of 31,102 records, 2_008_451 bytes (a complete copy of the Bible) is read in by one READ STATEMENT through the front-end, and a digital signature generated on-the-fly to compare against the original. If a different signature is produced, an auto-correct of the database occurs from a backup image for the effected sector/block of records only. This happens faster than you can blink.

    UPDATE Monday May 6, 2019... I tested a 114-GIG Flat File for random access of its records and it works on Windows 7 Home Premium O/S with a NTFS (NT File System) using 64-bit ActiveState ActivePerl for Windows version/release 5.26.1. and the File I/O syntax: sysopen, sysseek, $tell=sysseek(FH,0,1), sysread, syswrite, and close. The Flat File had 7500 copies of the Bible in it (7500 copies of the Bible * 31,102 verses/Bible copy, and at a fixed-length record size of 528 bytes, and over 233 million records). I used a single 2-GIG SDBM index file to index the records of the 114-GIG Flat File. I would have indexed a full 128-GIG Flat File, but I hit the 2-GIG limit with the single SDBM file used for indexing. A 2nd SDBM file would be needed to index the records in a 128-GIG Flat File from the 114-GIG mark to the 128-GIG mark i.e. Bible copy 7501 through 8369.

Find the array containing string and copy the file to other folder exactly
1 direct reply — Read more / Contribute
by SubaRavi
on Apr 05, 2019 at 02:19

    Find the array containing string and copy the file to other folder extactly

    use strict; use warnings; use File::Copy; use File::Find::Rule; use File::Find; use File::Basename; my $source_dir = "<Sorce Directory with specific files>"; my @doc=File::Find::Rule->file->name('*.xml')->in($source_dir); my $path="<parent folder>"; my @folders; find sub{ push @folders, "$File::Find::name" if (-d $File::Find::name); },$path; foreach my $fileformat (@doc){ my $baseFile=basename ($fileformat); $baseFile=~s/\.xml//isg;###handled xml for sake my @results = grep /$baseFile/, @folders; copy ($fileformat,@results); ###copied files to destination and ma +tched }
perl 2.01 on Cygwin
3 direct replies — Read more / Contribute
by rje
on Mar 26, 2019 at 19:02

    Even though I haven't programmed in C for a long time, I could get Perl2 compiled and running (a 387k binary!) in just a couple hours this afternoon on Cygwin.

    .Can I throw out the in-house malloc and other hand-rolled memory management code? What else can I do away with? How about all of the variant hardware #defines Larry had to make? Can't I simplify the code by aiming for one modern OS (Linux?) and Dockerizing it? And how about all that K&R C? Boy does that take me back... Can I shrink the binary by modernizing the code?

    $ perl2 -v This is perl 2, subversion 1 (v2.0.1) Copyright 1987-2019, Larry Wall Perl may be copied only under the terms of either the Artistic License or the GNU GPL ( Documentation for Perl should be found on this system via "man perl". Point your browser at, the Perl Home Page. Patch level: 0

    I needed GCC, make, and byacc (softlinked to 'yacc'). Then, I had to make a few edits:

    1. stab.c: commented out extern errno and replaced it with: int errno;

    2. perl.h: commented out the #ifdef that declared sprintf().

    3. perl.h: commented out the declaration of times().

    4. perly.c: I changed the -v message to look more Perl-like.

    I might have made two earlier edits, but they were along the same lines of removing conflicting or redundant declarations.

    And now, as a reward, I've got perl 2 running on Cygwin on my laptop! I have to say, it was worth the effort!

    Onward to hack!

    -rwxrwxr-x+ 1 rje None 387058 Mar 26 17:52 perl2.exe
Multiplication digit persistence
4 direct replies — Read more / Contribute
by tobyink
on Mar 21, 2019 at 09:17

    Try to find a number that takes more than eleven steps.

    use v5.10; use strict; use warnings; use List::Util qw(product); sub per { my ($n) = @_; return if $n < 10; my $p = product split //, $n; return $p, per($p); } my @steps = per 277777788888899; my $steps = @steps; say "$steps steps"; say for @steps;
Conversions of SI Units
1 direct reply — Read more / Contribute
by choroba
on Feb 16, 2019 at 16:57
    My son (11) has just learned SI units and their prefixes at school. They started just with metres and litres, only some of the prefixes (mili, centi, deci, hecto, kilo) and they don't know the floating point yet. Because of his broken arm, he missed several days at school and needed to practice. So, I've written a Tk application for him to practice. It takes one parameter, the number of formulas to generate.

    Feel free to localize it using the constants at the beginning. Adding the floating point left as an exercise for the reader.

    It's been a practice for me, too, because of Function::Parameters.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
IPC::Msg Fork queue
1 direct reply — Read more / Contribute
by trippledubs
on Feb 01, 2019 at 09:11

    IPC::Msg as a fork safe queue. Couple caveats that make it less cool, the by-default maximum queue size seems to be very small. Configurable though by changing /proc/sys/kernel/msgmnb which is the max size in bytes of an individual queue up to 2^31 - 1 on my system at least. Check with  ipcs -l . You also have to delete the queue when finished. Or not if you want to use it later. You can also find the same queue in different scripts with ftok, not shown. You can also see queue sizes / number of messages with ipcs command, add flags to pull off different messages from the queue. You don't always have to do FIFO, you can add different kinds of messages to the same queue, and you can block or not block if you want to have multiple queues being worked at the same time.

    For complex data structures you would need to serialize or pack / unpack which other cpan modules may already address and better. Message queues are already documented in perlipc, but a little sugar show show versatile it can be. Basically add or remove from the queue with any child process not worrying about blocking / synchronization issues, but you can accidentally destroy the queue before it is empty, or leave an empty or not-empty queue hanging around that you would have to delete with icprm command.

    #!/usr/bin/env perl use strict; use warnings; use feature 'say'; use IPC::SysV qw/IPC_PRIVATE S_IRUSR S_IWUSR IPC_NOWAIT/; use IPC::Msg; $|++; use constant MSG_BYTES => 1024; my $q = IPC::Msg->new(IPC_PRIVATE,S_IRUSR|S_IWUSR); sub quit { $q->remove; } $SIG{INT} = \&quit; sub enqueue { my $msg = shift; my $msg_type = shift // 1; # Used for message selection, see msgsn +d(2) $q->snd($msg_type,$msg,IPC_NOWAIT) || die $!; } sub dequeue { my $type = shift // 1; # See msgop(2) my $msg; $q->rcv($msg,MSG_BYTES,$type,IPC_NOWAIT); #if ($!) { warn "$$ - $!" } return $msg // undef; } sub dequeue_block { my $msg; my $type = shift // 1; # See msgop(2) $q->rcv($msg,MSG_BYTES,$type); #if ($!) { warn "$$ - $!" } return $msg // undef; } sub isEmpty { my $stat = $q->stat; return $stat->qnum == 0; } sub isPrime { my $num = shift; return 1 if ($num < 4); return 0 if ($num %2 == 0); for (my $i=3; $i <= sqrt($num); $i+=2) { return 0 if $num % $i == 0; } return 1; } my $n_workers = shift // die ; enqueue($_) for (1..2_000_000); my $parent = $$; for (my $i=1; $i<$n_workers && $$ == $parent; $i++) { fork // die; } while (my $data = dequeue()) { if (isPrime($data)) { print "Prime: $data\n"; } } END { if ($$ == $parent) { 1 until wait == -1; quit; } }
    • Num Procs: Time(seconds)
    • 2^0 : 55.171
    • 2^1 : 35.073
    • 2^2 : 21.455
    • 2^3 : 15.121
    • 2^4 : 12.380
    • 2^5 : 11.080

    Update 2019-02-04 - You can't wait on grandchildren, changed code to only fork from parent. Fixed spelling of empty.

Exploring Type::Tiny Part 7: Creating a Type Library with Type::Library
No replies — Read more | Post response
by tobyink
on Jan 28, 2019 at 09:52

    Type::Tiny is probably best known as a way of having Moose-like type constraints in Moo, but it can be used for so much more. This is the seventh in a series of posts showing other things you can use Type::Tiny for. This article along with the earlier ones in the series can be found on my blog and in the Cool Uses for Perl section of PerlMonks.

    For small projects, the type constraints in Types::Standard and other CPAN type libraries are probably enough to satisfy your needs. You can do things like:

    use Types::Common::Numeric qw(PositiveInt); has user_id => ( is => 'ro', isa => PositiveInt, );

    However for larger apps, say you need to check user identity numbers in an handful of places throughout your code and you use PositiveInt everywhere, then if you ever feel the need to change the constraint for them, you'll need to hunt through your code to look for every use of PositiveInt, make sure it's not being used for some other reason (like to check an age or a counter), and update it.

    So it is helpful to make your own application-specific type library. You can define your own UserId type constraint, and use that everywhere. If the format of your identifiers ever changes, you only need to change the definition of the type constraint.

    Moose-Like Syntax

    package MyApp::Types { use Type::Library -base, -declare => qw( UserId UserIdList ); use Type::Utils -all; BEGIN { extends qw( Types::Standard Types::Common::Numeric Types::Common::String ); }; declare UserId, as PositiveInt, where { $_ > 1000 }; declare UserIdList, as ArrayRef[UserId]; ...; }

    Using -base from Type::Library sets your package up as an exporter that inherits from Type::Library. Using -declare allows the type constraints there to be written as barewords in the rest of the package. Importing from Type::Utils gives you a bunch of helpful keywords that can be useful for defining your type constraints. (These keywords will be pretty familiar to people who have defined their own type constraints in Moose or MooseX::Types, but personally I prefer not to use them. I'll show you how to write this type library without the keywords from Type::Utils later.)

    The extends statement imports all the type constraints from the given type libraries, so all those types are added to this library. Putting it in a BEGIN block allows them to be written as barewords too.

    And then we define a couple of type constraints. Hopefully that part is pretty self-explanatory. The declare, as, and where keywords are some of the things exported by Type::Utils.

    Now your application code can just do:

       use MyApp::Types qw( UserId UserIdList HashRef NonEmptyStr );

    Your type library is also the perfect place to define any application-wide type coercions. For example:

    declare User, as InstanceOf['MyApp::User']; coerce User, from UserId, via { MyApp::Utils::find_user_by_id($_) }; coerce UserId, from User, via { $_->user_id };

    Bare Bones Syntax

    Although Type::Tiny supports this Moose-like syntax for defining type constraints, I personally find the Type::Utils DSL a little unnecessary. Here's another way you can write the same type library:

    package MyApp::Types { use Type::Library -base; use Type::Utils (); # don't import any keywords BEGIN { # Type::Utils is still the easiest way to do this part! Type::Utils::extends(qw( Types::Standard Types::Common::Numeric Types::Common::String )); }; my $userid = __PACKAGE__->add_type({ name => 'UserId', parent => PositiveInt, constraint => '$_ > 1000', }); my $user = __PACKAGE__->add_type({ name => 'User', parent => InstanceOf['MyApp::User'], }); $userid->coercion->add_type_coercions( $user => '$_->user_id' ); $user->coercion->add_type_coercions( $userid => 'MyApp::Utils::find_user_by_id($_)', ); __PACKAGE__->add_type({ name => 'UserIdList', parent => ArrayRef[$userid], coercion => 1, }); ...; __PACKAGE__->make_immutable; }

    Defining types this way exposes some parts of Type::Tiny which are subtly different from Moose. For example, coercions and contraints can be expressed as strings of Perl code. This allows Type::Tiny to optimize some of the Perl code it generates, avoiding the overhead of a function call. Notice also the coerce => 1 when defining UserIdList. This allows UserIdList to inherit ArrayRef's automatic ability to coerce one level deep.

    Calling make_immutable on the package allows Type::Coercion to further optimize coercions for all the types in the library and prevents code outside the library from changing the global coercions you've defined.

    # Imagine this is some code in a class... # use MyApp::Types qw( UserId Str ); # This will die because UserId is immutable now. UserId->coercion->add_type_coercions(Str, sub { ... }); # This will work, and only affect this one attribute. has user_id => ( is => 'ro', isa => UserId->plus_coercions(Str, sub { ... }), coerce => 1, );

    So this method of defining type libraries might look a little less clean, but it has advantages. And as I said, it's how I prefer to do things.

    Defining Utility Functions

    All Type::Library-based type libraries automatically inherit from Exporter::Tiny and can also be used to define utility functions. Just define a normal Perl sub in the package and add:

       our @EXPORT_OK = qw( my_function_name );

    I recommend using lower-case function names with underscores to separate words to make them visually distinct from camel-case type constraint names.

    To avoid creating a confusing package with a mishmash of unrelated functions, this feature should probably only be used to export functions which are vaguely related to types — validation functions, coercion functions, etc.

Exploring Type::Tiny Part 6: Some Interesting Type Libraries
No replies — Read more | Post response
by tobyink
on Jan 20, 2019 at 08:40

    Type::Tiny is probably best known as a way of having Moose-like type constraints in Moo, but it can be used for so much more. This is the sixth in a series of posts showing other things you can use Type::Tiny for. This article along with the earlier ones in the series can be found on my blog and in the Cool Uses for Perl section of PerlMonks.

    While Types::Standard provides all the type constraints Moose users will be familiar with (and a few more) there are other type libraries you can use instead of or as well as Types::Standard.


    If your attribute or parameter needs to accept a file or directory name, I'd strongly recommend using Types::Path::Tiny. It provides Path, File, and Dir types, plus Abs* versions of them which coerce given filenames into absolute paths. The Path::Tiny objects it coerces strings into provide a bunch of helpful methods for manipulating files.

    package MyApp::Config { use Moo; use Types::Path::Tiny qw(AbsFile); use JSON::MaybeXS qw(decode_json); has config_file => ( is => 'ro', isa => AbsFile->where(q{ $_->basename =~ q/\.json$/ }), coerce => 1, ); sub get_hash { my $self = shift; decode_json( $self->config_file->slurp_utf8 ); } }

    Nice? Types::Path::Tiny is my personal favourite third-party type library. If you're writing an application that needs to deal with files, use it.

    Types::Common::String and Types::Common::Numeric

    Types::Common::String provides a bunch of type constraints more specific than the standard Str type. If you have indicated that an attribute or parameter should be a string, it's pretty rare that you really want to allow any string. You might want to constrain it more. This type library has types like NonEmptyStr and UpperCaseStr.

    Types::Common::Numeric does the same for numbers, giving you type constraints like PositiveInt and IntRange[1,10].

    Both of these libraries come bundled with Type::Tiny, so if you're already using Types::Standard, won't add any extra dependencies to your code.


    This is a type library created for Type::Tiny's internal use and gives you types like ArrayLike, HashLike, and CodeLike which allow overloaded objects.

    Again it's bundled with Type::Tiny, so won't add any extra dependencies.


    A type library for DateTime objects, allowing them to be coerced from strings.

    has start_date => ( is => 'ro', isa => DateTimeUTC, coerce => 1, );

    The above will not only coerce the attribute to a DateTime object, but coerce it to the correct timezone.

Streaming Market Quotes from Ally Invest
1 direct reply — Read more / Contribute
by Your Mother
on Jan 04, 2019 at 14:03

    This service requires you to have an account with Ally. Their investment API is new. I don't work for them or have any professional relationship but they are often mentioned as the best online bank in the US, I've been a customer since they were GMAC (20 years), and they really came through for me once so I have no reservations in recommending them and sharing some code to use their services.

    I had trouble getting streaming to work which was embarassing because it's supposed to be the part of Perl I'm good at. :P pmqs, haukex bliako, kschwab, and vr all stepped up to help and solved my trouble. My new trouble is a websocket client—a cat to skin later—which led me back to Mojolicious. I knew it had some server support so I figured the example code might show client code. I was pleasantly shocked to see it supports it completely via its own client Mojo::UserAgent. All hail sri!

    I was even happier to see the client supports gzip content, even in streams. Might be more confusing code for some but for me it was much easier to follow. I have hesitated to move some of my personal code/practices to Mojo but this is probably the shove I needed.

    Since the problem is now solved with both libraries, I figured I should share some of the code here. Both rely on the terrific WWW::OAuth. There is a fair bit of identical boilerplate for the arguments and environment.

    WWW::Mechanize (LWP::UserAgent)

    #!/usr/bin/env perl use 5.10.0; use strictures; use WWW::Mechanize; # LWP::UserAgent is almost the same here. use WWW::OAuth; use Compress::Zlib; my @symbols = grep /\A[A-Z.]+\z/, @ARGV; die "Give a list of symbols; e.g., AAPL GHII AMZN XOM DIS PBF BABA JD +AMD VOO\n" unless @symbols; my $sym = join ",", @symbols; die "Missing ENV values: ALLY_CLIENT_ID ALLY_CLIENT_SECRET ALLY_TOKEN +ALLY_TOKEN_SECRET\n" unless $ENV{ALLY_CLIENT_ID} and $ENV{ALLY_CLIENT_SECRET} and $ENV{ALLY_TOKEN} and $ENV{ALLY_TOKEN_SECRET}; my $oauth = WWW::OAuth->new( client_id => $ENV{ALLY_CLIENT_ID}, client_secret => $ENV{ALLY_CLIENT_SECRET}, token => $ENV{ALLY_TOKEN}, token_secret => $ENV{ALLY_TOKEN_SECRET} ); my $mech = WWW::Mechanize->new( autocheck => undef ); $mech->add_handler( request_prepare => sub { $oauth->authenticate($_[0 +]) } ); my $gunzip = inflateInit( WindowBits => 16 + MAX_WBITS ) or die "Cannot create a inflation stream\n"; $mech->add_handler ( response_data => sub { my ( $response, $ua, $h, $data ) = @_; $response->content(undef); # Else will append. my ( $buffer, $status ) = $gunzip->inflate($data); die "zlib error: $status" if length $status; say $buffer; }); $mech->get("$sym +"); __END__


    #!/usr/bin/env perl use 5.10.0; use strictures; use Mojo::UserAgent; use WWW::OAuth; my @symbols = grep /\A[A-Z.]+\z/, @ARGV; die "Give a list of symbols; e.g., AAPL GHII AMZN XOM DIS PBF BABA JD +AMD VOO\n" unless @symbols; my $sym = join ",", @symbols; die "Missing ENV values: ALLY_CLIENT_ID ALLY_CLIENT_SECRET ALLY_TOKEN +ALLY_TOKEN_SECRET\n" unless $ENV{ALLY_CLIENT_ID} and $ENV{ALLY_CLIENT_SECRET} and $ENV{ALLY_TOKEN} and $ENV{ALLY_TOKEN_SECRET}; my $oauth = WWW::OAuth->new( client_id => $ENV{ALLY_CLIENT_ID}, client_secret => $ENV{ALLY_CLIENT_SECRET}, token => $ENV{ALLY_TOKEN}, token_secret => $ENV{ALLY_TOKEN_SECRET} ); my $ua = Mojo::UserAgent->new( max_response_size => 0 ); # Stream mean +s no max. $ua->on( start => sub { $oauth->authenticate( $_[1]->req ) } ); # OAut +h all requests. my $tx = $ua->build_tx( GET => " +/quotes?symbols=$sym" ); $tx->res->content ->unsubscribe("read") ->on( read => sub { my ( $content, $bytes ) = @_; say $bytes; }); $tx = $ua->start($tx); __END__
Encrypting Source Filter
No replies — Read more | Post response
by kschwab
on Dec 28, 2018 at 14:48

    After reading bliako's post, I was curious.

    I wanted to experiment a bit with his "I want to be asked for a password/key at encryption stage and then asked just once" requirement. I didn't get it working with PAR, or any ability to also encrypt used modules, so it's not acceptable as an answer there. Might be useful to play with though. Works in a way that's similar to Acme::Bleach, other than I don't overwrite the original file. You'll need to have a working openssl binary in your PATH somewhere.

    Feedback welcome.

    Save this as ""...
    package AESFilter; use IPC::Open2; our $openssl="openssl enc -aes-256-cbc -a"; our $marker = '#AESFilter'; sub encrypt { $_[0]=~s/$marker//gs; my $pid=open2(my $rdr,my $wrt,"$openssl 2>>/dev/null"); print $wrt $_[0]; close $wrt; my $output; while(<$rdr>) {$output.=$_}; close $rdr; waitpid($pid,0); my $status=$?>>8; if ($status !=0) { die("Exit status $status from openssl, encryption failed\n"); } return $output; } sub decrypt { my $pid=open2(my $rdr,my $wrt,"$openssl -d 2>>/dev/null"); print $wrt $_[0]."\n"; close $wrt; my $output; while(<$rdr>) {$output.=$_}; close $rdr; waitpid($pid,0); my $status=$?>>8; if ($status != 0) { die("Exit status $status from openssl, decryption failed\n"); } return $output; } open(IN,$0) or die "Can't open [$0]: $!\n"; my $prior=''; my $code=''; my $seen=0; while(<IN>) { if ($seen) { chomp; $code .= $_; next; } $prior .= $_; if (/use AESFilter;/) { $seen=1} } close IN; if ($code =~ s/^$marker//gm) { my $clear=decrypt($code); eval($prior.$clear); print STDERR $@ if $@; exit; } my $outfile=$0.".enc"; die "Encrypted file [$outfile] already exists\n" if (-e $outfile); my $encrypted=encrypt($code); open(OUT,">$outfile") or die "Can't open [$outfile] for write: $!\n"; printf OUT "%s%s\n%s",$prior,$marker,$encrypted; close OUT; exit; 1;

    To play with it, create a script like what's below. The first time you run it, it will create an encrypted script, with an extension of ".enc". So, if your script is called "foo", it creates a new file called "foo.enc" that's encrypted. It's calling openssl to get a password, so you'll be prompted for a password.

    #!/usr/bin/perl # so that you don't have to install, just # have it in your current dir use lib "."; # anything before the next line will be in the output in cleartext # ...anything after will be encrypted use AESFilter; print "test123\n"; print "again\n"; for ("one","two","three","four") { print $_."\n"; }
    If you save the code in a file called "foo", and run it once (with a password of '0'), it will produce a file called "foo.enc", that looks like this:
    #!/usr/bin/perl # so that you don't have to install, just # have it in your current dir use lib "."; # anything before the next line will be in the output in cleartext # anything after will be encrypted use AESFilter; #AESFilter U2FsdGVkX1/CjxWDKOh4Xdw/7c0PoKnkUFQsf5gxo3F7RXqcEtmdsAgeEmb1g/QO qd82hklpUxP/SNzbs34Z2NdzEStaDpeTlke1unf18gAw/2hlu78CIIItHVuAZlrH ovJhqCBhP0Rck1RwXt3cJw==
    And, if you run that code, it will prompt for the password.

Add your CUFP
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 rifling through the Monastery: (4)
    As of 2020-04-10 13:08 GMT
    Find Nodes?
      Voting Booth?
      The most amusing oxymoron is:

      Results (49 votes). Check out past polls.