@@ -1914,6 +1914,187 @@ expectations of shell programmers, back-quotes do I<NOT> interpolate
1914
1914
within double quotes, nor do single quotes impede evaluation of
1915
1915
variables when used within double quotes.
1916
1916
1917
+ =head3 Simpler Quote-Like Operators
1918
+ X<operator, quote-like>
1919
+
1920
+ =over 4
1921
+
1922
+ =item C<q/I<STRING>/>
1923
+ X<q> X<quote, single> X<'> X<''>
1924
+
1925
+ =item C<'I<STRING>'>
1926
+
1927
+ A single-quoted, literal string. A backslash represents a backslash
1928
+ unless followed by the delimiter or another backslash, in which case
1929
+ the delimiter or backslash is interpolated.
1930
+
1931
+ $foo = q!I said, "You said, 'She said it.'"!;
1932
+ $bar = q('This is it.');
1933
+ $baz = '\n'; # a two-character string
1934
+
1935
+ =item C<qq/I<STRING>/>
1936
+ X<qq> X<quote, double> X<"> X<"">
1937
+
1938
+ =item C<"I<STRING>">
1939
+
1940
+ A double-quoted, interpolated string.
1941
+
1942
+ $_ .= qq
1943
+ (*** The previous line contains the naughty word "$1".\n)
1944
+ if /\b(tcl|java|python)\b/i; # :-)
1945
+ $baz = "\n"; # a one-character string
1946
+
1947
+ =item C<qx/I<STRING>/>
1948
+ X<qx> X<`> X<``> X<backtick>
1949
+
1950
+ =item C<`I<STRING>`>
1951
+
1952
+ A string which is (possibly) interpolated and then executed as a
1953
+ system command, via F</bin/sh> or its equivalent if required. Shell
1954
+ wildcards, pipes, and redirections will be honored. Similarly to
1955
+ C<system>, if the string contains no shell metacharacters then it will
1956
+ be executed directly. The collected standard output of the command is
1957
+ returned; standard error is unaffected. In scalar context, it comes
1958
+ back as a single (potentially multi-line) string, or C<undef> if the
1959
+ shell (or command) could not be started. In list context, returns a
1960
+ list of lines (however you've defined lines with C<$/> or
1961
+ C<$INPUT_RECORD_SEPARATOR>), or an empty list if the shell (or command)
1962
+ could not be started.
1963
+
1964
+ print qx/date/; # prints "Sun Jan 28 06:16:19 CST 2024"
1965
+
1966
+ Because backticks do not affect standard error, use shell file descriptor
1967
+ syntax (assuming the shell supports this) if you care to address this.
1968
+ To capture a command's STDERR and STDOUT together:
1969
+
1970
+ $output = `cmd 2>&1`;
1971
+
1972
+ To capture a command's STDOUT but discard its STDERR:
1973
+
1974
+ $output = `cmd 2>/dev/null`;
1975
+
1976
+ To capture a command's STDERR but discard its STDOUT (ordering is
1977
+ important here):
1978
+
1979
+ $output = `cmd 2>&1 1>/dev/null`;
1980
+
1981
+ To exchange a command's STDOUT and STDERR in order to capture the STDERR
1982
+ but leave its STDOUT to come out the old STDERR:
1983
+
1984
+ $output = `cmd 3>&1 1>&2 2>&3 3>&-`;
1985
+
1986
+ To read both a command's STDOUT and its STDERR separately, it's easiest
1987
+ to redirect them separately to files, and then read from those files
1988
+ when the program is done:
1989
+
1990
+ system("program args 1>program.stdout 2>program.stderr");
1991
+
1992
+ The STDIN filehandle used by the command is inherited from Perl's STDIN.
1993
+ For example:
1994
+
1995
+ open(SPLAT, "stuff") || die "can't open stuff: $!";
1996
+ open(STDIN, "<&SPLAT") || die "can't dupe SPLAT: $!";
1997
+ print STDOUT `sort`;
1998
+
1999
+ will print the sorted contents of the file named F<"stuff">.
2000
+
2001
+ Using single-quote as a delimiter protects the command from Perl's
2002
+ double-quote interpolation, passing it on to the shell instead:
2003
+
2004
+ $perl_info = qx(ps $$); # that's Perl's $$
2005
+ $shell_info = qx'ps $$'; # that's the new shell's $$
2006
+
2007
+ How that string gets evaluated is entirely subject to the command
2008
+ interpreter on your system. On most platforms, you will have to protect
2009
+ shell metacharacters if you want them treated literally. This is in
2010
+ practice difficult to do, as it's unclear how to escape which characters.
2011
+ See L<perlsec> for a clean and safe example of a manual C<fork()> and C<exec()>
2012
+ to emulate backticks safely.
2013
+
2014
+ On some platforms (notably DOS-like ones), the shell may not be
2015
+ capable of dealing with multiline commands, so putting newlines in
2016
+ the string may not get you what you want. You may be able to evaluate
2017
+ multiple commands in a single line by separating them with the command
2018
+ separator character, if your shell supports that (for example, C<;> on
2019
+ many Unix shells and C<&> on the Windows NT C<cmd> shell).
2020
+
2021
+ Perl will attempt to flush all files opened for
2022
+ output before starting the child process, but this may not be supported
2023
+ on some platforms (see L<perlport>). To be safe, you may need to set
2024
+ C<$|> (C<$AUTOFLUSH> in C<L<English>>) or call the C<autoflush()> method of
2025
+ C<L<IO::Handle>> on any open handles.
2026
+
2027
+ Beware that some command shells may place restrictions on the length
2028
+ of the command line. You must ensure your strings don't exceed this
2029
+ limit after any necessary interpolations. See the platform-specific
2030
+ release notes for more details about your particular environment.
2031
+
2032
+ Using this operator can lead to programs that are difficult to port,
2033
+ because the shell commands called vary between systems, and may in
2034
+ fact not be present at all. As one example, the C<type> command under
2035
+ the POSIX shell is very different from the C<type> command under DOS.
2036
+ That doesn't mean you should go out of your way to avoid backticks
2037
+ when they're the right way to get something done. Perl was made to be
2038
+ a glue language, and one of the things it glues together is commands.
2039
+ Just understand what you're getting yourself into.
2040
+
2041
+ Like C<system>, backticks put the child process exit code in C<$?>.
2042
+ If you'd like to manually inspect failure, you can check all possible
2043
+ failure modes by inspecting C<$?> like this:
2044
+
2045
+ if ($? == -1) {
2046
+ print "failed to execute: $!\n";
2047
+ }
2048
+ elsif ($? & 127) {
2049
+ printf "child died with signal %d, %s coredump\n",
2050
+ ($? & 127), ($? & 128) ? 'with' : 'without';
2051
+ }
2052
+ else {
2053
+ printf "child exited with value %d\n", $? >> 8;
2054
+ }
2055
+
2056
+ Use the L<open> pragma to control the I/O layers used when reading the
2057
+ output of the command, for example:
2058
+
2059
+ use open IN => ":encoding(UTF-8)";
2060
+ my $x = `cmd-producing-utf-8`;
2061
+
2062
+ C<qx//> can also be called like a function with L<perlfunc/readpipe>.
2063
+
2064
+ See L</"I/O Operators"> for more discussion.
2065
+
2066
+ =item C<qw/I<STRING>/>
2067
+ X<qw> X<quote, list> X<quote, words>
2068
+
2069
+ Evaluates to a list of the words extracted out of I<STRING>, using embedded
2070
+ whitespace as the word delimiters. It can be understood as being roughly
2071
+ equivalent to:
2072
+
2073
+ split(" ", q/STRING/);
2074
+
2075
+ the differences being that it only splits on ASCII whitespace,
2076
+ generates a real list at compile time, and
2077
+ in scalar context it returns the last element in the list. So
2078
+ this expression:
2079
+
2080
+ qw(foo bar baz)
2081
+
2082
+ is semantically equivalent to the list:
2083
+
2084
+ "foo", "bar", "baz"
2085
+
2086
+ Some frequently seen examples:
2087
+
2088
+ use POSIX qw( setlocale localeconv )
2089
+ @EXPORT = qw( foo bar baz );
2090
+
2091
+ A common mistake is to try to separate the words with commas or to
2092
+ put comments into a multi-line C<qw>-string. For this reason, the
2093
+ S<C<use warnings>> pragma and the B<-w> switch (that is, the C<$^W> variable)
2094
+ produces warnings if the I<STRING> contains the C<","> or the C<"#"> character.
2095
+
2096
+ =back
2097
+
1917
2098
=head3 Regexp Quote-Like Operators
1918
2099
X<operator, regexp>
1919
2100
@@ -2458,187 +2639,6 @@ producing a warning if warnings are enabled.
2458
2639
2459
2640
=back
2460
2641
2461
- =head3 Simpler Quote-Like Operators
2462
- X<operator, quote-like>
2463
-
2464
- =over 4
2465
-
2466
- =item C<q/I<STRING>/>
2467
- X<q> X<quote, single> X<'> X<''>
2468
-
2469
- =item C<'I<STRING>'>
2470
-
2471
- A single-quoted, literal string. A backslash represents a backslash
2472
- unless followed by the delimiter or another backslash, in which case
2473
- the delimiter or backslash is interpolated.
2474
-
2475
- $foo = q!I said, "You said, 'She said it.'"!;
2476
- $bar = q('This is it.');
2477
- $baz = '\n'; # a two-character string
2478
-
2479
- =item C<qq/I<STRING>/>
2480
- X<qq> X<quote, double> X<"> X<"">
2481
-
2482
- =item C<"I<STRING>">
2483
-
2484
- A double-quoted, interpolated string.
2485
-
2486
- $_ .= qq
2487
- (*** The previous line contains the naughty word "$1".\n)
2488
- if /\b(tcl|java|python)\b/i; # :-)
2489
- $baz = "\n"; # a one-character string
2490
-
2491
- =item C<qx/I<STRING>/>
2492
- X<qx> X<`> X<``> X<backtick>
2493
-
2494
- =item C<`I<STRING>`>
2495
-
2496
- A string which is (possibly) interpolated and then executed as a
2497
- system command, via F</bin/sh> or its equivalent if required. Shell
2498
- wildcards, pipes, and redirections will be honored. Similarly to
2499
- C<system>, if the string contains no shell metacharacters then it will
2500
- be executed directly. The collected standard output of the command is
2501
- returned; standard error is unaffected. In scalar context, it comes
2502
- back as a single (potentially multi-line) string, or C<undef> if the
2503
- shell (or command) could not be started. In list context, returns a
2504
- list of lines (however you've defined lines with C<$/> or
2505
- C<$INPUT_RECORD_SEPARATOR>), or an empty list if the shell (or command)
2506
- could not be started.
2507
-
2508
- print qx/date/; # prints "Sun Jan 28 06:16:19 CST 2024"
2509
-
2510
- Because backticks do not affect standard error, use shell file descriptor
2511
- syntax (assuming the shell supports this) if you care to address this.
2512
- To capture a command's STDERR and STDOUT together:
2513
-
2514
- $output = `cmd 2>&1`;
2515
-
2516
- To capture a command's STDOUT but discard its STDERR:
2517
-
2518
- $output = `cmd 2>/dev/null`;
2519
-
2520
- To capture a command's STDERR but discard its STDOUT (ordering is
2521
- important here):
2522
-
2523
- $output = `cmd 2>&1 1>/dev/null`;
2524
-
2525
- To exchange a command's STDOUT and STDERR in order to capture the STDERR
2526
- but leave its STDOUT to come out the old STDERR:
2527
-
2528
- $output = `cmd 3>&1 1>&2 2>&3 3>&-`;
2529
-
2530
- To read both a command's STDOUT and its STDERR separately, it's easiest
2531
- to redirect them separately to files, and then read from those files
2532
- when the program is done:
2533
-
2534
- system("program args 1>program.stdout 2>program.stderr");
2535
-
2536
- The STDIN filehandle used by the command is inherited from Perl's STDIN.
2537
- For example:
2538
-
2539
- open(SPLAT, "stuff") || die "can't open stuff: $!";
2540
- open(STDIN, "<&SPLAT") || die "can't dupe SPLAT: $!";
2541
- print STDOUT `sort`;
2542
-
2543
- will print the sorted contents of the file named F<"stuff">.
2544
-
2545
- Using single-quote as a delimiter protects the command from Perl's
2546
- double-quote interpolation, passing it on to the shell instead:
2547
-
2548
- $perl_info = qx(ps $$); # that's Perl's $$
2549
- $shell_info = qx'ps $$'; # that's the new shell's $$
2550
-
2551
- How that string gets evaluated is entirely subject to the command
2552
- interpreter on your system. On most platforms, you will have to protect
2553
- shell metacharacters if you want them treated literally. This is in
2554
- practice difficult to do, as it's unclear how to escape which characters.
2555
- See L<perlsec> for a clean and safe example of a manual C<fork()> and C<exec()>
2556
- to emulate backticks safely.
2557
-
2558
- On some platforms (notably DOS-like ones), the shell may not be
2559
- capable of dealing with multiline commands, so putting newlines in
2560
- the string may not get you what you want. You may be able to evaluate
2561
- multiple commands in a single line by separating them with the command
2562
- separator character, if your shell supports that (for example, C<;> on
2563
- many Unix shells and C<&> on the Windows NT C<cmd> shell).
2564
-
2565
- Perl will attempt to flush all files opened for
2566
- output before starting the child process, but this may not be supported
2567
- on some platforms (see L<perlport>). To be safe, you may need to set
2568
- C<$|> (C<$AUTOFLUSH> in C<L<English>>) or call the C<autoflush()> method of
2569
- C<L<IO::Handle>> on any open handles.
2570
-
2571
- Beware that some command shells may place restrictions on the length
2572
- of the command line. You must ensure your strings don't exceed this
2573
- limit after any necessary interpolations. See the platform-specific
2574
- release notes for more details about your particular environment.
2575
-
2576
- Using this operator can lead to programs that are difficult to port,
2577
- because the shell commands called vary between systems, and may in
2578
- fact not be present at all. As one example, the C<type> command under
2579
- the POSIX shell is very different from the C<type> command under DOS.
2580
- That doesn't mean you should go out of your way to avoid backticks
2581
- when they're the right way to get something done. Perl was made to be
2582
- a glue language, and one of the things it glues together is commands.
2583
- Just understand what you're getting yourself into.
2584
-
2585
- Like C<system>, backticks put the child process exit code in C<$?>.
2586
- If you'd like to manually inspect failure, you can check all possible
2587
- failure modes by inspecting C<$?> like this:
2588
-
2589
- if ($? == -1) {
2590
- print "failed to execute: $!\n";
2591
- }
2592
- elsif ($? & 127) {
2593
- printf "child died with signal %d, %s coredump\n",
2594
- ($? & 127), ($? & 128) ? 'with' : 'without';
2595
- }
2596
- else {
2597
- printf "child exited with value %d\n", $? >> 8;
2598
- }
2599
-
2600
- Use the L<open> pragma to control the I/O layers used when reading the
2601
- output of the command, for example:
2602
-
2603
- use open IN => ":encoding(UTF-8)";
2604
- my $x = `cmd-producing-utf-8`;
2605
-
2606
- C<qx//> can also be called like a function with L<perlfunc/readpipe>.
2607
-
2608
- See L</"I/O Operators"> for more discussion.
2609
-
2610
- =item C<qw/I<STRING>/>
2611
- X<qw> X<quote, list> X<quote, words>
2612
-
2613
- Evaluates to a list of the words extracted out of I<STRING>, using embedded
2614
- whitespace as the word delimiters. It can be understood as being roughly
2615
- equivalent to:
2616
-
2617
- split(" ", q/STRING/);
2618
-
2619
- the differences being that it only splits on ASCII whitespace,
2620
- generates a real list at compile time, and
2621
- in scalar context it returns the last element in the list. So
2622
- this expression:
2623
-
2624
- qw(foo bar baz)
2625
-
2626
- is semantically equivalent to the list:
2627
-
2628
- "foo", "bar", "baz"
2629
-
2630
- Some frequently seen examples:
2631
-
2632
- use POSIX qw( setlocale localeconv )
2633
- @EXPORT = qw( foo bar baz );
2634
-
2635
- A common mistake is to try to separate the words with commas or to
2636
- put comments into a multi-line C<qw>-string. For this reason, the
2637
- S<C<use warnings>> pragma and the B<-w> switch (that is, the C<$^W> variable)
2638
- produces warnings if the I<STRING> contains the C<","> or the C<"#"> character.
2639
-
2640
- =back
2641
-
2642
2642
=head3 Transliteration Quote-Like Operators
2643
2643
2644
2644
=over
0 commit comments