String::Scanf(3pm)

πŸ“¦
version 2.1 Β· 3 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

✨ String::Scanf ✨

okay so bestie, this is the String::Scanf page from the User Contributed Perl Documentation (that's the 3pm fam πŸ’–), and it's literally the Perl-flavored glow-up of C's sscanf() β€” no cap.


🌸 NAME 🌸

String::Scanf β€” the way it literally emulates sscanf() from the C library 🫢


πŸŽ€ SYNOPSIS πŸŽ€

use String::Scanf; # imports sscanf()

($a, $b, $c, $d) = sscanf("%d+%d %f-%s", $input);
($e, $f, $g, $h) = sscanf("%x %o %s:%3c"); # input defaults to $_

$r = String::Scanf::format_to_re($f);

or, and hear me out, the object-oriented era πŸ’…:

# works only for Perl 5.005 or later
use String::Scanf qw(); # import nothing

my $s1 = String::Scanf->new("%d+%d %f-%s");
my $s2 = String::Scanf->new("%x %o %s:%3c");

($a, $b, $c, $d) = $s1->sscanf($input);
($e, $f, $g, $h) = $s2->sscanf(); # input defaults to $_
heads up bestie: that second block only works on Perl 5.005 or later. noted. moving on. ✨

πŸ’œ DESCRIPTION πŸ’œ

String::Scanf scans strings for data using formats that are basically sscanf() from libc/stdio but make it Perl. πŸ‘‘

here's the tea on the supported sscanf() formats:

  • %d β€” a decimal integer, and it will lowkey accept an optional plus or minus sign in front. πŸ“
  • %u β€” decimal unsigned integer, optional plus sign allowed.
  • %x β€” hexadecimal unsigned integer, and it'll happily eat an optional 0x or 0x sitting in front. 🌈
  • %o β€” octal unsigned integer.
  • %e %f %g β€” so the [efg] trio all work identically, no favorites here. this one's a decimal floating point number, optional plus or minus sign, and it will vibe with any of these formats:
1
1.
1.23
.23
1e45
1.e45
1.23e45
.23e45

the exponent also gets an optional plus or minus sign, and that little e? it can also show up as E. iconic flexibility. ✨ the messy borderline cases like Inf and Nan are not recognized, so don't even try it. πŸ’€

  • %s β€” a non-whitespace string. simple, clean, slay.
  • %c β€” a string of characters, and what you get back is an array reference containing the numerical values of those characters. πŸŽ€
  • %% β€” a literal %. yes, that's the whole thing.
big yikes: the sscanf() formats [pnSC] are not supported. fr fr, they're just not here.

so %s and %c can carry an optional maximum width, like %4s β€” in that case at most that many characters get consumed, but fewer characters are also accepted. no pressure. πŸ¦‹

the numeric formats can have a width too, but bestie, it's just ignored. it's giving decorative.

same energy with [hl] β€” the numeric formats can have it right before the main option, e.g. %hd β€” but since those widths mean absolutely nothing in Perl, they get ignored. periodt. πŸ’…

non-format parts of the parameter string are matched literally (like, : matches as :), except that any whitespace is matched as any whitespace β€” so a " " matches as \s+. understood the assignment. πŸ‘‘


⚠️ WARNING ⚠️

this is the important part, don't scroll past it. πŸ™ˆ

the numeric formats only match something that looks like a number. they do not care whether that number actually fits into Perl's numbers. so like, 123e456789 is perfectly valid for sscanf(), but it quite probably will not fit into your Perl's numbers. 😭

consider using the various Math::* modules instead. seriously. do that. 🫢


Jarkko Hietaniemi <jhi@iki.fi>

Copyright (c) 2002,2004 Jarkko Hietaniemi. All rights reserved. πŸ’–

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


🌟 the sign-off 🌟

and that's the whole page, bestie β€” brought to you by perl v5.36.0, dated 2022-10-22, and yes the man page title is literally String::Scanf(3pm). go forth and scan those strings. πŸ¦‹βœ¨πŸ’œ

πŸ“¦ where this page comes from