Found in /usr/share/perl/5.34/pod/perlfaq4.pod How can I take a string and turn it into epoch seconds? If it's a regular enough string that it always has the same format, you can split it up and pass the parts to "timelocal" in the standard Time::Local module. Otherwise, you should look into the Date::Calc, Date::Parse, and Date::Manip modules from CPAN. How do I unescape a string? It depends just what you mean by "escape". URL escapes are dealt with in perlfaq9. Shell escapes with the backslash ("\") character are removed with s/\\(.)/$1/g; This won't expand "\n" or "\t" or any other special escapes. How do I expand function calls in a string? (contributed by brian d foy) This is documented in perlref, and although it's not the easiest thing to read, it does work. In each of these examples, we call the function inside the braces used to dereference a reference. If we have more than one return value, we can construct and dereference an anonymous array. In this case, we call the function in list context. print "The time values are @{ [localtime] }.\n"; If we want to call the function in scalar context, we have to do a bit more work. We can really have any code we like inside the braces, so we simply have to end with the scalar reference, although how you do that is up to you, and you can use code inside the braces. Note that the use of parens creates a list context, so we need "scalar" to force the scalar context on the function: print "The time is ${\(scalar localtime)}.\n" print "The time is ${ my $x = localtime; \$x }.\n"; If your function already returns a reference, you don't need to create the reference yourself. sub timestamp { my $t = localtime; \$t } print "The time is ${ timestamp() }.\n"; The "Interpolation" module can also do a lot of magic for you. You can specify a variable name, in this case "E", to set up a tied hash that does the interpolation for you. It has several other methods to do this as well. use Interpolation E => 'eval'; print "The time values are $E{localtime()}.\n"; In most cases, it is probably easier to simply use string concatenation, which also forces scalar context. print "The time is " . localtime() . ".\n"; How do I reverse a string? Use "reverse()" in scalar context, as documented in "reverse" in perlfunc. my $reversed = reverse $string; How do I expand tabs in a string? You can do it yourself: 1 while $string =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e; Or you can just use the Text::Tabs module (part of the standard Perl distribution). use Text::Tabs; my @expanded_lines = expand(@lines_with_tabs); How can I access or change N characters of a string? You can access the first characters of a string with substr(). To get the first character, for example, start at position 0 and grab the string of length 1. my $string = "Just another Perl Hacker"; my $first_char = substr( $string, 0, 1 ); # 'J' To change part of a string, you can use the optional fourth argument which is the replacement string. substr( $string, 13, 4, "Perl 5.8.0" ); You can also use substr() as an lvalue. substr( $string, 13, 4 ) = "Perl 5.8.0"; How can I count the number of occurrences of a substring within a string? There are a number of ways, with varying efficiency. If you want a count of a certain single character (X) within a string, you can use the "tr///" function like so: my $string = "ThisXlineXhasXsomeXx'sXinXit"; my $count = ($string =~ tr/X//); print "There are $count X characters in the string"; This is fine if you are just looking for a single character. However, if you are trying to count multiple character substrings within a larger string, "tr///" won't work. What you can do is wrap a while() loop around a global pattern match. For example, let's count negative integers: my $string = "-9 55 48 -2 23 -76 4 14 -44"; my $count = 0; while ($string =~ /-\d+/g) { $count++ } print "There are $count negative numbers in the string"; Another version uses a global match in list context, then assigns the result to a scalar, producing a count of the number of matches. my $count = () = $string =~ /-\d+/g; How can I split a [character]-delimited string except when inside [character]? Several modules can handle this sort of parsing--Text::Balanced, Text::CSV, Text::CSV_XS, and Text::ParseWords, among others. Take the example case of trying to split a string that is comma-separated into its different fields. You can't use "split(/,/)" because you shouldn't split if the comma is inside quotes. For example, take a data line like this: SAR001,"","Cimetrix, Inc","Bob Smith","CAM",N,8,1,0,7,"Error, Core Dumped" Due to the restriction of the quotes, this is a fairly complex problem. Thankfully, we have Jeffrey Friedl, author of *Mastering Regular Expressions*, to handle these for us. He suggests (assuming your string is contained in $text): my @new = (); push(@new, $+) while $text =~ m{ "([^\"\\]*(?:\\.[^\"\\]*)*)",? # groups the phrase inside the quotes | ([^,]+),? | , }gx; push(@new, undef) if substr($text,-1,1) eq ','; If you want to represent quotation marks inside a quotation-mark-delimited field, escape them with backslashes (eg, "like \"this\"". Alternatively, the Text::ParseWords module (part of the standard Perl distribution) lets you say: use Text::ParseWords; @new = quotewords(",", 0, $text); For parsing or generating CSV, though, using Text::CSV rather than implementing it yourself is highly recommended; you'll save yourself odd bugs popping up later by just using code which has already been tried and tested in production for years. How do I strip blank space from the beginning/end of a string? (contributed by brian d foy) A substitution can do this for you. For a single line, you want to replace all the leading or trailing whitespace with nothing. You can do that with a pair of substitutions: s/^\s+//; s/\s+$//; You can also write that as a single substitution, although it turns out the combined statement is slower than the separate ones. That might not matter to you, though: s/^\s+|\s+$//g; In this regular expression, the alternation matches either at the beginning or the end of the string since the anchors have a lower precedence than the alternation. With the "/g" flag, the substitution makes all possible matches, so it gets both. Remember, the trailing newline matches the "\s+", and the "$" anchor can match to the absolute end of the string, so the newline disappears too. Just add the newline to the output, which has the added benefit of preserving "blank" (consisting entirely of whitespace) lines which the "^\s+" would remove all by itself: while( <> ) { s/^\s+|\s+$//g; print "$_\n"; } For a multi-line string, you can apply the regular expression to each logical line in the string by adding the "/m" flag (for "multi-line"). With the "/m" flag, the "$" matches *before* an embedded newline, so it doesn't remove it. This pattern still removes the newline at the end of the string: $string =~ s/^\s+|\s+$//gm; Remember that lines consisting entirely of whitespace will disappear, since the first part of the alternation can match the entire string and replace it with nothing. If you need to keep embedded blank lines, you have to do a little more work. Instead of matching any whitespace (since that includes a newline), just match the other whitespace: $string =~ s/^[\t\f ]+|[\t\f ]+$//mg; How do I pad a string with blanks or pad a number with zeroes? In the following examples, $pad_len is the length to which you wish to pad the string, $text or $num contains the string to be padded, and $pad_char contains the padding character. You can use a single character string constant instead of the $pad_char variable if you know what it is in advance. And in the same way you can use an integer in place of $pad_len if you know the pad length in advance. The simplest method uses the "sprintf" function. It can pad on the left or right with blanks and on the left with zeroes and it will not truncate the result. The "pack" function can only pad strings on the right with blanks and it will truncate the result to a maximum length of $pad_len. # Left padding a string with blanks (no truncation): my $padded = sprintf("%${pad_len}s", $text); my $padded = sprintf("%*s", $pad_len, $text); # same thing # Right padding a string with blanks (no truncation): my $padded = sprintf("%-${pad_len}s", $text); my $padded = sprintf("%-*s", $pad_len, $text); # same thing # Left padding a number with 0 (no truncation): my $padded = sprintf("%0${pad_len}d", $num); my $padded = sprintf("%0*d", $pad_len, $num); # same thing # Right padding a string with blanks using pack (will truncate): my $padded = pack("A$pad_len",$text); If you need to pad with a character other than blank or zero you can use one of the following methods. They all generate a pad string with the "x" operator and combine that with $text. These methods do not truncate $text. Left and right padding with any character, creating a new string: my $padded = $pad_char x ( $pad_len - length( $text ) ) . $text; my $padded = $text . $pad_char x ( $pad_len - length( $text ) ); Left and right padding with any character, modifying $text directly: substr( $text, 0, 0 ) = $pad_char x ( $pad_len - length( $text ) ); $text .= $pad_char x ( $pad_len - length( $text ) ); How do I extract selected columns from a string? (contributed by brian d foy) If you know the columns that contain the data, you can use "substr" to extract a single column. my $column = substr( $line, $start_column, $length ); You can use "split" if the columns are separated by whitespace or some other delimiter, as long as whitespace or the delimiter cannot appear as part of the data. my $line = ' fred barney betty '; my @columns = split /\s+/, $line; # ( '', 'fred', 'barney', 'betty' ); my $line = 'fred||barney||betty'; my @columns = split /\|/, $line; # ( 'fred', '', 'barney', '', 'betty' ); If you want to work with comma-separated values, don't do this since that format is a bit more complicated. Use one of the modules that handle that format, such as Text::CSV, Text::CSV_XS, or Text::CSV_PP. If you want to break apart an entire line of fixed columns, you can use "unpack" with the A (ASCII) format. By using a number after the format specifier, you can denote the column width. See the "pack" and "unpack" entries in perlfunc for more details. my @fields = unpack( $line, "A8 A8 A8 A16 A4" ); Note that spaces in the format argument to "unpack" do not denote literal spaces. If you have space separated data, you may want "split" instead. How do I find the soundex value of a string? (contributed by brian d foy) You can use the "Text::Soundex" module. If you want to do fuzzy or close matching, you might also try the String::Approx, and Text::Metaphone, and Text::DoubleMetaphone modules. How can I expand variables in text strings? (contributed by brian d foy) If you can avoid it, don't, or if you can use a templating system, such as Text::Template or Template Toolkit, do that instead. You might even be able to get the job done with "sprintf" or "printf": my $string = sprintf 'Say hello to %s and %s', $foo, $bar; However, for the one-off simple case where I don't want to pull out a full templating system, I'll use a string that has two Perl scalar variables in it. In this example, I want to expand $foo and $bar to their variable's values: my $foo = 'Fred'; my $bar = 'Barney'; $string = 'Say hello to $foo and $bar'; One way I can do this involves the substitution operator and a double "/e" flag. The first "/e" evaluates $1 on the replacement side and turns it into $foo. The second /e starts with $foo and replaces it with its value. $foo, then, turns into 'Fred', and that's finally what's left in the string: $string =~ s/(\$\w+)/$1/eeg; # 'Say hello to Fred and Barney' The "/e" will also silently ignore violations of strict, replacing undefined variable names with the empty string. Since I'm using the "/e" flag (twice even!), I have all of the same security problems I have with "eval" in its string form. If there's something odd in $foo, perhaps something like "@{[ system "rm -rf /" ]}", then I could get myself in trouble. To get around the security problem, I could also pull the values from a hash instead of evaluating variable names. Using a single "/e", I can check the hash to ensure the value exists, and if it doesn't, I can replace the missing value with a marker, in this case "???" to signal that I missed something: my $string = 'This has $foo and $bar'; my %Replacements = ( foo => 'Fred', ); # $string =~ s/\$(\w+)/$Replacements{$1}/g; $string =~ s/\$(\w+)/ exists $Replacements{$1} ? $Replacements{$1} : '???' /eg; print $string; Does Perl have anything like Ruby's #{} or Python's f string? Unlike the others, Perl allows you to embed a variable naked in a double quoted string, e.g. "variable $variable". When there isn't whitespace or other non-word characters following the variable name, you can add braces (e.g. "foo ${foo}bar") to ensure correct parsing. An array can also be embedded directly in a string, and will be expanded by default with spaces between the elements. The default LIST_SEPARATOR can be changed by assigning a different string to the special variable $", such as "local $" = ', ';". Perl also supports references within a string providing the equivalent of the features in the other two languages. "${\ ... }" embedded within a string will work for most simple statements such as an object->method call. More complex code can be wrapped in a do block "${\ do{...} }". When you want a list to be expanded per $", use "@{[ ... ]}". use Time::Piece; use Time::Seconds; my $scalar = 'STRING'; my @array = ( 'zorro', 'a', 1, 'B', 3 ); # Print the current date and time and then Tommorrow my $t = Time::Piece->new; say "Now is: ${\ $t->cdate() }"; say "Tomorrow: ${\ do{ my $T=Time::Piece->new + ONE_DAY ; $T->fullday }}"; # some variables in strings say "This is some scalar I have $scalar, this is an array @array."; say "You can also write it like this ${scalar} @{array}."; # Change the $LIST_SEPARATOR local $" = ':'; say "Set \$\" to delimit with ':' and sort the Array @{[ sort @array ]}"; You may also want to look at the module Quote::Code, and templating tools such as Template::Toolkit and Mojo::Template. See also: "How can I expand variables in text strings?" and "How do I expand function calls in a string?" in this FAQ. Found in /usr/share/perl/5.34/pod/perlfaq5.pod How can I open a filehandle to a string? (contributed by Peter J. Holzer, hjp-usenet2 AT hjp.at) Since Perl 5.8.0 a file handle referring to a string can be created by calling open with a reference to that string instead of the filename. This file handle can then be used to read from or write to the string: open(my $fh, '>', \$string) or die "Could not open string for writing"; print $fh "foo\n"; print $fh "bar\n"; # $string now contains "foo\nbar\n" open(my $fh, '<', \$string) or die "Could not open string for reading"; my $x = <$fh>; # $x now contains "foo\n" With older versions of Perl, the IO::String module provides similar functionality. How can I write() into a string? (contributed by brian d foy) If you want to "write" into a string, you just have to <open> a filehandle to a string, which Perl has been able to do since Perl 5.6: open FH, '>', \my $string; write( FH ); Since you want to be a good programmer, you probably want to use a lexical filehandle, even though formats are designed to work with bareword filehandles since the default format names take the filehandle name. However, you can control this with some Perl special per-filehandle variables: $^, which names the top-of-page format, and $~ which shows the line format. You have to change the default filehandle to set these variables: open my($fh), '>', \my $string; { # set per-filehandle variables my $old_fh = select( $fh ); $~ = 'ANIMAL'; $^ = 'ANIMAL_TOP'; select( $old_fh ); } format ANIMAL_TOP = ID Type Name . format ANIMAL = @## @<<< @<<<<<<<<<<<<<< $id, $type, $name . Although write can work with lexical or package variables, whatever variables you use have to scope in the format. That most likely means you'll want to localize some package variables: { local( $id, $type, $name ) = qw( 12 cat Buster ); write( $fh ); } print $string; There are also some tricks that you can play with "formline" and the accumulator variable $^A, but you lose a lot of the value of formats since "formline" won't handle paging and so on. You end up reimplementing formats when you use them. Found in /usr/share/perl/5.34/pod/perlfaq6.pod How can I match strings with multibyte characters? Starting from Perl 5.6 Perl has had some level of multibyte character support. Perl 5.8 or later is recommended. Supported multibyte character repertoires include Unicode, and legacy encodings through the Encode module. See perluniintro, perlunicode, and Encode. If you are stuck with older Perls, you can do Unicode with the Unicode::String module, and character conversions using the Unicode::Map8 and Unicode::Map modules. If you are using Japanese encodings, you might try using the jperl 5.005_03. Finally, the following set of approaches was offered by Jeffrey Friedl, whose article in issue #5 of The Perl Journal talks about this very matter. Let's suppose you have some weird Martian encoding where pairs of ASCII uppercase letters encode single Martian letters (i.e. the two bytes "CV" make a single Martian letter, as do the two bytes "SG", "VS", "XX", etc.). Other bytes represent single characters, just like ASCII. So, the string of Martian "I am CVSGXX!" uses 12 bytes to encode the nine characters 'I', ' ', 'a', 'm', ' ', 'CV', 'SG', 'XX', '!'. Now, say you want to search for the single character "/GX/". Perl doesn't know about Martian, so it'll find the two bytes "GX" in the "I am CVSGXX!" string, even though that character isn't there: it just looks like it is because "SG" is next to "XX", but there's no real "GX". This is a big problem. Here are a few ways, all painful, to deal with it: # Make sure adjacent "martian" bytes are no longer adjacent. $martian =~ s/([A-Z][A-Z])/ $1 /g; print "found GX!\n" if $martian =~ /GX/; Or like this: my @chars = $martian =~ m/([A-Z][A-Z]|[^A-Z])/g; # above is conceptually similar to: my @chars = $text =~ m/(.)/g; # foreach my $char (@chars) { print "found GX!\n", last if $char eq 'GX'; } Or like this: while ($martian =~ m/\G([A-Z][A-Z]|.)/gs) { # \G probably unneeded if ($1 eq 'GX') { print "found GX!\n"; last; } } Here's another, slightly less painful, way to do it from Benjamin Goldberg, who uses a zero-width negative look-behind assertion. print "found GX!\n" if $martian =~ m/ (?<![A-Z]) (?:[A-Z][A-Z])*? GX /x; This succeeds if the "martian" character GX is in the string, and fails otherwise. If you don't like using (?<!), a zero-width negative look-behind assertion, you can replace (?<![A-Z]) with (?:^|[^A-Z]). It does have the drawback of putting the wrong thing in $-[0] and $+[0], but this usually can be worked around. Found in /usr/share/perl/5.34/pod/perlfaq7.pod Do I always/never have to quote my strings or use semicolons and commas? Normally, a bareword doesn't need to be quoted, but in most cases probably should be (and must be under "use strict"). But a hash key consisting of a simple word and the left-hand operand to the "=>" operator both count as though they were quoted: This is like this ------------ --------------- $foo{line} $foo{'line'} bar => stuff 'bar' => stuff The final semicolon in a block is optional, as is the final comma in a list. Good style (see perlstyle) says to put them in except for one-liners: if ($whoops) { exit 1 } my @nums = (1, 2, 3); if ($whoops) { exit 1; } my @lines = ( "There Beren came from mountains cold", "And lost he wandered under leaves", ); Found in /usr/share/perl/5.34/pod/perlfaq9.pod How do I remove HTML from a string? Use HTML::Strip, or HTML::FormatText which not only removes HTML but also attempts to do a little simple formatting of the resulting plain text. How do I decode a MIME/BASE64 string? The MIME::Base64 package handles this as well as the MIME/QP encoding. Decoding base 64 becomes as simple as: use MIME::Base64; my $decoded = decode_base64($encoded); The Email::MIME module can decode base 64-encoded email message parts transparently so the developer doesn't need to worry about it.
Generated by phpman local Author: Che Dong Under GNU General Public License
2026-06-15 04:23 @216.73.216.200
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)