You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Perl自定义函数设为内置函数?附实践疑问

First off, great job getting started with Perl packages for code reuse—coming from C++ it takes a minute to wrap your head around Perl's mix of OO and procedural styles, but you're already on the right track!

Yes, absolutely possible to make those print functions callable directly without instantiating an object. Here's how to adjust your Printer package, plus fix a small gotcha in your original code:

Step 1: Use Perl's Built-in Exporter Module

Perl has a core module called Exporter that lets you share package-level functions with the scripts that use them. This is exactly what we need to make println (and a renamed version of print, to avoid conflicts) act like "built-in" functions.

Step 2: Adjust Your Function Logic

In your original code, your print and println skipped the first element of @_ because when called via an object ($printer->print(@_)), the first argument is always the object itself. For direct calls, the entire @_ will be the array you pass in—so we can remove that skip. Also, naming your function print will override Perl's native print function, which is likely to cause bugs later, so I'll rename it to print_array to avoid conflicts.

Modified Printer.pm Code

use Modern::Perl;
use Exporter qw(import);

# Define which functions to export to the caller's namespace
our @EXPORT = qw(print_array println_array);

package Printer;

sub println_array {
    my @userArray = @_;
    # No more skipping the first element—@_ is the full stack array
    foreach my $elem (@userArray) {
        say $elem; # Scalar is unnecessary here for most values
    }
}

sub print_array {
    my @userArray = @_;
    # Cleaner than looping: join elements with spaces and print
    print join(' ', @userArray), " ";
}

1; # Required for all Perl packages to load correctly

Step 3: Update Your Stack Script

You no longer need to create a Printer object—just import the functions and call them directly:

use lib "."; # Add current directory to module search path
use Printer; # Imports print_array and println_array automatically

# Your stack logic
my @stack = (1, 8, 14, 45);
push(@stack, 69);
push(@stack, 55);
push(@stack, 65536);
push(@stack, 4294967296);

# Call the direct functions
print "Print without newline: ";
print_array(@stack);

print "\nPrint with newline:\n";
println_array(@stack);

# POPPING (LIFO)
pop(@stack);
pop(@stack);
pop(@stack);

print "\nAfter popping three elements: ";
print_array(@stack);

Optional: Explicit Import

If you prefer to let users choose which functions to import (instead of auto-importing), replace @EXPORT with @EXPORT_OK in Printer.pm:

our @EXPORT_OK = qw(print_array println_array);

Then update the use line in your script to specify which functions to load:

use Printer qw(print_array println_array);

内容的提问来源于stack exchange,提问作者aspiringsoftwaredev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 20:57:53