When Laziness Isn't

I just needed a few rows of UUIDs in a column of a spreadsheet, more for esthetics than anything else. uuidgen to the rescue.

At the time I didn't realize that uuidgen natively supports outputting multiple ids like so
uuidgen -C 8


The truly lazy path would have been to read the fine uuidgen manual.

Alas, supposing I needed to make multiple calls to uuidgen, I went with a Perl one-liner with a loop, as I couldn't recall the Bash loop syntax.

Here comes the laziness... I I didn't want to write something like this:

perl -e 'print `uuidgen` for @{[1..5]}';


I'm not so found of of perl's de-reference syntax these days, also that array reference/range was giving "the ick" as my kids would say. I needed something lazier, cleaner. I wondered if there were any default/exported arrays available to me that don't have too many elements to them.... Ah, I know!



$ perl -e 'print `uuidgen` for @INC';

d2c9c4b9-2126-4eda-ba52-ca30fdc55db0
eac4f86a-04eb-4c1a-aba1-fb1fa5c7dcda
2a2c416c-00bc-46d8-b7ce-c639f73cef26
4cc052cc-6423-4420-bbf5-595a7ad28c51
0bb78a2e-f4e9-44cd-80ae-e463197398f5
37728b6c-69dc-4669-99e7-2814b0d5e2a6
5acf78b2-6938-465b-ad8a-3bf29037e749
87d6d4ef-e85c-40bb-b3c2-acf9dc88f3e1


This is more a case of (ab)using a variable for an unintended purpose, but today it got the job done, even if it wasn't the most lazy approach. Hubris? Maybe.

7 Comments

Just FYI no need for the array dereference in your example, could just do:


perl -e 'print `uuidgen` for (1..5)';

Heh, that's pretty clever to get to some number of values. However, you didn't need the reference:

$ perl -le "system q(uuidgen) for 1..5"
76666A86-7155-4EBF-9ED4-F2625DE3CE55
CA1A8D82-467A-4682-8F96-FA0BE0800676
40850CBF-1A41-4803-BE9C-BD87B548826E
08DD5AB6-EBEC-40E9-B4AB-817708A0A584
8BCC70B1-4430-45C6-AF72-5A839EAE6DD5

There can even be a parameter in there:

$ u 14
4073DA92-F528-46C1-84D7-A88E2575C4A2
ADD8AB19-10EA-4D5B-A23E-DC580C6E1B5C
A52DD5A2-36BA-42DE-A604-8DB85B2F6300
2B12ED05-D502-4F1E-A9E0-DF97CE2D6C7E
5FDF7AB1-A8B4-4F6F-A7CC-88FAC617C299
A7F29335-AD7B-469C-97E8-A626D1A28B68
53BD868B-11A2-49B4-9EAD-8979AEA0089B
3FDB4339-6C88-470C-8FB7-C1326665A1CD
3EEFCB02-5FBE-41BA-95FB-9CA41FE152CA
D0639836-5046-473A-8572-F7F432144A2B
2A212385-69C2-42A9-A00B-E67AB3858486
2EC62806-27A9-4C40-B487-D48BCD31ED5D
89E0DE45-6A79-4D9D-8948-D89ADE766940
DDD4BEF0-134E-4ED9-A084-A33C2E18ACBB

And, I can never remember the bash for loop either, which is how I end up at the same [Stackoverflow answer](https://stackoverflow.com/q/49110/2766176) (where I learned bash has a `help` command for builtins, although the format of `help for` doesn't do itself any favors.

My problem is always the right number and placement of the semicolons on the one-liner:

$ for i in $(seq 1 5); do uuidgen; done
33E5B2E9-4212-4997-8F5E-B812AE9FBFBD
CE775C34-7AC6-4C39-85F4-E03FC218D510
60964665-84E6-476D-A31D-9EE70A2EB079
EE4BFA26-3613-44E3-ADD8-8C2B81D4CA0E
4244A1F1-9EF6-4263-8F79-8B0109D1AB00

$ for i in {1..5}; do uuidgen; done
D0C753DE-034A-41F9-A846-109A96891F37
993AA245-C215-4781-AD52-6F6226DAD4DB
EAF31617-FC9E-4636-9530-750DF7EA41AF
5DCE95BB-22CE-4018-9178-C6083FEDF134
E129283C-C377-499D-B0A4-5926913F943E

Crap, this submitted when I was trying to change something. I had an `alias` example right before the `u 14` line.

$ alias u="perl -le 'system q(uuidgen) for 1..$$ARGV[0]'"

@briandfoy - My shell now sources a separate zsh_aliases file with many dozen aliases - about half of which I've learned from you, like your set of very handy number conversions. I'm sure others collect yours, too. So it seemed worthwhile to post that this one gives me an error (recent macOS, Perl 5.40.1):

alias u="perl -le 'system q(uuidgen) for 1..$$ARGV[0]'"

Looks like the double '$' is being interpreted as the process ID:

Bareword found where operator expected (Missing operator before "ARGV"?) at -e line 1, near "54994ARGV"
syntax error at -e line 1, near "54994ARGV"

All the best,

This works, getting the value from @ARGV as a slice:

alias un="perl -e 'system q(uuidgen) for 1..@ARGV[0]'"

As does this:

alias u="perl -e 'print qx(uuidgen) for 1..@ARGV[0]'"

But I'm not clear why plain $ARGV[0] doesn't work there. Your suggested '$$ARGV[0]' doesn't mean the same thing as '@ARGV[0]' in perl directly. Is this a shell thing? (I'm using zsh.)

The $$ in $$ARGV[0] is being replaced when the alias is created:


tony@venus:~$ alias u="perl -le 'system q(uuidgen) for 1..$$ARGV[0]'"
tony@venus:~$ alias
alias u='perl -le '\''system q(uuidgen) for 1..95370ARGV[0]'\'''

The shell doesn't use @ here so @ARGV[0] works.

The single $ARGV is replaced with nothing, so it also doesn't work:

$ alias u="perl -le 'system q(uuidgen) for 1..$ARGV[0]'"
tony@venus:~$ alias
alias u='perl -le '\''system q(uuidgen) for 1..[0]'\'''

You can avoid the replacement with bash and other sh-alikes by quoting the $:

tony@venus:~$ alias u="perl -le 'system q(uuidgen) for 1..\$ARGV[0]'"
tony@venus:~$ alias
alias u='perl -le '\''system q(uuidgen) for 1..$ARGV[0]'\'''
tony@venus:~$ u 2
1e92e5c1-b589-4c9e-a530-da0376e4de9e
b5253531-730a-4a4f-b196-c73a5f3ff218

Leave a comment

About silent11

user-pic Will Willis (silent11)