method extension

Documentation for method extension assembled from the following types:

class IO::Path

From IO::Path

(IO::Path) method extension

Defined as:

multi method extension(IO::Path:D:                                         --> Str:D)
multi method extension(IO::Path:D:               Int :$parts               --> Str:D)
multi method extension(IO::Path:D:             Range :$parts               --> Str:D)
multi method extension(IO::Path:D: Str $subst,   Int :$partsStr :$joiner --> IO::Path:D)
multi method extension(IO::Path:D: Str $substRange :$partsStr :$joiner --> IO::Path:D)

Returns the extension consisting of $parts parts (defaults to 1), where a "part" is defined as a dot followed by possibly-empty string up to the end of the string, or previous part. That is "foo.tar.gz" has an extension of two parts: first part is "gz" and second part is "tar" and calling "foo.tar.gz".IO.extension: :2parts gives "tar.gz". If an extension with the specified number of $parts is not found, returns an empty string.

$parts can be a Range, specifying the minimum number of parts and maximum number of parts the extension should have. The routine will attempt to much the most parts it can. If $parts range's endpoints that are smaller than 0 they'll be treated as 0; implementations may treat endpoints larger than 2⁶³-1 as 2⁶³-1. Ranges with NaN or Str endpoints will cause an exception to be thrown.

If $subst is provided, the extension will be instead replaced with $subst and a new IO::Path object will be returned. It will be joined to the file's name with $joiner, which defaults to an empty string when $subst is an empty string and to "." when $subst is not empty. Note: if as the result of replacement the basename of the path ends up being empty, it will be assumed to be . (a single dot).

# Getting an extension: 
say "foo.tar.gz".IO.extension;               # OUTPUT: «gz␤» 
say "foo.tar.gz".IO.extension: :2parts;      # OUTPUT: «tar.gz␤» 
say "foo.tar.gz".IO.extension: :parts(^5);   # OUTPUT: «tar.gz␤» 
say "foo.tar.gz".IO.extension: :parts(0..1); # OUTPUT: «gz␤» 
 
# Replacing an extension 
say "foo.tar.gz".IO.extension: '';                # OUTPUT: «"foo.tar".IO␤» 
say "foo.tar.gz".IO.extension: 'ZIP';             # OUTPUT: «"foo.tar.ZIP".IO␤» 
say "foo.tar.gz".IO.extension: 'ZIP':0parts;    # OUTPUT: «"foo.tar.gz.ZIP".IO␤» 
say "foo.tar.gz".IO.extension: 'ZIP':2parts;    # OUTPUT: «"foo.ZIP".IO␤» 
say "foo.tar.gz".IO.extension: 'ZIP':parts(^5); # OUTPUT: «"foo.ZIP".IO␤» 
 
# Replacing an extension using non-standard joiner: 
say "foo.tar.gz".IO.extension: '',    :joiner<_>;  # OUTPUT: «"foo.tar_".IO␤» 
say "foo.tar.gz".IO.extension: 'ZIP':joiner<_>;  # OUTPUT: «"foo.tar_ZIP".IO␤» 
say "foo.tar.gz".IO.extension: 'ZIP':joiner<_>,
                                       :2parts;     # OUTPUT: «"foo_ZIP".IO␤» 
say "foo.tar.gz".IO.extension: 'ZIP':joiner<_>,
                                       :parts(^5);  # OUTPUT: «"foo_ZIP".IO␤» 
 
# EDGE CASES: 
 
# There is no 5-part extension, so returned value is an empty string 
say "foo.tar.gz".IO.extension: :5parts; # OUTPUT: «␤» 
 
# There is no 5-part extension, so we replaced nothing: 
say "foo.tar.gz".IO.extension: 'ZIP':5parts; # OUTPUT: «"foo.tar.gz".IO␤» 
 
# Replacing a 0-part extension is just appending: 
say "foo.tar.gz".IO.extension: 'ZIP':0parts; # OUTPUT: «"foo.tar.gz.ZIP".IO␤» 
 
# Replace 1-part of the extension, using '.' joiner 
say "...".IO.extension: 'tar'# OUTPUT: «"...tar".IO␤» 
 
# Replace 1-part of the extension, using empty string joiner 
say "...".IO.extension: 'tar':joiner(''); # OUTPUT: «"..tar".IO␤» 
 
# Remove 1-part extension; results in empty basename, so result is ".".IO 
say ".".IO.extension: ''# OUTPUT: «".".IO␤»

class IO::Spec::Unix

From IO::Spec::Unix

(IO::Spec::Unix) method extension

NOTE: Most users would want to use the higher-level routine IO::Path.extension instead of this lower-level version.

Defined as:

method extension(Str:D $path --> Str:D)

Takes a string representing a base name and returns the characters after the last dot ("."), or empty string if no dots are present. The routine makes no attempt to detect path separators and will return everything after the last dot.

$*SPEC.extension('foo.'      ).perl.say;  # OUTPUT: «""␤» 
$*SPEC.extension('foo.txt'   ).perl.say;  # OUTPUT: «"txt"␤» 
$*SPEC.extension('foo.tar.gz').perl.say;  # OUTPUT: «"gz"␤» 
$*SPEC.extension('foo'       ).perl.say;  # OUTPUT: «""␤» 
$*SPEC.extension('bar.foo/foo').perl.say# OUTPUT: «"foo/foo"␤»