| Use Case | Command | Description |
|---|---|---|
| Get first character of a string | substr($string, 0, 1) | Access the first character of a string. |
| Replace part of a string | substr($string, 13, 4, "replacement") | Change a substring using the fourth argument. |
| Access global variable when lexical is in scope | $main::var | Explicitly reference the package variable. |
| Bring dynamic variable into lexical scope | our $var; | Use our() to alias a global variable. |
| Catch undefined variable accesses | use warnings FATAL => qw(uninitialized); | Promote warnings to errors for uninitialized variables. |
📂 Found in /usr/share/perl/5.34/pod/perlfaq4.pod
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";
📂 Found in /usr/share/perl/5.34/pod/perlfaq7.pod
If you know your package, you can just mention it explicitly, as in $Some_Pack::var. Note that the notation $::var is not the dynamic $var in the current package, but rather the one in the "main" package, as though you had written $main::var.
use vars '$var';
local $var = "global";
my $var = "lexical";
print "lexical is $var\n";
print "global is $main::var\n";
Alternatively you can use the compiler directive our() to bring a dynamic variable into the current lexical scope.
require 5.006; # our() did not exist before 5.6
use vars '$var';
local $var = "global";
my $var = "lexical";
print "lexical is $var\n";
{
our $var;
print "global is $var\n";
}
The AUTOLOAD method, discussed in "Autoloading" in perlsub lets you capture calls to undefined functions and methods.
When it comes to undefined variables that would trigger a warning under "use warnings", you can promote the warning to an error.
use warnings FATAL => qw(uninitialized);
Generated by phpman v4.10.0-7-g98e9fd5 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-09-17 14:17 @216.73.216.102
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)