php: writing command-line applications with macrame. pt 2

in this series, we’ve been looking at how to write command line applications in php using macrame. in the previous installment, we covered the basic structure of macrame scripts, getting user input both as text and from interactive menus, parsing command-line arguments, and styling our output text.

the sample application we’re building is a script that fetches a list of a mastodon user’s followers and outputs the data in a nicely-formatted table. it looks like this:

the sample script in action
Continue reading →

php: powerful sorting with usort

i recently worked on a rescue project where the original dev wanted to sort an array of item objects first by manufacturer name and then by price, descending. their ‘solution’ was an eighty-line foreach mess of fiddling with array keys. it was difficult to read, more difficult to modify, and didn’t even work correctly lots of the time. i replaced the whole thing with a five-line usort call.

usort stands for ‘user-defined sort’. it allows us, as devs, to write our own sorting rules (plural!) and makes working with arrays of complex data easier.

in this post, we’re going to take a thorough look at sorting arrays with usort. we’ll go over the basics of how the function works, write some ascending and descending sorts on integers and strings, and then cover how to sort on multiple values.

best other sorting functions can do for you is one rule on an array of primitives
Continue reading →

php: writing command-line applications with macrame. pt 1

php doesn’t get a lot of attention as a command line scripting language. which is a shame, since php has a lot of features that make it a good choice for writing terminal apps.

in this series, we’ll be going over writing interactive command line scripts using the macrame library. we’ll be working through building an example project, a script that fetches a list of a mastodon user’s followers, from start to end, and will cover topics such as getting and validating user input, building interactive menus, handling command line arguments, accessing files safely, styling output text, and running functions in the background while showing our users an animated spinner.

further information on macrame can be found on the documentation site.

the sample project

the project we will be working through is a simple command-line script that returns a list of a mastodon user’s followers. running it looks like this:

Continue reading →

vim: five tips to become a more mediocre vim user

i am a mediocre vim user. sure, i’ve been using vim for over twenty years, have handcrafted a custom vimrc, and even written a syntax file, but for my day-to-day usage, my skills are resoundingly and undeniably mediocre.

this is a good place to be. people who are ‘power users’ spend so much time fiddling with plugins and configurations to build their perfect homerolled ide that they never get any real work done. vim becomes their personality, not their tool. by contrast, novices don’t have the skill or knowledge to leverage vim’s power. they thrash around, treating vim like a cumbersome and difficult nano; they google how to quit. that’s not good for productivity!

the sweet spot is in the middle: mediocrity.

this post is going to go over five vim features that will get you on the path from novice to mediocre.

nobody cares if you’re mediocre at vim
Continue reading →

mysql: “thai food near me”, or: doing geo distance calculations in your database.

we’re all familiar with the whole “thai food near me” thing. you type that phrase into your phone and it responds with a list of thai restaurants that are, well, near you. and we have a kind-of understanding of how that works under the hood: google or whoever has a database of thai restaurants with their latitudes and longitudes and knows our location from our phone and then does ‘some process’ to figure out which thai places are nearby.

in this post,we’ll be going over that ‘some process’ part, looking at how to use mysql to do some standard location stuff. we’ll cover mysql’s POINT and POLYGON types, finding the distance between two points on a sphere (which the earth, contrary to what you may have read on the internet, is), determining if a point is inside of a polygon defined by points, and look at things like ‘spatial reference systems’ which define how coordinates are plotted on the surface of the earth.

a restaurant attempts an sql injection attack
Continue reading →

nginx: putting your site in ‘downtime’ for everyone except you

we’ve all been in that less-than-ideal situation of something going horribly awry in production and having to put the site into downtime while we fix it. that “scheduled maintenance”[sic.] page is important because it keeps users from seeing our glaring error, but it makes investigating or fixing production more difficult because, well, the site is in downtime.

in this post, we’re going to go over a couple of ways we can use nginx to show different content to different users based on their ip address; configuring our web server so that everyone in the world gets our downtime message, except us. we get to see site as normal, allowing us to engage in the not-quite-best-practice of debugging in production.

two users (left) are served the well-crafted downtime page, while the developer (right) sees the real site.
Continue reading →

php: concurrency with processes. pt. 2: interprocess communication with shmop

php isn’t the sort of language where developers usually think about things like memory. we just sort of sling around variables and functions and let the internals figure out all that ‘ram stuff’ for us. let’s change that.

in first part of this series, we built a php script that was able to run a number of tasks concurrently by forking child processes. it worked pretty well, but there was a glaring, unaddressed problem: there was no way for those child processes to send data back to the parent process.

in this installment, we’re going to solve that issue by using shmop, php’s “shared memory operations”.

shmop!
Continue reading →

php: concurrency with processes. pt. 1: using pcntl_fork for fun and performance

i often joke that php is a systems programming language that you can also use to create a home page. it’s not really true, of course; no one is going to write, say, a video driver in php. but it does highlight the fact that php has an absolute tonne of functionality that is either straight through calls to c or built-in libraries that allow us to access all sorts of operating systems features.

in this series, we’re going to leverage some of that power to write a php program that does multiple things at the same time. we’ll do that by using pcntl_fork to create a number of child processes that run simultaneously. then in part two, we’ll be looking at shmop, php’s ‘shared memory operations’, as a way to allow those processes to communicate their results.

a process forking a copy of itself to do work concurrently
Continue reading →

amber: writing bash scripts in amber instead. pt. 4: functions

a while ago, i blogged about uploading files to s3 using curl and provided the solution as two functions written in bash, and basically the only feedback was “wait. you can write functions in bash?”

you can. but in reality, you probably don’t want to. the syntax for defining (and calling) bash functions is horrible. there’s a reason people genrally don’t use them. writing and using functions in amber, by comparison, is a borderline delight of sanity. if you’ve ever written a function in php or python or javascript, amber’s function syntax will feel familiar and, well, ‘normal’.

a look at the syntax of functions in bash
Continue reading →

amber: writing bash scripts in amber instead. pt. 3: the standard library

a programming language is only ever going to be as good as its standard library. try writing a c program without <string.h>, for instance . it’s not great.

amber’s standard library is not large — it’s only 24 commands, and some of those aren’t yet available in version 1.0 — but it provides a lot of the convenience functionality that we want when scripting. there’s string manipulation, array handling, some file access stuff. all of these functions are, under the hood, just bash commands, but they provide proven and convenient ways to do the things we want to do.

none of the standard library commands are covered in the official documentation. to find out what they do (or even that they exist), you need to read the source code. so, lets’ go over them here so we don’t have to do that.

a developer uses a standard library function to perform a basic but important task
Continue reading →