{
    "mode": "perldoc",
    "parameter": "IV",
    "section": "-q",
    "url": "https://www.chedong.com/phpMan.php/perldoc/IV/json",
    "generated": "2026-06-11T00:31:47Z",
    "sections": {
        "Found in /usr/share/perl/5.34/pod/perlfaq3.pod": {
            "content": "How can I use Perl interactively?\nThe typical approach uses the Perl debugger, described in the",
            "subsections": [
                {
                    "name": "perldebug",
                    "content": "perl -de 42\n\nNow just type in any legal Perl code, and it will be immediately\nevaluated. You can also examine the symbol table, get stack backtraces,\ncheck variable values, set breakpoints, and other operations typically\nfound in symbolic debuggers.\n\nYou can also use Devel::REPL which is an interactive shell for Perl,\ncommonly known as a REPL - Read, Evaluate, Print, Loop. It provides\nvarious handy features.\n"
                }
            ]
        },
        "Found in /usr/share/perl/5.34/pod/perlfaq4.pod": {
            "content": "How do I remove consecutive pairs of characters?\n(contributed by brian d foy)\n\nYou can use the substitution operator to find pairs of characters (or\nruns of characters) and replace them with a single instance. In this\nsubstitution, we find a character in \"(.)\". The memory parentheses store\nthe matched character in the back-reference \"\\g1\" and we use that to\nrequire that the same thing immediately follow it. We replace that part\nof the string with the character in $1.\n\ns/(.)\\g1/$1/g;\n\nWe can also use the transliteration operator, \"tr///\". In this example,\nthe search list side of our \"tr///\" contains nothing, but the \"c\" option\ncomplements that so it contains everything. The replacement list also\ncontains nothing, so the transliteration is almost a no-op since it\nwon't do any replacements (or more exactly, replace the character with\nitself). However, the \"s\" option squashes duplicated and consecutive\ncharacters in the string so a character does not show up next to itself\n\nmy $str = 'Haarlem';   # in the Netherlands\n$str =~ tr///cs;       # Now Harlem, like in New York\n\nHow can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays?\nUsually a hash ref, perhaps like this:\n\n$record = {\nNAME   => \"Jason\",\nEMPNO  => 132,\nTITLE  => \"deputy peon\",\nAGE    => 23,\nSALARY => 37000,\nPALS   => [ \"Norbert\", \"Rhys\", \"Phineas\"],\n};\n\nReferences are documented in perlref and perlreftut. Examples of complex\ndata structures are given in perldsc and perllol. Examples of structures\nand object-oriented classes are in perlootut.\n\nHow do I print out or copy a recursive data structure?\nThe Data::Dumper module on CPAN (or the 5.005 release of Perl) is great\nfor printing out data structures. The Storable module on CPAN (or the\n5.8 release of Perl), provides a function called \"dclone\" that\nrecursively copies its argument.\n\nuse Storable qw(dclone);\n$r2 = dclone($r1);\n\nWhere $r1 can be a reference to any kind of data structure you'd like.\nIt will be deeply copied. Because \"dclone\" takes and returns references,\nyou'd have to add extra punctuation if you had a hash of arrays that you\nwanted to copy.\n\n%newhash = %{ dclone(\\%oldhash) };\n",
            "subsections": []
        },
        "Found in /usr/share/perl/5.34/pod/perlfaq6.pod": {
            "content": "How do I substitute case-insensitively on the LHS while preserving case on the RHS?\nHere's a lovely Perlish solution by Larry Rosler. It exploits properties\nof bitwise xor on ASCII strings.\n\n$= \"this is a TEsT case\";\n\n$old = 'test';\n$new = 'success';\n\ns{(\\Q$old\\E)}\n{ uc $new | (uc $1 ^ $1) .\n(uc(substr $1, -1) ^ substr $1, -1) x\n(length($new) - length $1)\n}egi;\n\nprint;\n\nAnd here it is as a subroutine, modeled after the above:\n\nsub preservecase {\nmy ($old, $new) = @;\nmy $mask = uc $old ^ $old;\n\nuc $new | $mask .\nsubstr($mask, -1) x (length($new) - length($old))\n}\n\n$string = \"this is a TEsT case\";\n$string =~ s/(test)/preservecase($1, \"success\")/egi;\nprint \"$string\\n\";\n\nThis prints:\n\nthis is a SUcCESS case\n\nAs an alternative, to keep the case of the replacement word if it is\nlonger than the original, you can use this code, by Jeff Pinyan:\n\nsub preservecase {\nmy ($from, $to) = @;\nmy ($lf, $lt) = map length, @;\n\nif ($lt < $lf) { $from = substr $from, 0, $lt }\nelse { $from .= substr $to, $lf }\n\nreturn uc $to | ($from ^ uc $from);\n}\n\nThis changes the sentence to \"this is a SUcCess case.\"\n\nJust to show that C programmers can write C in any programming language,\nif you prefer a more C-like solution, the following script makes the\nsubstitution have the same case, letter by letter, as the original. (It\nalso happens to run about 240% slower than the Perlish solution runs.)\nIf the substitution has more characters than the string being\nsubstituted, the case of the last character is used for the rest of the\nsubstitution.\n\n# Original by Nathan Torkington, massaged by Jeffrey Friedl\n#\nsub preservecase\n{\nmy ($old, $new) = @;\nmy $state = 0; # 0 = no change; 1 = lc; 2 = uc\nmy ($i, $oldlen, $newlen, $c) = (0, length($old), length($new));\nmy $len = $oldlen < $newlen ? $oldlen : $newlen;\n\nfor ($i = 0; $i < $len; $i++) {\nif ($c = substr($old, $i, 1), $c =~ /[\\W\\d]/) {\n$state = 0;\n} elsif (lc $c eq $c) {\nsubstr($new, $i, 1) = lc(substr($new, $i, 1));\n$state = 1;\n} else {\nsubstr($new, $i, 1) = uc(substr($new, $i, 1));\n$state = 2;\n}\n}\n# finish up with any remaining new (for when new is longer than old)\nif ($newlen > $oldlen) {\nif ($state == 1) {\nsubstr($new, $oldlen) = lc(substr($new, $oldlen));\n} elsif ($state == 2) {\nsubstr($new, $oldlen) = uc(substr($new, $oldlen));\n}\n}\nreturn $new;\n}\n",
            "subsections": []
        },
        "Found in /usr/share/perl/5.34/pod/perlfaq8.pod": {
            "content": "How do I find out if I'm running interactively or not?\n(contributed by brian d foy)\n\nThis is a difficult question to answer, and the best answer is only a\nguess.\n\nWhat do you really want to know? If you merely want to know if one of\nyour filehandles is connected to a terminal, you can try the \"-t\" file\ntest:\n\nif( -t STDOUT ) {\nprint \"I'm connected to a terminal!\\n\";\n}\n\nHowever, you might be out of luck if you expect that means there is a\nreal person on the other side. With the Expect module, another program\ncan pretend to be a person. The program might even come close to passing\nthe Turing test.\n\nThe IO::Interactive module does the best it can to give you an answer.\nIts \"isinteractive\" function returns an output filehandle; that\nfilehandle points to standard output if the module thinks the session is\ninteractive. Otherwise, the filehandle is a null handle that simply\ndiscards the output:\n\nuse IO::Interactive;\n\nprint { isinteractive } \"I might go to standard output!\\n\";\n\nThis still doesn't guarantee that a real person is answering your\nprompts or reading your output.\n\nIf you want to know how to handle automated testing for your\ndistribution, you can check the environment. The CPAN Testers, for\ninstance, set the value of \"AUTOMATEDTESTING\":\n\nunless( $ENV{AUTOMATEDTESTING} ) {\nprint \"Hello interactive tester!\\n\";\n}\n\nHow do I add the directory my program lives in to the module/library search path?\n(contributed by brian d foy)\n\nIf you know the directory already, you can add it to @INC as you would\nfor any other directory. You might \"use lib\" if you know the directory\nat compile time:\n\nuse lib $directory;\n\nThe trick in this task is to find the directory. Before your script does\nanything else (such as a \"chdir\"), you can get the current working\ndirectory with the \"Cwd\" module, which comes with Perl:\n\nBEGIN {\nuse Cwd;\nour $directory = cwd;\n}\n\nuse lib $directory;\n\nYou can do a similar thing with the value of $0, which holds the script\nname. That might hold a relative path, but \"rel2abs\" can turn it into an\nabsolute path. Once you have the\n\nBEGIN {\nuse File::Spec::Functions qw(rel2abs);\nuse File::Basename qw(dirname);\n\nmy $path   = rel2abs( $0 );\nour $directory = dirname( $path );\n}\n\nuse lib $directory;\n\nThe FindBin module, which comes with Perl, might work. It finds the\ndirectory of the currently running script and puts it in $Bin, which you\ncan then use to construct the right library path:\n\nuse FindBin qw($Bin);\n\nYou can also use local::lib to do much of the same thing. Install\nmodules using local::lib's settings then use the module in your program:\n\nuse local::lib; # sets up a local lib at ~/perl5\n\nSee the local::lib documentation for more details.\n",
            "subsections": []
        }
    },
    "flags": [],
    "examples": [],
    "see_also": []
}