Net::Cmd(3perl)

✨ NAME ✨

Net::Cmd β€” the Network Command class, aka the glow-up backbone that FTP, SMTP and all their protocol besties inherit from πŸ’… (it's a 3perl page, from the Perl Programmers Reference Guide, and yes we're getting into it)

🌸 SYNOPSIS 🌸

use Net::Cmd;

@ISA = qw(Net::Cmd);

Okay bestie, look at that. Two lines. It's giving minimalist icon. πŸŽ€

πŸ¦‹ DESCRIPTION πŸ¦‹

Net::Cmd is basically a whole lil collection of methods that a sub-class of IO::Socket::INET can inherit. The way that it works? These methods implement everything you need for a command-based protocol β€” think FTP, think SMTP, think "I send a command, the server talks back" energy. No cap. πŸ”₯

Now listen, real talk: if your sub-class doesn't also derive from IO::Socket::INET or something similar (like IO::Socket::IP, IO::Socket::INET6 or IO::Socket::SSL β€” yes those all count), then you have to provide the following methods yourself, by other means:

  • close()
  • timeout()

That's the vibe check. If you skip it, don't come crying to me. 😭


πŸ‘‘ Public Methods πŸ‘‘

These are the ones that give you, the user, an actual user interface to the Net::Cmd object. Main character methods only. ✨

  • debug($level) β€” Sets the level of debug info for this object. If you don't pass $level, you just get the current state handed back to you. If you do pass it, the state changes to $level and the previous state gets returned. Different packages may implement different levels, but any non-zero value means copies of every command and every response also get yeeted to STDERR. And if $level is undef? Then the debug level gets set to the default debug level for the class. You can also call this as a static method to set/get the default debug level for a given class. πŸ’…
  • message() β€” Returns the text message from the last command. In scalar context you get one single string; in list context you get each line as its own separate element. (Peep "PSEUDO RESPONSES" below for the tea. πŸ“)
  • code() β€” Returns the 3-digit code from the last command. If a command is still pending, you get 0. (Also see "PSEUDO RESPONSES" below. πŸ‘€)
  • ok() β€” Returns non-zero if the last code value was greater than zero and less than 400. This holds true for most command servers. Servers where it doesn't hold? They get to override this method. Their problem, honestly.
  • status() β€” Returns the most significant digit of the current status code. If a command is pending, CMD_PENDING is what you get back.
  • datasend($data) β€” Sends data to the remote server, converting LF to CRLF. Any line starting with a . gets prefixed with another .. Pro tip: $data can be an array or a reference to an array. And heads up β€” the $data you pass in must be encoded by the caller to octets of whatever encoding is required, e.g. by using the Encode module's encode() function. Don't skip that step bestie.
  • dataend() β€” Ends the sending of data to the remote server. How? First it makes sure the data already sent ends with CRLF, then it sends .CRLF to end the transmission. Once that data is sent, dataend calls response and returns true if response returns CMD_OK. Clean. Neat. Slay. 🌈

🧚 Protected Methods 🧚

These ones are NOT for you, bestie. They're not meant to be called by the user β€” they're meant to be used or over-ridden by a sub-class of Net::Cmd. It's giving "staff only" door. πŸ™ˆ

  • debug_print($dir, $text) β€” Prints debugging information. $dir denotes the direction, where true means data being sent to the server. It calls debug_text before printing to STDERR.
  • debug_text($dir, $text) β€” This method is called to print debugging information. $text is the text being sent. The method should return the text to be printed. This exists primarily for modules like FTP where passwords get sent but we absolutely do NOT want them showing up in the debug info. Privacy queen behavior honestly. πŸ’œ
  • command($cmd[, $args, ... ]) β€” Sends a command to the command server. All arguments first get joined with a space character, then CRLF is appended, and that string is what gets sent to the command server. Returns undef upon failure. 😭
  • unsupported() β€” Sets the status code to 580 and the response text to 'Unsupported command'. Returns zero. Short. Sweet. Rejection message delivered. πŸ’€
  • response() β€” Obtains a response from the server. On success, the most significant digit of the status code is returned. On failure, timeout, etc., CMD_ERROR is returned.
  • parse_response($text) β€” This method is called by response as a method with one argument. It should return an array of 2 values: the 3-digit status code, and a flag which is true when this is part of a multi-line response and this line is not the last. Two values, no more, no less. πŸŽ€
  • getline() β€” Retrieves one line, delimited by CRLF, from the remote server. Returns undef upon failure.
⚠️ NOTE: If you do use this method for any reason, please remember to add some debug_print calls into your method. Do not forget. I will know. πŸ‘€
  • ungetline($text) β€” Ungets a line of text from the server. Putting it back, like a polite lil gremlin.
  • rawdatasend($data) β€” Sends data to the remote server without performing any conversions. $data is a scalar. And same as with datasend(), the $data you pass in must be encoded by the caller to octets of whatever encoding is required, e.g. by using the Encode module's encode() function.
  • read_until_dot() β€” Reads data from the remote server until a line consisting of a single .. Any lines starting with .. will have one of the .s removed. Returns a reference to a list containing the lines, or undef upon failure. πŸ“œ
  • tied_fh() β€” Returns a filehandle tied to the Net::Cmd object. After issuing a command, you may read from this filehandle using read() or <>. The filehandle will return EOF when the final dot is encountered. Similarly, you may write to the filehandle to send data to the server after issuing a command that expects data to be written. Check the Net::POP3 and Net::SMTP modules for examples of this β€” they're the blueprint. 🫢

πŸ’€ Pseudo Responses πŸ’€

Normally the values returned by message() and code() come straight from the remote server. But in a few circumstances, detailed below, Net::Cmd will return values that it set all by itself. You can alter this behavior by overriding DEF_REPLY_CODE() to specify a different default reply code, or by overriding one of the specific error handling methods below. Your house, your rules. πŸ’…

  • Initial value β€” Before any command has executed, or if an unexpected error occurs, code() will return "421" (temporary connection failure) and message() will return undef.
  • Connection closed β€” If the underlying IO::Handle is closed, or if there are any read or write failures, the file handle will be forced closed, and code() will return "421" (temporary connection failure) and message() will return "[$pkg] Connection closed" (where $pkg is the name of the class that subclassed Net::Cmd). The _set_status_closed() method can be overridden to set a different message (by calling set_status()) or otherwise trap this error, if you're feeling fancy.
  • Timeout β€” If there's a read or write timeout, code() will return "421" (temporary connection failure) and message() will return "[$pkg] Timeout" (where $pkg is the name of the class that subclassed Net::Cmd). The _set_status_timeout() method can be overridden to set a different message (by calling set_status()) or otherwise trap this error. The clock ran out bestie, it happens to everyone. 😀

🌟 EXPORTS 🌟

The following symbols are, or can be, exported by this module:

Default Exports

CMD_INFO, CMD_OK, CMD_MORE, CMD_REJECT, CMD_ERROR, CMD_PENDING.

(These correspond to possible results of response() and status().) The lil legend of the whole module, honestly. ⭐

Optional Exports

None. Nada. Nothing. πŸŽ€

Export Tags

None. Also nothing. 😌


πŸ› KNOWN BUGS πŸ›

See <https://rt.cpan.org/Dist/Display.html?Status=Active&Queue=libnet>. If you found a bug, that's the spot. Go, be the main character who reports it. 🫑


🫢 AUTHOR 🫢

Graham Barr <gbarr@pobox.com <mailto:gbarr@pobox.com>>.

Steve Hay <shay@cpan.org <mailto:shay@cpan.org>> is now maintaining libnet as of version 1.22_02. Passing the torch, iconic behavior. πŸ’œ


Copyright (C) 1995-2006 Graham Barr. All rights reserved.

Copyright (C) 2013-2016, 2020, 2022 Steve Hay. All rights reserved.


πŸ“œ LICENCE πŸ“œ

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself, i.e. under the terms of either the GNU General Public License or the Artistic License, as specified in the LICENCE file.


πŸŽ€ VERSION πŸŽ€

Version 3.15


πŸ—“οΈ DATE πŸ—“οΈ

20 March 2023


πŸ“– HISTORY πŸ“–

See the Changes file.


and that's the whole page bestie, no notes, go forth and use Net::Cmd responsibly πŸ’–βœ¨ This lil slice of the Perl Programmers Reference Guide is Net::Cmd(3perl), straight out of perl v5.44.0 with a date stamp of 2026-10-17 β€” she's serving documentation, she's serving legacy, she's serving iconic. Periodt. πŸ‘‘πŸŒˆ

πŸ“¦ where this page comes from
  • arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms