--- name: Where a line is not a file --- ```{index} single: ipps; limits single: ipps; match variables single: ipps; local ``` # Where a line is not a file Each line you enter is compiled and run by itself, and `ipps` carries over what the line declared. A few things in Perl last exactly as long as the code that surrounds them, and at the prompt that is one line. ## The match variables `$1`, `$2` and their relatives belong to the line that matched: ```text ipps> "2026-10-13" =~ /(\d+)-(\d+)/; "$1 / $2" '2026 / 10' ipps> $1 undef ``` Take what you need into variables in the same line: ```text ipps> my ($year, $month) = "2026-10-13" =~ /(\d+)-(\d+)/; ipps> "$year / $month" '2026 / 10' ``` ## `local` `local` at the top of a line lasts to the end of that line: ```text ipps> our $level = 1; local $level = 2; $level 2 ipps> $level 1 ``` Inside a sub or a block it behaves as everywhere. ## Lexical subs A sub declared with `my sub` exists for its line only: ```text ipps> my sub helper { 42 } helper() 42 ipps> helper() Undefined subroutine &main::helper called at (ipps:2) line 1. ``` An ordinary `sub` stays. ## Packages A `package` statement holds for the following lines, as it does in a file, and `my` variables stay visible across it: ```text ipps> package Counter; my $count = 0; sub bump { ++$count } ipps> bump(); bump() 2 ipps> __PACKAGE__ 'Counter' ipps> package main; ipps> Counter::bump() 3 ``` ## Unfinished input is compiled twice While you are typing a statement over several lines, `ipps` finds out that it is unfinished by compiling what there is. A `use` or a `BEGIN` block in the first lines has run by then, and runs again when the whole text is compiled. For nearly every module that makes no difference. Where it would, put the `use` on a line of its own. ## Completion runs nothing Tab looks at the variables, packages and files that exist. It does not call your code, so it cannot know what a call returns. After ```perl make_thing()-> ``` there is nothing to offer. Put the result in a variable and complete on that. ## Running programs `ipps` is a shell for Perl: a line at its prompt is Perl, and a bare `ls` is a Perl word, not a command. To hand a line to your own shell, start it with `!` and a space: ```text ipps> ! ls -l Cargo.toml -rw-r--r-- 1 rj rj 8046 Oct 12 09:18 Cargo.toml ipps> ! ls Cargo.* | wc -l 2 ``` The line goes to the shell as it stands, with its globs and pipes, and runs on your terminal. Nothing of it is Perl and its output is not kept. An exit status other than 0 is reported: ```text ipps> ! ll Cargo.toml /bin/bash: line 1: ll: command not found ipps: exit status 127 ``` That `ll` is an alias, and the command runs under `$SHELL -c`, which does not read the start file of an interactive shell. The `shell` setting says what to run instead. For the aliases of bash: ```text ipps> :set shell bash -ic ipps> ! ll Cargo.toml -rw-r--r-- 1 rj rj 8046 Oct 12 09:18 Cargo.toml ``` The space after `!` matters. Without it the line is Perl's negation: ```text ipps> my $done = 0; ipps> !$done true ``` When you want a command's output in a variable, that is Perl, and Perl starts programs as it always has: ```text ipps> my @files = `ls Cargo.*` ("Cargo.lock\n", "Cargo.toml\n") ipps> system "echo", "run by system" run by system 0 ipps> open my $pipe, "-|", "echo", "read from a pipe"; scalar readline $pipe "read from a pipe\n" ``` The `0` after the first line is what `system` returned. Listing files needs no program at all: ```text ipps> my @manifests = glob "Cargo.*" ('Cargo.lock', 'Cargo.toml') ```