phpman > perldoc > IV(0xbe860)

Markdown | JSON | MCP    

Found in /usr/share/perl/5.34/pod/perlfaq3.pod
  How can I use Perl interactively?
    The typical approach uses the Perl debugger, described in the
    perldebug(1) manpage, on an "empty" program, like this:

        perl -de 42

    Now just type in any legal Perl code, and it will be immediately
    evaluated. You can also examine the symbol table, get stack backtraces,
    check variable values, set breakpoints, and other operations typically
    found in symbolic debuggers.

    You can also use Devel::REPL which is an interactive shell for Perl,
    commonly known as a REPL - Read, Evaluate, Print, Loop. It provides
    various handy features.

Found in /usr/share/perl/5.34/pod/perlfaq4.pod
  How do I remove consecutive pairs of characters?
    (contributed by brian d foy)

    You can use the substitution operator to find pairs of characters (or
    runs of characters) and replace them with a single instance. In this
    substitution, we find a character in "(.)". The memory parentheses store
    the matched character in the back-reference "\g1" and we use that to
    require that the same thing immediately follow it. We replace that part
    of the string with the character in $1.

        s/(.)\g1/$1/g;

    We can also use the transliteration operator, "tr///". In this example,
    the search list side of our "tr///" contains nothing, but the "c" option
    complements that so it contains everything. The replacement list also
    contains nothing, so the transliteration is almost a no-op since it
    won't do any replacements (or more exactly, replace the character with
    itself). However, the "s" option squashes duplicated and consecutive
    characters in the string so a character does not show up next to itself

        my $str = 'Haarlem';   # in the Netherlands
        $str =~ tr///cs;       # Now Harlem, like in New York

  How can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays?
    Usually a hash ref, perhaps like this:

        $record = {
            NAME   => "Jason",
            EMPNO  => 132,
            TITLE  => "deputy peon",
            AGE    => 23,
            SALARY => 37_000,
            PALS   => [ "Norbert", "Rhys", "Phineas"],
        };

    References are documented in perlref and perlreftut. Examples of complex
    data structures are given in perldsc and perllol. Examples of structures
    and object-oriented classes are in perlootut.

  How do I print out or copy a recursive data structure?
    The Data::Dumper module on CPAN (or the 5.005 release of Perl) is great
    for printing out data structures. The Storable module on CPAN (or the
    5.8 release of Perl), provides a function called "dclone" that
    recursively copies its argument.

        use Storable qw(dclone);
        $r2 = dclone($r1);

    Where $r1 can be a reference to any kind of data structure you'd like.
    It will be deeply copied. Because "dclone" takes and returns references,
    you'd have to add extra punctuation if you had a hash of arrays that you
    wanted to copy.

        %newhash = %{ dclone(\%oldhash) };

Found in /usr/share/perl/5.34/pod/perlfaq6.pod
  How do I substitute case-insensitively on the LHS while preserving case on the RHS?
    Here's a lovely Perlish solution by Larry Rosler. It exploits properties
    of bitwise xor on ASCII strings.

        $_= "this is a TEsT case";

        $old = 'test';
        $new = 'success';

        s{(\Q$old\E)}
        { uc $new | (uc $1 ^ $1) .
            (uc(substr $1, -1) ^ substr $1, -1) x
            (length($new) - length $1)
        }egi;

        print;

    And here it is as a subroutine, modeled after the above:

        sub preserve_case {
            my ($old, $new) = @_;
            my $mask = uc $old ^ $old;

            uc $new | $mask .
                substr($mask, -1) x (length($new) - length($old))
        }

        $string = "this is a TEsT case";
        $string =~ s/(test)/preserve_case($1, "success")/egi;
        print "$string\n";

    This prints:

        this is a SUcCESS case

    As an alternative, to keep the case of the replacement word if it is
    longer than the original, you can use this code, by Jeff Pinyan:

        sub preserve_case {
            my ($from, $to) = @_;
            my ($lf, $lt) = map length, @_;

            if ($lt < $lf) { $from = substr $from, 0, $lt }
            else { $from .= substr $to, $lf }

            return uc $to | ($from ^ uc $from);
        }

    This changes the sentence to "this is a SUcCess case."

    Just to show that C programmers can write C in any programming language,
    if you prefer a more C-like solution, the following script makes the
    substitution have the same case, letter by letter, as the original. (It
    also happens to run about 240% slower than the Perlish solution runs.)
    If the substitution has more characters than the string being
    substituted, the case of the last character is used for the rest of the
    substitution.

        # Original by Nathan Torkington, massaged by Jeffrey Friedl
        #
        sub preserve_case
        {
            my ($old, $new) = @_;
            my $state = 0; # 0 = no change; 1 = lc; 2 = uc
            my ($i, $oldlen, $newlen, $c) = (0, length($old), length($new));
            my $len = $oldlen < $newlen ? $oldlen : $newlen;

            for ($i = 0; $i < $len; $i++) {
                if ($c = substr($old, $i, 1), $c =~ /[\W\d_]/) {
                    $state = 0;
                } elsif (lc $c eq $c) {
                    substr($new, $i, 1) = lc(substr($new, $i, 1));
                    $state = 1;
                } else {
                    substr($new, $i, 1) = uc(substr($new, $i, 1));
                    $state = 2;
                }
            }
            # finish up with any remaining new (for when new is longer than old)
            if ($newlen > $oldlen) {
                if ($state == 1) {
                    substr($new, $oldlen) = lc(substr($new, $oldlen));
                } elsif ($state == 2) {
                    substr($new, $oldlen) = uc(substr($new, $oldlen));
                }
            }
            return $new;
        }

Found in /usr/share/perl/5.34/pod/perlfaq8.pod
  How do I find out if I'm running interactively or not?
    (contributed by brian d foy)

    This is a difficult question to answer, and the best answer is only a
    guess.

    What do you really want to know? If you merely want to know if one of
    your filehandles is connected to a terminal, you can try the "-t" file
    test:

        if( -t STDOUT ) {
            print "I'm connected to a terminal!\n";
        }

    However, you might be out of luck if you expect that means there is a
    real person on the other side. With the Expect module, another program
    can pretend to be a person. The program might even come close to passing
    the Turing test.

    The IO::Interactive module does the best it can to give you an answer.
    Its "is_interactive" function returns an output filehandle; that
    filehandle points to standard output if the module thinks the session is
    interactive. Otherwise, the filehandle is a null handle that simply
    discards the output:

        use IO::Interactive;

        print { is_interactive } "I might go to standard output!\n";

    This still doesn't guarantee that a real person is answering your
    prompts or reading your output.

    If you want to know how to handle automated testing for your
    distribution, you can check the environment. The CPAN Testers, for
    instance, set the value of "AUTOMATED_TESTING":

        unless( $ENV{AUTOMATED_TESTING} ) {
            print "Hello interactive tester!\n";
        }

  How do I add the directory my program lives in to the module/library search path?
    (contributed by brian d foy)

    If you know the directory already, you can add it to @INC as you would
    for any other directory. You might "use lib" if you know the directory
    at compile time:

        use lib $directory;

    The trick in this task is to find the directory. Before your script does
    anything else (such as a "chdir"), you can get the current working
    directory with the "Cwd" module, which comes with Perl:

        BEGIN {
            use Cwd;
            our $directory = cwd;
        }

        use lib $directory;

    You can do a similar thing with the value of $0, which holds the script
    name. That might hold a relative path, but "rel2abs" can turn it into an
    absolute path. Once you have the

        BEGIN {
            use File::Spec::Functions qw(rel2abs);
            use File::Basename qw(dirname);

            my $path   = rel2abs( $0 );
            our $directory = dirname( $path );
        }

        use lib $directory;

    The FindBin module, which comes with Perl, might work. It finds the
    directory of the currently running script and puts it in $Bin, which you
    can then use to construct the right library path:

        use FindBin qw($Bin);

    You can also use local::lib to do much of the same thing. Install
    modules using local::lib's settings then use the module in your program:

         use local::lib; # sets up a local lib at ~/perl5

    See the local::lib documentation for more details.

IV(0xbe860)
Found in /usr/share/perl/5.34/pod/perlfaq3.pod
perldebug(1) manpage, on an "empty" program, like this:
Found in /usr/share/perl/5.34/pod/perlfaq4.pod Found in /usr/share/perl/5.34/pod/perlfaq6.pod Found in /usr/share/perl/5.34/pod/perlfaq8.pod

Generated by phpman v3.7.12 Author: Che Dong Under GNU General Public License
2026-06-13 12:32 @216.73.216.28
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 TransitionalValid CSS!

^_back to top