如何在Windows 7/10中用Perl6(Rakudo Star 2019.03)创建弹窗?
Hey there! Since you're running Windows 7/10 with Rakudo Star 2019.03, here are a few straightforward ways to create pop-up messages that behave like Linux's send-notify or Windows' built-in msg command—no network required:
Option 1: Use Windows' Built-in Commands (No Extra Modules Needed)
These methods leverage tools already present on your system, so you don't need to install anything extra.
Using the msg Command
The msg utility is perfect for sending quick messages to the current user's session. It works on both Windows 7 and 10:
my $notification-text = "Hey! This is a pop-up from Perl 6!"; # Send the message to the current user run 'msg', '%username%', $notification-text;
The %username% variable automatically targets your active user account, so the pop-up will appear directly on your desktop.
Windows 10+: Toast Notifications via PowerShell
If you're on Windows 10, you can use PowerShell's BurntToast module (pre-installed in newer builds) to send notifications to the system tray:
my $title = "Perl 6 Alert"; my $message = "This is a toast notification from your Perl 6 script!"; run 'powershell', '-Command', "New-BurntToastNotification -Text '$title', '$message'";
Windows 7-Compatible PowerShell Pop-up
For Windows 7, use this PowerShell command to trigger a classic message box:
my $message = "Windows 7-friendly pop-up from Perl 6"; run 'powershell', '-Command', "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.MessageBox]::Show('$message')";
Option 2: Call Windows API Directly with NativeCall
For maximum control and compatibility across both Windows 7 and 10, use Perl 6's NativeCall feature to invoke the Windows MessageBox function directly from user32.dll. This is lightweight and doesn't rely on external tools:
use NativeCall; # Declare the Windows API function sub MessageBoxW( int32, # Parent window handle (0 = use desktop) Str, # Message text Str, # Window caption uint32 # Message box style (0 = OK button only) ) returns int32 is native('user32.dll') { * } # Show the pop-up MessageBoxW(0, "Hello from Perl 6 NativeCall!", "Perl 6 Notification", 0);
You can customize the style by changing the last parameter—for example, use 1 to add an OK/Cancel button, or 48 for a warning icon.
Quick Notes
- All these methods work locally (no network required), just like the
msgcommand orsend-notify. - The
NativeCallapproach is the most reliable for cross-Windows-version support if you need consistency between 7 and 10.
内容的提问来源于stack exchange,提问作者Todd

